Cod sursa(job #116223)

Utilizator mithyPopovici Adrian mithy Data 17 decembrie 2007 23:14:00
Problema Grozavesti Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.9 kb
#include <stdio.h>
#define NMax 305

long n, a[NMax][NMax], opmax;
struct operatie
{
	int x, y, t;
}op[NMax];

void citire();
void rez();
void afis();
void schL( int x, int y );
void schC( int x, int y );
int main()
{
	citire();
	rez();
	afis();
	return 0;
}
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;
	}
}
void afis()
{
	int i;
	FILE *g = fopen( "grozavesti.out", "wt" );
	fprintf( g, "%ld\n", opmax );

	for (i=0; i<opmax; i++)
	{
		if ( op[i].t == 1 )
			fprintf( g, "L %d %d\n", op[i].x, op[i].y );
		if ( op[i].t == 2 )
			fprintf( g, "C %d %d\n", op[i].x, op[i].y );
	}
	fprintf( g, "\n" );
	fclose( g );
}
void rez()
{
	int i, j, ok, aux;

	do 
	{
		ok = 0;
		for (i=0; i<n-1; i++)
		
			if ( a[i][i] > a[i+1][i+1] )
			{
				ok = 1;
				if ( a[i+1][i] <= a[i][i+1] )
				{
					// schimb liniile
					op[opmax].x = i;
					op[opmax].y = i+1;
					op[opmax].t = 1;
					opmax++;
					schL( i, i+1 );					
					break;
				}
			
				if ( a[i][i+1] <= a[i+1][i] )
				{
					// schimb coloanele
					op[opmax].x = i;
					op[opmax].y = i+1;
					op[opmax].t = 2;
					opmax++;

					schC( i, i+1 );
					break;
				}

				if ( a[i+1][i+1] <= a[i+1][i] )
				{
					// schimb si si
					op[opmax].x = i;
					op[opmax].y = i+1;
					op[opmax].t = 1;
					opmax++;
					op[opmax].x = i;
					op[opmax].y = i+1;
					op[opmax].t = 2;
					opmax++;

					schL( i, i+1 );
					schC( i, i+1 );
					break;
				}
			}
	} while(ok);
}
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 );
}