poj 1753--Flip Game

来源:互联网 发布:win7数据库安装失败 编辑:程序博客网 时间:2024/05/17 02:39

题目链接:点击打开链接

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
题意:在一个4*4的棋盘上,由黑白两种颜色的棋,让你改变任一一个棋子的颜色(黑->白||白->黑),此时这颗棋子的上下左右颜色自动改变.问最小反动的次数,可以使得棋盘成为一种颜色(全白||全黑)
基本思路:枚举+dfs(我觉得主要是dfs)
因为只有两种颜色,所以每个棋最多翻转一次(最小步数,翻两次就重复了),也就是最多16步,因此对这步数枚举.剩下的就是dfs的遍历.(我就卡在用dfs 怎么遍历最优路径上了.).就是每一行每一行的遍历,如果第一行到达了最后一列就进行下一行.(不要忘了回溯是要还原原始状态).不断的在搜索回溯.

#include <iostream>#include<cstring>using namespace std;bool f;int r[]= {-1,1,0,0};//移动坐标int c[]= {0,0,-1,1};bool mp[4][4]= {false};//4*4图int step;bool judge()//判断是否为一个颜色{    for(int i=0; i<4; i++)    {        for(int j=0; j<4; j++)        {            if(mp[i][j]!=mp[0][0])            {                return false;            }        }    }    return true;}void turn(int x,int y)//翻转棋盘{    mp[x][y]=!mp[x][y];    for(int i=0; i<4; i++)    {        if(x+r[i]>=0&&x+r[i]<4&&y+c[i]>=0&&y+c[i]<4)//边界判断            mp[x+r[i]][y+c[i]]=!mp[x+r[i]][y+c[i]];    }    return ;}void dfs(int x,int y,int deep)//深搜{    if(deep==step)    {        f=judge();       // return ;这个可以不加    }    if(f||x==4)return ;//如果是全部为一个颜色或者是在枚举的这一步上已经把图遍历完了是才返回,这样的话就不难理解怎么寻找最优的了    turn(x,y);//翻转棋盘    if(y<3)    {        dfs(x,y+1,deep+1);    }    else    {        dfs(x+1,0,deep+1);    }    //回溯还原棋盘    turn(x,y);//翻转棋盘    if(y<3)    {        dfs(x,y+1,deep);    }    else    {        dfs(x+1,0,deep);    }    return;}int main(){    char ch;    for(int i=0; i<4; i++)    {        for(int j=0; j<4; j++)        {            cin>>ch;            if(ch=='b') mp[i][j]=true;        }    }    for(step=0; step<=16; step++)    {        dfs(0,0,0);        if(f)break;    }    if(f)cout<<step<<endl;    else cout<<"Impossible\n";    return 0;}


1 0
原创粉丝点击