hdu 3220好广搜

用位运算来实现广搜,每一条边就是一个方向

View Code
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int steps;
queue<int> Q;
int vis[1<<17];
int dir[32][2]={0,1,0,2,0,8,0,4, 1,3,1,9,1,5, 3,7,3,2,3,11, 11,9,11,15,11,10, 10,2,10,8,10,14,
8,12,8,9, 9,13, 2,6, 4,5,4,6,4,12, 5,13,5,7, 7,6,6,15, 15,13,15,14, 14,6,14,12, 12,13,12,4};
void bfs()
{
int i,j,temp,a,b,A,B;
vis[65280]=1;
Q.push(65280);
steps=1;
while(setps<=3&&!Q.empty())
{
int temp=Q.front();
Q.pop();
for(i=0;i<32;i++)
{
a=temp&(1<<dir[i][0]);
b=temp&(1<<dir[i][1]);
if(a==0&&b!=0||a!=0&&b==0)
{
A=temp^(1<<dir[i][0]);
B=A^(1<<dir[i][1]);
if(vis[B]==-1)
{
Q.push(B);
vis[B]=steps;
}
}
}
steps++;
}
}
int main()
{
int i,j;
int t,num;
int sum;
scanf("%d",&t);
while(t--)
{
for(i=0;i<16;i++)
{
scanf("%d",&num);
if(num==1)
sum+=(1<<i);
else continue;
}
memset(vis,-1,sizeof(vis));
bfs();
if(vis[sum]!=-1)
printf("Case #%d: %d\n",vis[sum]);
else printf("Case #%d: more\n");
}
return 0;
}



你可能感兴趣的:(HDU)