poj 1753 flip(技巧性枚举)

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

解题说明:1,数据比较少,当然可以直接暴力bfs或者dfs

2,可以枚举第一行的的所有的翻牌方式(16种),一旦第一行确定了,那么后面行的翻牌就是确定的,都要为了上一行全是总体的b或者w,而去翻牌。最后一行翻是为了成就了倒数第二行,如果最后一行也是满足情况的,那这种翻牌方式就可以。

void deal(int node,int num,int wt)//node表示第几行,num翻的次数,wt表示翻成b还是w;

这里每一行我用一个二进制f[i]表示牌的状态,注意的就是每次重新搜索,f[i]要回到原来的初始化,代码中f[k]=b[k],temp1,temp2就是在干这样的操作。也可以用一个数表示16位二进制压缩,可是位操作可能稍微麻烦点,但是初始化比较好搞。


ac代码:

#include<cstdio>
#include<string.h>
#include<string> 
#include<algorithm>
#include<iostream>
using namespace std;
int f[5]={0},b[5]={0},mn=0x3f3f3f3f;
int cg[5][2]={{0,1},{0,-1},{1,0},{-1,0},{0,0}};
void flip(int x, int y){
	for(int i=0;i<5;i++){
		int dx=x+cg[i][0];
		int dy=y+cg[i][1];
		if(dx>4||dx<1||dy>4||dy<1)continue;
	    f[dx]^=(1<<(dy-1));
	}
}
void deal(int node,int num,int wt){
	if(node==4){
		if((wt&&f[node]==15)||(!wt&&f[node]==0))mn=min(mn,num);
		return; 
	}
	int temp=f[node];
	for(int i=1;i<=4;i++){
		if((temp%2)^wt){
			flip(node+1,i);
			num++;
		}
		temp>>=1;
	}
	deal(node+1,num,wt);
}
int main(){
     for(int i=1;i<=4;i++){
	 string s;cin>>s;
     	for(int j=0;j<4;j++){
     		if(s[j]=='w'){
     			f[i]|=(1<<j);
     			b[i]|=(1<<j);
			 }
		 }
	 }
	 for(int i=0;i<16;i++){
	      int num=0;
	      for(int k=1;k<=4;k++)f[k]=b[k];
		  for(int j=1;j<=4;j++){
		  	if(i&(1<<(j-1))){
		  		   flip(1,j);
		  		   num++;
			  }
		  }
		  int temp1=f[1];int temp2=f[2]; 
		  deal(1,num,0);
		  for(int k=1;k<=4;k++){
		  	if(k==1)f[k]=temp1;
		  	else if(k==2)f[k]=temp2;
		  	else f[k]=b[k];
		  }
		  deal(1,num,1); 	
	 }
     if(mn==0x3f3f3f3f)printf("Impossible\n");
     else printf("%d\n",mn); 
	return 0;
}
 

猜你喜欢

转载自blog.csdn.net/zxk_hi/article/details/80046667