Cod sursa(job #610775)

Utilizator vladtarniceruVlad Tarniceru vladtarniceru Data 29 august 2011 10:43:03
Problema Grozavesti Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
# include <fstream>
# include <vector>
using namespace std;

struct solutie {char c; int i, j;} ;
vector <solutie> v;
int n, a[301][301];

solutie make_solutie (char ch, int l, int c)
{
    solutie aa;
    aa.c = ch;
    aa.i = l;
    aa.j = c;
    return aa;
}
inline void swap (int &a, int &b)
{
    a ^= b ^= a ^= b;
}

void linie (int i1, int i2)
{
    for (int i = 1; i <= n; ++i)
        swap (a[i1][i], a[i2][i]);
}

void coloana (int j1, int j2)
{
    for (int j = 1; j <= n; ++j)
        swap (a[j][j1], a[j][j2]);
}
int main ()
{
    ifstream f ("grozavesti.in");
    ofstream g ("grozavesti.out");

    f >> n;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            f >> a[i][j];

    for (int i = 1; i < n; ++i)
    {
        int var = a[i][i], poz = 0;
        for (int j = i + 1; j <= n; ++j)
            if (var > a[j][j])
                poz = j, var = a[j][j];
        if (poz)
        {
            v.push_back (make_solutie ('L', i, poz));
            v.push_back (make_solutie ('C', i, poz));
            linie (i, poz);
            coloana (i, poz);
        }
    }

    for (vector <solutie> :: iterator it = v.begin (); it != v.end (); ++it)
        g << it -> c << ' ' << it -> i << ' ' << it -> j << '\n';
    if (!v.size ()) g << "0\n";
    g.close ();
    return 0;
}