Cod sursa(job #1703729)

Utilizator atatomirTatomir Alex atatomir Data 17 mai 2016 15:54:09
Problema Tije Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long

#define maxN 111

int n, m, i, j, step;
bool us[maxN];
vector<int> to_move;

void move(int a, int b) {
    printf("%d %d\n", a, b);
}

void top_to_pos(int st, int pos) {
    int aux = (st == 1 ? 2 : 1);

    if (pos == 1) return;

    move(st, aux);
    for (int i = 1; i < pos; i++) move(st, n + 1);
    move(aux, st);
    for (int i = 1; i < pos; i++) move(n + 1, st);
}

void solve(int bg) {
    int i;
    to_move.clear();

    for (; us[bg] == false; bg = (bg + step) % n) {
        if (bg == 0) {
            bg = n;
            if (us[bg]) break;
        }

        to_move.pb(bg);
        us[bg] = true;
    }

    move(to_move[0], n + 1);
    for (i = 1; i < to_move.size(); i++) move(to_move[i], to_move[i - 1]);
    move(n + 1, to_move.back());
}

int main()
{
    freopen("tije.in","r",stdin);
    freopen("tije.out","w",stdout);

    scanf("%d", &n);
    if (n == 1) return 0;

    for (i = 1; i < n; i++) {
        memset(us, 0, sizeof(us));
        step = i;

        for (j = 1; j <= n; j++)
            if (!us[j])
                solve(j);

        for (j = 1; j <= n; j++)
            top_to_pos(j, n - i);
    }

    return 0;
}