Pagini recente » Cod sursa (job #3156595) | Cod sursa (job #2529046) | Cod sursa (job #2554198) | Cod sursa (job #1187322) | Cod sursa (job #116241)
Cod sursa(job #116241)
#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 = INF;
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 )
{
if ( x != i && y == i )
{
s[smax].x = i;
s[smax].y = x;
s[smax].t = 1;
schL( i, x );
}
if ( x == i && y != i )
{
s[smax].x = i;
s[smax].y = y;
s[smax].t = 2;
schC( i, y );
}
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;
}
}