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.
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).
有4*4的正方形,每個(gè)格子要么是黑色,要么是白色,當(dāng)把一個(gè)格子的顏色改變(黑->白 || 白->黑)時(shí),其周圍上下左右(如果存在的話)的格子的顏色也被反轉(zhuǎn),問至少反轉(zhuǎn)幾個(gè)格子可以使4*4的正方形變?yōu)榧儼谆蛘呒兒冢?/p>
對(duì)于每一個(gè)格子,只有兩個(gè)狀態(tài),將它翻轉(zhuǎn)一次與翻轉(zhuǎn)奇數(shù)次效果是一樣的,翻轉(zhuǎn)零次與翻轉(zhuǎn)偶數(shù)次的效果是一樣的。
因?yàn)橹挥?6個(gè)格子,選擇0個(gè)、1個(gè)、2個(gè)…16個(gè)所有的情況有
枚舉不會(huì)超時(shí),所以我們可以用遞歸的思想模擬0-16重循環(huán),分別表示選擇翻轉(zhuǎn)的棋子個(gè)數(shù)。
|
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注