Flip Game POJ

首先要鸣谢一下教了我半天的同学:

看不明白我写的,可以看一下他的  http://www.cnblogs.com/orion7/p/7469236.html

 

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules: 

  1. Choose any one of the 16 pieces. 
  2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).


Consider the following position as an example: 

bwbw 
wwww 
bbwb 
bwwb 
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become: 

bwbw 
bwww 
wwwb 
wwwb 
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal. 

Input

The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.

Output

Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).

Sample Input

bwwb bbwb bwwb bwww

Sample Output

4

 

题解:

    棋盘反转,转成朝上的面一样就可以了。

    一开始我是找规律一个一个归类,可是太麻烦了,后来还漏了情况,emmmm特别恶心。

抱完大腿之后才知道使用dfs+枚举。逐个算起,前不久学的dfs现在隔了一段时间后再看就理解的很清楚了。

AC代码:

 

#include<iostream>
#include<cstdio>
using namespace std;
bool map[5][5];
int node;
int flag;
int search()//查找函数
{
for(int i=1;i<=4;i++) { for(int j=1;j<=4;j++) { if(map[i][j]!=map[1][1]) return 0; } } return 1; } int rode[2][5]={0,1,-1,0,0,0,0,0,1,-1}; void change(int x,int y)//反转函数
{
for(int i=0;i<5;i++) { int fx=x+rode[0][i]; int fy=y+rode[1][i]; if(fx>0&&fx<5&&fy>0&&fy<5) { map[fx][fy]=!map[fx][fy]; } } } void dfs(int x,int y,int z)//按照行来的搜索
{
if(z==node&&search()) { flag=1; return; } if(x>4) return; change(x,y); if(y<4) dfs(x,y+1,z+1); else dfs(x+1,1,z+1);//千万不能写成z++引为后面回溯会受到影响!!! change(x,y);//回溯
if(y<4) dfs(x,y+1,z); else dfs(x+1,1,z); } int main() { for(int i=1;i<=4;i++) { for(int j=1;j<=4;j++) { char d; cin>>d; if(d=='b') map[i][j]=true; else map[i][j]=false; } getchar(); } flag=0; for(node=0;node<=16;node++) { dfs(1,1,0); if(flag) break; } if(flag) printf("%d ",node); else printf("Impossible "); return 0; }

 

把dfs那个按照列来:

void dfs(int x,int y,int z)
{
     if(z==node&&search())
     {
         flag=1;
         return;
     }
     if(y>4)
     {
         return;
     }
     change(x,y);
     if(x<4)
         dfs(x+1,y,z+1);
     else
        dfs(1,y+1,z+1);
     change(x,y);
     if(x<4)
         dfs(x+1,y,z);
     else
        dfs(1,y+1,z);
}

今天也是元气满满的一天,good luck!

 

我想要变得高一点,最好能伸手给你一片天。
原文地址:https://www.cnblogs.com/cattree/p/7483816.html