Cod sursa(job #231524)

Utilizator hello2alex gen2 hello2 Data 14 decembrie 2008 12:34:52
Problema Tablete Scor 100
Compilator cpp Status done
Runda Algoritmiada 2009, Runda 1, Studenti Marime 2.9 kb
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function
#include <algorithm>
#include <math.h>

using namespace std;

int n;
int nPatrat;
int k;
int** mat;

bool isSolutionWithoutDistinct()
{
	for (int i = 0; i < n; i++)
	{
		if (mat[i][k] % 2 != 0)
		{
			//cout << "a" << i <<" "<<k<<endl;
			return false;
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (j - 1 >= 0)
				if (mat[i][j - 1] > mat[i][j])
				{
					//cout << "b" << i <<" "<<j<<endl;
					return false;
				}
		}
	}
	return true;
}

bool isSolution()
{
	if (!isSolutionWithoutDistinct())
		return false;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			long x = mat[i][j];
			for (int ii = 0; ii < n; ii++)
			{
				for (int jj = 0; jj < n; jj++)
				{
					if (mat[ii][jj] == x && ii != i && jj != j)
					{
						//cout << "x" << ii <<" "<<jj<<endl;
						return false;
					}
				}
			}
		}
	}
	return true;
}


int main()
{
	ifstream indata; // indata is like cin
	ofstream outdata;
	indata.open("tablete.in"); // opens the file
	outdata.open("tablete.out");
	if(!indata) { // file couldn't be opened
		cerr << "Error: file could not be opened" << endl;
		exit(1);
	}
	indata >> n >> k;
	nPatrat = n * n;
	k--;
	//cout << "n=" << n << " | k=" << k << endl;
	indata.close();
	mat = new int*[n];
	for (int i = 0; i<n; i++)
	{
		mat[i] = new int[n];
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			mat[i][j] = i * n + j + 1;
		}
	}
	//for (int i = 0; i < n; i++)
	//{
	//	for (int j = 0; j < n; j++)
	//	{
	//		cout << mat[i][j] << "\t";
	//	}
	//	cout << endl;
	//}
	;
	for (int i = n - 1; i > 0; i--)
	{
		if (mat[i][k] % 2 == 0)
			continue;
		int val = mat[i][k];
		for (int j = k; j > 0; j--)
		{
			mat[i][j] = mat[i][j - 1];
		}
		mat[i][0] = mat[i - 1][0];
		for (int j = 0; j < n - 1; j++)
		{
			mat[i - 1][j] = mat[i - 1][j + 1];
		}
		mat[i - 1][n - 1] = val;
	}
	bool toatePare = true;
	for (int i = 0; i < n; i++)
	{
		if (mat[i][k] % 2 != 0)
		{
			//cout << "aaa" << i <<" "<<k<<endl;
			toatePare = false;
			break;
		}
	}
	if (!toatePare)
	{
		int val = mat[0][k];
		for (int i = k; i < n; i++)
		{
			mat[0][i] = mat[0][i + 1];
		}
		int index = 1;
		for (int i = 1; i < n; i++)
		{
			if (mat[i][0] % 2 == 0 && mat[i][0] > mat[0][n - 1])
			{
				index = i;
				break;
			}
		}
		mat[0][n - 1] = mat[index][0];
		mat[index][0] = val;
	}
	;
	//cout << endl << endl;
	//for (int i = 0; i < n; i++)
	//{
	//	for (int j = 0; j < n; j++)
	//	{
	//		cout << mat[i][j] << "\t";
	//	}
	//	cout << endl;
	//}
	//cout << endl << "is solution? " << isSolution() << endl;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			outdata << mat[i][j] << " ";
		}
		outdata << endl;
	}
	outdata.close();
	return 0;
}