国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

POJ 1753 Flip Game (枚舉)

2019-11-08 18:35:44
字體:
供稿:網(wǎng)友

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

bwwbbbwbbwwbbwww

Sample Output

4

題意

有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è)所有的情況有

C016+C116+C216+C316+...+C1516+C1616=216

枚舉不會(huì)超時(shí),所以我們可以用遞歸的思想模擬0-16重循環(huán),分別表示選擇翻轉(zhuǎn)的棋子個(gè)數(shù)。

AC 代碼

#include <iostream>#include<stdio.h>#include<math.h>#include<string.h>#include<algorithm>#include<stdlib.h>using namespace std;bool bits[16];bool flag=false;void rese(int n) //翻轉(zhuǎn){ int x=n/4,y=n%4; for(int i=max(0,y-1); i<min(y+2,4); i++) bits[x*4+i]=!bits[x*4+i]; if(x!=0) bits[(x-1)*4+y]=!bits[(x-1)*4+y]; if(x!=3) bits[(x+1)*4+y]=!bits[(x+1)*4+y];}bool jud() //判斷是否一致{ bool ini=bits[0]; for(int i=1; i<16; i++) if(ini!=bits[i]) return false; return true;}void solve(int maxx,int now,int step){ if(maxx==step) //翻轉(zhuǎn)完最后一枚棋子 { if(jud()) //滿足狀態(tài) { flag=true; printf("%d/n",maxx); } return; } for(int i=now; i<16; i++) //從上次翻轉(zhuǎn)位置繼續(xù) { rese(i); //翻轉(zhuǎn)i solve(maxx,i+1,step+1); if(flag)return; //找到答案,返回 rese(i); //恢復(fù)原來狀態(tài) }}int main(){ char str[4][4]; for(int i=0; i<4; i++) { cin>>str[i]; for(int j=0; j<4; j++) bits[i*4+j]=(str[i][j]=='b')?1:0; } for(int i=0; i<=16; i++) //i 為要翻轉(zhuǎn)的棋子個(gè)數(shù) solve(i,0,0); if(!flag) //沒有找到答案 printf("Impossible/n"); return 0;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宝兴县| 红桥区| 息烽县| 璧山县| 乃东县| 武威市| 鹤壁市| 漠河县| 合阳县| 枞阳县| 绍兴市| 吕梁市| 大余县| 大埔县| 湖南省| 贵州省| 通道| 墨竹工卡县| 凤山市| 台东县| 蒲江县| 乌拉特后旗| 阿坝| 建水县| 浙江省| 麻江县| 乐安县| 成都市| 博白县| 柳江县| 望谟县| 昔阳县| 林州市| 惠州市| 池州市| 二连浩特市| 碌曲县| 巍山| 原平市| 高碑店市| 荆门市|