Cod sursa(job #116246)

Utilizator mithyPopovici Adrian mithy Data 18 decembrie 2007 00:47:17
Problema Grozavesti Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <stdio.h>
#define NMax 305
#define INF 30000

long n, a[NMax][NMax], smax;
struct sol
{
	int x, y, t;
}s[NMax];

void citire();
void rez();
void schL( int x, int y );
void schC( int x, int y );

int main()
{
	citire();
	rez();
	return 0;
}

void rez()
{
	FILE *g = fopen( "grozavesti.out", "wt" );
	int i, j, k, min, aux, x, y;

	for (i=0; i<n-1; i++)
	{
		min = a[i][i];
		x = y = i;
		for (k=i; k<n; k++)
			for (j=i; j<n; j++)
				if ( a[k][j] < min )
				{
					min = a[k][j];
					x = k;
					y = j;
				}

		if ( x != i || y != i )
		{
			// daca e alta linie, aceeasi coloana
			if ( x != i && y == i )
			{
				s[smax].x = i;
				s[smax].y = x;
				s[smax].t = 1;
				schL( i, x );
			}
			// daca e alta coloana, aceeasi linie
			if ( x == i && y != i )
			{
				s[smax].x = i;
				s[smax].y = y;
				s[smax].t = 2;
				schC( i, y );
			}
			// daca sunt schibate si lin si col
			if ( x != i && y != i )
			{
				s[smax].x = i;
				s[smax].y = x;
				s[smax].t = 1;
				schL( i, x );
				smax++;
				s[smax].x = i;
				s[smax].y = y;
				s[smax].t = 2;
				schC( i, y );
			}
			smax++;
		}
	}

	fprintf( g, "%ld\n", smax );
	for (i=0; i<smax; i++)
	{
		if ( s[i].t == 1 )
			fprintf( g, "L %d %d\n", s[i].x+1, s[i].y+1 );
		if ( s[i].t == 2 )
			fprintf( g, "C %d %d\n", s[i].x+1, s[i].y+1 );
	}
	fclose(g);
}
void citire()
{
	int i, j;
	FILE *f = fopen( "grozavesti.in", "rt" );
	fscanf( f, "%ld", &n );
	for (i=0; i<n; i++)
		for (j=0; j<n; j++)
			fscanf( f, "%ld", &a[i][j] );
	fclose(f);
}
void schL( int x, int y )
{
	int i, aux;

	for (i=0; i<n; i++)
	{
		aux = a[x][i];
		a[x][i] = a[y][i];
		a[y][i] = aux;
	}
}
void schC( int x, int y )
{
	int i, aux;
	for (i=0; i<n; i++)
	{
		aux = a[i][x];
		a[i][x] = a[i][y];
		a[i][y] = aux;
	}
}