http://acm.pku.edu.cn/JudgeOnline/problem?id=1753
Flip Game
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 2565 |
|
Accepted: 1016 |
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
Source
Northeastern Europe 2000
题目的意思很明白,就是可以同时改变最多5个的颜色(既,自身,上,下,左,右)一开始赤裸裸的BFS,很显然是MLE..
这里不讨论暴力解的情况了....-______________________-暴力是好东西可是偶不会,苍天也.......用暴力的大牛们勿鄙
考虑到状态的特殊性,一个节点要么白色要么黑色,很容易想到用2进制表示,16个节点,最多用16个表示就OK了.
选取的时候自然是去int(32位),因为需要移位
第一次写也不知道这是不是传说中的hash.................不过居然错在了没把自身的状态改变-____________-囧一下~
首先把白的状态看成0
(例如,图上的状态在int中是0000 0000 0000 0000 1010 0000 1101 1001)
1.全白/全黑就是0/65535,很容易理解
2.这题的BFS应该没什么问题,问题集中在了状态改变上,位运算解决..
具体见代码
- #include <iostream>
- #include <queue>
- using namespace std;
- struct sta
- {
- int state;
- int step;
- }p,temp,s;
- void move(sta *a,int id)
- {
- a->state<<=id;
- a->state^=32768;
- a->state>>=id;
- if(id<=11)
- {
- a->state<<=(id+4);
- a->state^=32768;
- a->state>>=(id+4);
- }
- if(id>=4)
- {
- a->state<<=(id-4);
- a->state^=32768;
- a->state>>=(id-4);
- }
- if(id==0||id==4||id==8||id==12)
- {
- a->state<<=(1+id);
- a->state^=32768;
- a->state>>=(id+1);
- }
- else
- if(id==3||id==7||id==11||id==15)
- {
- a->state<<=(id-1);
- a->state^=32768;
- a->state>>=(id-1);
- }
- else
- {
- a->state<<=(id+1);
- a->state^=32768;
- a->state>>=2;
- a->state^=32768;
- a->state>>=(id-1);
- }
- }
- void pri(int num)
- {
- int i,j=0,l=0,n[16]={0};
- while(num)
- n[l++]=num%2,num/=2;
- for(i=15;i>=0;i--)
- {
- cout<<n[i];
- j++;
- if(j==4)
- {
- cout<<endl;
- j=0;
- }
- }
- cout<<endl;
- }
- queue<sta> q;
- bool hash[65537];
- int main()
- {
- int w,i,j;
- char map[4][5];
- bool flag;
- while(cin>>map[0])
- {
- memset(hash,false,sizeof(hash));
- flag=false;
- getchar();
- for(i=1;i<4;i++)
- cin>>map[i],getchar();
- s.step=0;s.state=0;
- for(i=0;i<4;i++)
- for(j=0;j<4;j++)
- if(map[i][j]=='b')
- s.state++,s.state<<=1;
- else
- s.state<<=1;
- s.state>>=1;
- if(s.state==0||s.state==65535)
- {
- cout<<0<<endl;
- continue;
- }
- q.push(s);
- hash[s.state]=true;
- w=0;
- while(!q.empty())
- {
- while(q.front().step==w)
- {
- p=q.front();
- p.step++;
- for(i=0;i<=15;i++)
- {
- temp=p;
- move(&temp,i);
- if(temp.state==0||temp.state==65535)
- {
-
- flag=true;
- break;
- }
- if(!hash[temp.state])
- {
- hash[temp.state]=true;
- q.push(temp);
- }
- }
- q.pop();
- if(flag) break;
- }
- w++;
- if(flag) break;
- }
- if(flag)
- cout<<temp.step<<endl;
- else
- cout<<"Impossible"<<endl;
- }
- return 0;
- }