Cod sursa(job #117380)

Utilizator tvladTataranu Vlad tvlad Data 21 decembrie 2007 12:54:50
Problema Tije Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <cstdio>
#include <vector>
using namespace std;

const int N = 150;

int n;
vector<int> tija[N];

void print() {
	for (int i = n-1; i >= 0; --i) {
		for (int j = 1; j <= n+1; ++j) {
			if(tija[j].size() > i) {
				printf("%d ",tija[j][i]);
			} else {
				printf("  ");
			}
		}
		printf("\n");
	}
}

void check() {
	bool* found = new bool[n+1];
	bool probleme_gogule = false;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) found[j] = false;
		for (int j = 0; j < tija[i].size(); ++j) {
			found[tija[i][j]] = true;
		}
		for (int j = 1; j <= n; ++j) probleme_gogule = probleme_gogule || !found[j];
	}
	if (probleme_gogule) {
		fprintf(stderr,"Gresit la n = %d!!!!!\n",n);
		printf("Gresit la n = %d!!!!!\n",n);
	} else {
		printf("Corect\n");
	}
}

inline void move ( int a, int b ) {
	tija[b].push_back(tija[a].back());
	tija[a].pop_back();
#ifndef BAGA_MARE
	printf("%d %d\n",a,b);
#endif
}

void solve() {
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			tija[i].push_back(i);
		}
	}

	for (int i = 2; i <= n; ++i) {
		for (int j = 1; j <= n-1; ++j) move(i,n+1);
		for (int j = 1; j <= i-1; ++j) move(1,i);
		for (int j = 2; j <= i-1; ++j)
			for (int k = n; k >= n-i+j+1; --k) move(j,j-1);
		for (int j = 1; j <= i-1; ++j) move(n+1,j);
		for (int j = 1; j <= n-i; ++j) move(n+1,i);
	}
}

int main() {
	freopen("tije.in","rt",stdin);
	freopen("tije.out","wt",stdout);
#ifdef BAGA_MARE
	for (n = 1; n <= 100; ++n) {
		for (int i = 1; i <= n+1; ++i) tija[i].clear();
		solve();
		print();
		check();
	}
#else
	scanf("%d",&n);
	solve();
#endif
	return 0;
}