Cod sursa(job #783353)

Utilizator Cosmin1490Balan Radu Cosmin Cosmin1490 Data 2 septembrie 2012 16:38:45
Problema Tablete Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.03 kb
#include <iostream>
#include <fstream>

using namespace std;

const char infile[] = "tablete.in";
const char outfile[] = "tablete.out";




int main(int argc, char* argv[])
{
    int N, K;
    fstream fin(infile, ios::in);
    fin >> N >> K;
    fin.close();
    
    
    
    fstream fout(outfile, ios::out);
    
    
    int N2 = N * N;
    int column = 1;
    
    if((N & 1) == 0 && (K & 1) == 0)
    {
        for(int i = 1 ; i <= N2; i++)
        {
            
            fout << i << " ";
            
            column++;
            if(column == N + 1)
            {
                column = 1;
                fout << "\n";
            }
            
        }
    }
    else
    {
        bool swap = false;
        bool swap2 = false;
        
        for(int i = 1 ; i <= N2; i++)
        {
            if((column == 1) && (swap == true))
            {
                fout << (i - 1)  - N + K << " ";
                swap = false;
            }
            else if( swap2 == true )
            {
                if(column <= K)
                {
                    fout << i - 1 << " ";

                }
                else
                {
                    fout << i << " ";

                }
            }
            else if(i == N2 - N && (i + K) & 1)
            {
                fout << i + K << " ";
                swap2 = true;

            }
            else if( column == K &&  i & 1)
            {
                fout << i + 1 << " ";
                swap = true;
            }
            else
            {
                if(swap == true)
                {
                    fout << i + 1 << " ";
                }
                else
                {
                    fout << i << " ";
                }
            }
            
            column++;
            if(column == N + 1)
            {
                column = 1;
                fout << "\n";
            }

        }

    }
    
    
    fout.close();
}