Pagini recente » Cod sursa (job #2844091) | Cod sursa (job #1545331) | Cod sursa (job #1749247) | Cod sursa (job #3290717) | Cod sursa (job #116225)
Cod sursa(job #116225)
#include <stdio.h>
#include <vector>
#define NMax 500
long n, a[NMax][NMax], opmax;
struct operatie
{
int x, y, t;
}aux2;
std::vector<operatie> op;
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
aux2.x = i; aux2.y = i+1; aux2.t = 1;
op.push_back( aux2 );
schL( i, i+1 );
break;
}
if ( a[i][i+1] <= a[i+1][i] )
{
// schimb coloanele
aux2.x = i; aux2.y = i+1; aux2.t = 2;
op.push_back( aux2 );
schC( i, i+1 );
break;
}
if ( a[i+1][i+1] <= a[i+1][i] )
{
// schimb si si
aux2.x = i; aux2.y = i+1; aux2.t = 1;
op.push_back( aux2 );
aux2.x = i; aux2.y = i+1; aux2.t = 2;
op.push_back( aux2 );
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 );
}