Cod sursa(job #2257408)

Utilizator nimicLeoveanu Mihaita Alexandru nimic Data 10 octombrie 2018 00:43:57
Problema Tablete Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <fstream>
using namespace std;
ifstream fin("tablete.in");
ofstream fout("tablete.out");

int n,m,k,v[1006][1006],i,j, aux;

int main()
{
    fin>>n>>m;
    if(n%2==0&&m%2==0)for(i=1;i<=n;i++)for(j=1;j<=n;j++) {v[i][j]=n*(i-1)+j;}
    if(n%2==0&&m%2!=0)
    {
        for(i=1;i<=n;i++)
            v[i][1]=i;
        k=n*n-n+1;
        for(i=1;i<=n;i++)
        {
                v[i][n]=k;
                k++;
        }
        k=n+1;
        for(i=1;i<=n;i++)
            for(j=2;j<=n-1;j++)
            {
                v[i][j]=k;
                k++;
            }
    }
    if(n%2!=0 && m%2==0)
    {
        for(i=1;i<=n;i++)
            v[i][1]=i;
        for(i=2;i<=n;i++)
            v[n][i]=n+i-1;
        k=2*n;
        for(i=1;i<=n-1;i++)
            for(j=2;j<=n;j++)
            {
                v[i][j]=k;
                k++;
            }
    }
    if(n%2!=0&&m%2!=0)
    {
        for(i=1;i<=n;i++)
            v[i][1]=i;
        for(i=2;i<=n-1;i++)
            v[n][i]=n+i-1;
        v[n][n]=n*n;
        k=n+n-1;
        for(i=1;i<=n-1;i++)
            for(j=2;j<=n;j++)
            {
                v[i][j]=k;
                k++;
            }
        aux = v[n][m];
        v[n][m] = v[n - 1][1];
        v[n - 1][1] = aux;

        for(i = m; i>1; i--)
        {
            aux = v[n][i];
            v[n][i] = v[n][i - 1];
            v[n][i - 1] = aux;
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
            fout<<v[i][j]<<" ";
        fout<< endl;
    }
    return 0;
}