Flip Game POJ - 1753

首先要鳴謝一下教了我半天的同窗:html

看不明白我寫的,能夠看一下他的  http://www.cnblogs.com/orion7/p/7469236.htmlnode

 

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: ios

  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. ide

Input函數

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

Outputspa

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).code

Sample Inputhtm

bwwb bbwb bwwb bwwwblog

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\n",node); else printf("Impossible\n"); 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!

相關文章
相關標籤/搜索