POJ1753
Description
- 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
Output
Sample Input
bwwb bbwb bwwb bwww
Sample Output
4
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
bool vis[65540];
int p[65540],v[65540],ope[16],l,r;
int bfs(int ans){
if(ans==0||ans==(1<<16)-1)
return 0;
memset(vis,0,sizeof(vis));
l=r=0;
vis[ans]=1,p[r++]=ans,v[0]=0;
while(l<r){
for(int i=0;i<16;i++){
int num=p[l]^ope[i];
if(vis[num])
continue;
if(num==0||num==(1<<16)-1)
return v[l]+1;
v[r]=v[l]+1;
p[r++]=num;
vis[num]=1;
}
l++;
}
return -1;
}
int main()
{
char s[4][5];
for(int i=0;i<4;i++)
for(int j=0;j<4;j++){
int num=4*i+j;
ope[num]=1<<num;
if(i-1>=0)
ope[num]+=1<<(num-4);
if(j-1>=0)
ope[num]+=1<<(num-1);
if(j+1<4)
ope[num]+=1<<(num+1);
if(i+1<4)
ope[num]+=1<<(num+4);
}
scanf("%s%s%s%s",s[0],s[1],s[2],s[3]);
int ans=0;
for(int i=3;i>=0;i--)
for(int j=3;j>=0;j--){
ans<<=1;
if(s[i][j]=='b')
ans++;
}
int a=bfs(ans);
if(a==-1)
printf("Impossible\n");
else
printf("%d\n",a);
return 0;
}
POJ2965
Description
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.
There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.
The task is to determine the minimum number of handle switching necessary to open the refrigerator.
Input
The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.
Output
The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.
Sample Input
-+-- ---- ---- -+--
Sample Output
6 1 1 1 3 1 4 4 1 4 3 4 4
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
bool vis[65540];
struct op{
int a[16];
}h[65540];//操作过程
int p[65540],v[65540],ope[16],l,r;//节点 步数 操作
int bfs(int ans){
if(!ans) return 0;
l=r=0;
for(int i=0;i<16;i++)
h[0].a[i]=0;
vis[ans]=1;
v[r]=0;
p[r++]=ans;
while(l<r){
for(int i=0;i<16;i++){
int num=ope[i]^p[l];
if(!vis[num]){
vis[num]=1;
v[r]=v[l]+1;
h[r]=h[l];
h[r].a[v[r]]=i;
p[r++]=num;
if(!num)
return r-1;
}
}
l++;
}
return -1;
}
int main()
{
char s[4][5];
int x[4],y[4];
x[0]=(1<<4)-1;
y[0]=1+(1<<4)+(1<<8)+(1<<12);
for(int i=1;i<4;i++)
x[i]=x[i-1]<<4,y[i]=y[i-1]<<1;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
ope[4*i+j]=x[i]|y[j];
while(scanf("%s%s%s%s",s[0],s[1],s[2],s[3])!=EOF){
int ans=0;
for(int i=3;i>=0;i--)
for(int j=3;j>=0;j--){
ans<<=1;
if(s[i][j]=='+')
ans++;
}
int n=bfs(ans);
printf("%d\n",v[n]);
for(int i=1;i<=v[n];i++)
{
int mid=h[n].a[i];
printf("%d %d\n",mid/4+1,mid%4+1);
}
}
return 0;
}
bfs,第i个状态得来的操作步骤存储在h[i]的op数组里。(最多不会超过16个操作步骤)