1753:Flip Game(bfs)

总时间限制: 

1000ms
 
内存限制: 
65536kB
描述
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.
输入
The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.
输出
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).
样例输入
bwwb
bbwb
bwwb
bwww
样例输出
4
来源
Northeastern Europe 2000

题目意思大体是:4*4的棋盘,棋子一面黑b一面白w,可以对一个棋子翻转操作,使得上下左右中5个棋子翻转,求最少翻转次数使得初始状态变成全部为黑或白。
题目有了思路就会很简单。
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 char a[4][4];
 4 int dr[5]= {1,-1,0,0,0};                    //上下左右中5个方向
 5 int dc[5]= {0,0,1,-1,0};
 6 int inf=1024;
 7 void turn(int r,int c) {                    //对(r,c)翻转操作
 8     for(int i=0; i<5; i++) {
 9         int rr=r+dr[i];
10         int cc=c+dc[i];
11         if(rr>3||cc>3||cc<0||rr<0)continue;
12         if(a[rr][cc]=='b')a[rr][cc]='w';
13         else a[rr][cc]='b';
14     }
15 }
16 bool check() {                            //判断当前状态是否符合要求
17     for(int i=0; i<4; i++) {
18         for(int j=0; j<4; j++) {
19             if(a[i][j]!=a[0][0])return false;
20         }
21     }
22     return true;
23 }
24 void dfs(int r,int c,int k) {
25     if(check()) {
26         inf=min(inf,k);
27         return ;
28     }
29     if(r==4)return;
30     turn(r,c);                            //翻转
31     if(c==3)dfs(r+1,0,k+1);                //下一个元素,换行 
32     else dfs(r,c+1,k+1);
33     turn(r,c);                            //再翻转相当于没有翻转
34     if(c==3)dfs(r+1,0,k);
35     else dfs(r,c+1,k);
36 }
37 int main() {
38     for(int i=0; i<4; i++) {
39         for(int j=0; j<4; j++) {
40             cin>>a[i][j];
41         }
42     }
43     dfs(0,0,0);
44     if(inf<=16)cout<<inf<<endl;
45     else cout<<"Impossible"<<endl;
46     return 0;
47 }

猜你喜欢

转载自www.cnblogs.com/aiqinger/p/12631683.html