ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1060. Flip Game

WA #8
Posted by Faeton (Kyiv - Mohyla Academy) 29 Feb 2008 06:16
please, help me to find a bug, or give me some tests

Here is my code.

#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <stdio.h>

#define MAX_VAL 100

using namespace std;

int res = -1;

vector<int> mem;

void go(bool a[4][4],int cur) {
    long hex = 0,step = 1;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            if (a[i][j]==1) hex+=step;
            step*=2;
        }
    }

    if (hex==65535) {
        if (cur<res||res==-1) res = cur;
        return;
    }

    if (cur<mem[hex]) {
        mem[hex] = cur;

        int mov[4][2];

        mov[0][0] = 1; mov[0][1] = 0;
        mov[1][0] = -1;mov[1][1] = 0;
        mov[2][0] = 0; mov[2][1] = 1;
        mov[3][0] = 0; mov[3][1] = -1;


        for (int x = 0; x < 4; x++) {
            for (int y = 0; y < 4; y++) {
                bool next[4][4];

                for (int w1 = 0; w1 < 4; w1++) for (int w2 = 0; w2 < 4; w2++) next[w1][w2] = a[w1][w2];

                for (int k = 0; k < 4; k++) {
                    int nx = x+mov[k][0],ny = y+mov[k][1];

                    if (nx>=0&&ny>=0&&nx<4&&ny<4)
                        next[nx][ny] = 1-next[nx][ny];// flip
                }
                next[x][y] = 1-next[x][y];

                go(next,cur+1);
            }
        }
    }
}

int main() {
    bool a[4][4],f = true;

    string s;
    for (int i = 0; i < 4; i++) {
        cin >> s;
        for (int j = 0; j < 4; j++) {
            if (s[j]=='w') a[i][j] = 0;
            else {
                a[i][j] = 1;
                f = false;
            }
        }
    }
    if (f) {
        cout << "0";
        return 0;
    }

    for (long x = 0; x < 1<<16+1; x++) mem.push_back(MAX_VAL);

    go(a,0);

    if (res==-1) cout << "Impossible";
    else cout << res;

    return 0;
}