poj 3185 The Water Bowls (bfs 加未压缩)

注意 标记hash[],不然会错(数据状态好像很多 ,所以要标记)
http://poj.org/problem?id=3185
#include<stdio.h>
#include<string.h>
#define N 1000000
struct node
{
    int num;
    int step;
}p[N];

bool hash[1048576];
int d[20]={0xC0000,0xE0000,0x70000,0x38000,0x1C000,0xE000,0x7000,0x3800,0x1C00,
             0xE00,0x700,0x380,0x1C0,0xE0,0x70,0x38,0x1C,0xE,0x7,0x3};

int bfs(int num)
{
    memset(hash,0,sizeof(hash));
    int head=0,tail=0,i;
    p[head].num=num;
    p[head].step=0;
    tail++;
    while(head<tail)
    {
        int k=p[head].num;
        if(k==0)return p[head].step;
        for(i=0;i<20;i++)
        {
            int  x=k^d[i];
            
            if(x==0)return p[head].step+1;
            else
            {
                if(hash[x])continue;
                hash[x]=1;
                p[tail].num=x;
                p[tail].step=p[head].step+1;
                tail++;
                if(tail==N-1)tail=0;
            }
        }
        head++;

    }
}
int main()
{
    int a,f,i,j;
      int num=0;

     
    for(i=1;i<=20;i++)
    {
        scanf("%d",&a);
        num<<=1;
        num+=a;
    }
    int ans=bfs(num);
    printf("%d\n",ans);



}

  

原文地址:https://www.cnblogs.com/acSzz/p/2438080.html