poj 1753 Flip Game(暴力枚举)

Flip Game
 
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 52279   Accepted: 22018

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

bwwb
bbwb
bwwb
bwww

Sample Output

4

Source

 
题目大意:
    一个4✖️4的棋盘上布满了棋子,棋子具有黑白两面,当翻转一个棋子的时候,周边的4个棋子包括自己都会变色。给出一个棋盘,问最少翻转几次,全部变白或者全部变黑。
 
思路:
    棋盘很小,所以暴力枚举即可。从别的大犇那里了解到翻转的顺序对结果是不影响的,但是不是很懂,如果大佬知道可以在评论区里评论。
 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<list>
 9 #include<vector>
10 #include<stack>
11 #include<string>
12 #include<map>
13 #include<numeric>
14 #include<set>
15 #include<functional>
16 #include<sstream>
17 #include<time.h>
18 using namespace std;
19 int mapp[4][4];
20 int turn1[5]={1,-1,0,0,0};
21 int turn2[5]={0,0,0,1,-1};
22 const int inf = 0x3f3f3f3f;
23 int ans=inf;
24 
25 int judge( void )    //判断棋盘是否全为0,或全为1
26 {
27     int temp=mapp[0][0];
28     for(int i = 0; i<4; i++)
29     {
30         for(int j = 0; j<4; j++)
31         {
32             if(temp != mapp[i][j])
33                 return 0;
34         }
35     }
36     return 1;
37 }
38 
39 void turn(int x, int y)
40 {
41     for(int i=0;i<5;i++)
42     {
43         if(x+turn1[i]>=0&&x+turn1[i]<4&&y+turn2[i]>=0&&y+turn2[i]<4)
44             mapp[x+turn1[i]][y+turn2[i]]= !mapp[x+turn1[i]][y+turn2[i]];
45     }
46 }
47 
48 int dfs( int x, int y, int t)
49 {
50     if(judge())
51     {
52         ans=min(ans,t);
53         return 0;
54     }
55     if(x >= 4 || y >= 4)
56         return 0;
57     int nx=(x+1)%4;   //坐标,满4换成第二行
58     int ny=y+(x+1)/4;
59     
60     dfs(nx,ny,t);       //搜索翻转
61     turn(x,y);
62     dfs(nx,ny,t+1);
63     turn(x,y);
64     
65     return 0;
66 }
67 int main()
68 {
69     
70     for(int i=0;i<4;i++)
71     {
72         char s[10];
73         gets(s);
74         for(int j=0;j<4;j++)
75         {
76             if(s[j]=='b') mapp[i][j]=1;
77             else mapp[i][j]=0;
78         }
79     }
80     
81     dfs(0,0,0);
82     
83     if(ans!=inf)
84         printf("%d
",ans);
85     else
86         printf("Impossible
");
87 }
原文地址:https://www.cnblogs.com/Tangent-1231/p/9740096.html