Cod sursa(job #231029)

Utilizator MariusMarius Stroe Marius Data 14 decembrie 2008 11:19:28
Problema Tablete Scor 100
Compilator cpp Status done
Runda Algoritmiada 2009, Runda 1, Clasele 9-10 Marime 1.42 kb
#include <fstream>
#include <iostream>
#include <bitset>
using namespace std;

const char iname[] = "tablete.in";
const char oname[] = "tablete.out";

#define MAXN  1005
#define FOR(i, a, b)  for (int i = (a); i < (b); ++ i)

bitset <1000005> used;

int M[MAXN][MAXN];      // 4 MB

int eval(const int N, const int K)
{
    used.reset();
    int wrong = false;
    FOR (i, 0, N) {
        FOR (j, 0, N) {
            if (j > 0) if (M[i][j - 1] >= M[i][j])
                wrong = true;
            used[ M[i][j] ] = true;
        }
        if (M[i][K - 1] & 1)
            wrong = true;
    }
    FOR (i, 0, N * N) if (!used[i + 1])
        wrong = true;

    return (wrong == false);
}

int main(void)
{
    ifstream in(iname);
    int N, K;
    in >> N >> K;

    int cnt = 0, num = 0;
    FOR (i, 0, N) {
        while (cnt < K || (num & 1)) {
            num ++, cnt ++;
        }
        cnt --;
        used[num] = true;
        cnt -= (K - 1);
        M[i][K - 1] = num;
    }
    cnt = 0, num = 0;
    FOR (i, 0, N) FOR (j, 0, K - 1) {
        if (used[++ num])  ++ num;
        M[i][j] = num;
    }
    FOR (i, 0, N) FOR (j, K, N) {
        if (used[++ num])  ++ num;
        M[i][j] = num;
    }

    ofstream out(oname);
    FOR (i, 0, N) {
        FOR (j, 0, N)
            out << M[i][j] << " ";
        out << "\n";
    }
    in.close(), out.close();
    return 0;
}