POJ 1753 Flip Game

来源:互联网 发布:企鹅fm播音软件 编辑:程序博客网 时间:2024/06/06 03:29

http://poj.org/problem?id=1753

1.反转奇数次效果相同,偶数次效果也相同,因此等价于每个棋子翻转0或1次即可

2.使用DFS按照反转个数进行枚举即可。

#include <iostream>#include <string.h>#include <stdio.h>using namespace std;int chess[7][7];int x[5] = {0,0,1,0,-1};int y[5] = {0,1,0,-1,0};int flag, step;int judge(){      for(int i = 1; i <= 4; i++)            for(int j = 1; j <= 4; j++)                  if(chess[i][j] != chess[1][1])                        return 0;      return 1;}void flip(int row,  int col){    for(int i = 0; i <= 4; i++) {        if(chess[row+x[i]][col+y[i]] == 1)      chess[row+x[i]][col+y[i]] = 0;        else      chess[row+x[i]][col+y[i]] = 1;      }      return;}void dfs(int row,int col, int deep){      if(deep == step) {            flag = judge();            return;      }      if(flag || row == 5)    return;      flip(row,col);      if(col < 4)  dfs(row,col+1,deep+1);      else  dfs(row+1,1,deep+1);      flip(row,col);      if(col < 4)       dfs(row,col+1,deep);      else  dfs(row+1,1,deep);      return;}int main(){//      freopen("in.txt", "r", stdin);      char temp;      memset(chess,0,sizeof(chess));      for(int i = 1; i <= 4; i++)            for(int j = 1; j <= 4; j++) {                  cin >>temp;                  if(temp == 'b')                        chess[i][j] = 1;            }      for(step = 0; step <= 16; step++) {            dfs(1,1,0);            if(flag)                  break;      }      if(flag)            cout<<step<<endl;      else            cout<<"Impossible"<<endl;      return 0;}


0 0
原创粉丝点击