POJ 1753 Flip Game

Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 53632   Accepted: 22497

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的棋盘,上面的棋子一面是黑的,一面是白的。规定翻转一个棋子的同时也要翻转它的上、下、左、右的棋子,问给定一个棋盘的棋子状态,至少需要翻转多少个棋子,能使得所有棋子朝上都是白的或黑的。

 

【分析】

1、对于每个格子,它要么反转0次,要么反转1次(当然,它的邻格子也跟着反转),因为它反转偶数次和反转0次的效果是一样的,同理反转奇数次的效果和反转1次的效果是一样的。
2、数据规模小,考虑我们最少不翻任何棋子(初始状态就是全白或全黑),最多只翻16个棋子(再翻就有棋子被翻两次了),因此总的状态数为
C160+C161++C1616=216,直接枚举或搜索即可。
3、考虑要求的是最小值,因此从翻0个开始,搜索翻i个,直到翻16个,中间搜索到即为最小值,翻16个都不满足即输出Impossible。
4、由于棋子都只有黑、白两面,可以用0、1表示,因此可以位压缩成一个数字来进行判断,翻棋子的操作可使用位运算,有两种方法
5、只有16个棋子,直接状态压缩,一个int型变量就能存下这16个0/1了,所以可以直接压缩成一个数字。如i=2,j=2,则与20032(0100 1110 0100 0000)相异或

至此 迭代加深搜索 已经很明确了 

【代码】

#include<cstdio>
#include<iostream>
using namespace std;
const int N=105;
char s[N];int now,state[]={19,39,78,140,305,626,1252,2248,4880,10016,20032,35968,12544,29184,58368,51200};
inline void Init(){
	for(int i=0;i<4;i++){
		scanf("%s",s);
		for(int j=0;j<4;j++){
			now<<=1;
			now|=s[j]=='b';
		}
	}
}
inline bool check(){
	return !now||now==0xffff;
}
inline void flip(int t){
	now^=state[t];
}
bool find(int n,int i){
	if(!n) return check();
	for(;i<16;i++){
		flip(i);
		if(find(n-1,i+1)) return 1;
		flip(i);
	}
	return 0;
}
inline void Solve(){
	for(int i=0;i<16;i++){
		if(find(i,0)){
			printf("%d\n",i);return ;
		}
	}
	puts("Impossible");
}
int main(){
	Init();
	Solve();
	return 0;
}

 

 

 

posted @ 2019-02-12 21:35  神犇(shenben)  阅读(141)  评论(0编辑  收藏  举报