poj1753之DFS

来源:互联网 发布:freebsd和centos 编辑:程序博客网 时间:2024/06/05 00:09
Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 45109 Accepted: 19321

Description

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

bwwbbbwbbwwbbwww

Sample Output

4

Source

Northeastern Europe 2000

题目大意:现在有一个4*4的棋盘,其中‘b’代表是黑棋,‘w’代表是白棋。现在有一种翻棋的方法,当翻某个棋时,这个棋子和它上下左右的棋子都变为相反的颜色,求最少翻几个棋子,所有的棋子的颜色能够相同。

题目分析:用DFS暴力解决就可以了,这里有一点就是对于下一次搜索可以只搜前面搜索的后面的棋子,即row*4+col<i*4+j,而且下一次搜索时每次之前要设置为已访问,防止被再次搜到,同时棋子肯定要执行翻棋子的操作,这种搜索完成之后,visit数组恢复为0,棋子恢复到未翻棋子时的情况,让其能被其他情况搜到。

代码:
#include <iostream>#include <queue>using namespace std;int map[10][10];int MIN=17;int visit[10][10]={0};int dx[]={-1,0,1, 0,0};int dy[]={ 0,1,0,-1,0};//判断是否全白或全黑bool finish(){    int n=0;    for(int i=1;i<=4;i++){        for(int j=1;j<=4;j++){            if(map[i][j]){                n++;            }        }    }    if(n%16==0)        return true;    else        return false;}//翻棋子void flip(int i,int j){    for(int k=0;k<5;k++){        int x=i+dx[k];        int y=j+dy[k];        if(x>=1&&x<=4&&y>=1&&y<=4){            map[x][y]=!map[x][y];        }    }}void dfs(int row,int col,int steps){    if(finish()){         //如果全白或全黑,则可结束。        if(MIN>steps)            MIN=steps;        return;    }    if(steps>=16){      //每个棋子最多只能翻一次,当其被翻两次时,相当于没被翻过。        return;    }    for(int i=1;i<=4;i++){        for(int j=1;j<=4;j++){            if(!visit[i][j]&&row*4+col<i*4+j){                visit[i][j]=1;                flip(i,j);                dfs(i,j,steps+1);                flip(i,j);                visit[i][j]=0;            }        }    }}int main(){    char c;    for(int i=1;i<=4;i++){        for(int j=1;j<=4;j++){            cin>>c;            if(c=='b')  map[i][j]=0;            else        map[i][j]=1;        }    }    dfs(0,0,0);    if(MIN<17)        cout<<MIN<<endl;        else            cout<<"Impossible"<<endl;    return 0;}