棋盤翻轉

/*Flip Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 40202 Accepted: 17453
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: 
Choose any one of the 16 pieces. 
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*/

#include <stdio.h>

char map[4][4];
bool judge()//判斷是否顏色相同 
{
int i, x1, y1, x2, y2;
for(i = 0; i < 15; i++)
{
x1 = i / 4;
y1 = i % 4;
x2 = (i + 1) / 4;
y2 = (i + 1) % 4;
if(map[x1][y1] != map[x2][y2])
return false;
}
return true;//如果顏色相同則返回true,否則返回false 
}
void change(int i)
{
int x = i / 4;
int y = i % 4;
map[x][y] = (map[x][y] == 'b' ? 'w' : 'b');//轉換棋子顏色 
if(x > 0)
map[x-1][y] = (map[x-1][y] == 'b' ? 'w' : 'b');//轉換上 
if(x < 3)
map[x+1][y] = (map[x+1][y] == 'b' ? 'w' : 'b');//轉換下 
if(y > 0)
map[x][y-1] = (map[x][y-1] == 'b' ? 'w' : 'b');//轉換左 
if(y < 3)
map[x][y+1] = (map[x][y+1] == 'b' ? 'w' : 'b');//轉換右 
}
bool DFS(int n, int num)
{
if(n == 0)
{
if(judge())
return true;//如果翻轉後顏色相同則返回true 
else
return false;//否則返回false 
}
for(int i = num; i < 16; i++)
{
change(i);//翻轉第i個棋子,其相鄰棋子也會發生變化 
if(DFS(n-1, i+1))//翻轉一次,少一次,但棋子會往後翻 
return true;
change(i);//變回原來的樣子 
}
return false;
}
int main()
{
int i, j;
bool flag = false;
for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
scanf("%c", &map[i][j]);
}
getchar();
}
for(i = 0; i <= 16; i++)
{
if(DFS(i, 0))//i表示翻了幾個棋子,從0開始,最多翻轉16個。0表示從第一個開始翻轉 
{
flag = true;
break;
}
}
if(flag)
{
printf("%d\n", i);//輸出所翻轉的棋子個數 
}
else
{
printf("Impossible\n");//如果16個棋子都翻轉了一次,讓沒有達到目標,輸出impossible,因爲同一個棋子翻轉偶數次和沒有翻轉的狀態一樣,翻轉奇數次和翻轉一次
//的狀態一樣 
}
return 0;
}
發佈了33 篇原創文章 · 獲贊 1 · 訪問量 9677
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章