POJ 1324 Holedox Moving 状态 bfs

http://poj.org/problem?id=1324

题意:省略;

这个题纠结一天多了,算是了解什么叫状态吧  思路 代码都是参考别人的写的,没有加任何优化   

以后了解多了再优化吧

代码:

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m,L,dir[4][2]={-1,0,0,-1,0,1,1,0};
bool map[21][21],vs[21][21][1<<14];
struct State
{
    int x[8],y[8],step;
};
State t1,t2,qu[20*20*(1<<14)];

int bfs()
{
    int head=0,tail=0;
    qu[0]=t1;

    int x0,y0,x1,y1,x2,y2;
    int i,j,k,b,s;
    while(head<=tail)
    {
        t1=qu[head++];
        if(t1.x[0]==1&&t1.y[0]==1)return t1.step;

        for(j=0;j<4;j++)  //开始写的K,WA了一下午,变量都弄混了,晕死
        {
            x0=x1=t1.x[0]+dir[j][0];
            y0=y1=t1.y[0]+dir[j][1];

            for(i=0;i<L;i++)
                if(x0==t1.x[i]&&y0==t1.y[i])break;

            if( x0<1||x0>n||y0<1||y0>m||map[x0][y0]||i!=L)
               continue;

            t2.x[0]=x1,t2.y[0]=y1;
            s=0;
            for(k=0;k<L-1;k++)
            {
                x2=t2.x[k+1]=t1.x[k];
                y2=t2.y[k+1]=t1.y[k];

                if(y1==y2)
                {
                    if(x1>x2)b=0;
                    else b=2;
                }
                else
                {
                    if(y1>y2)b=1;
                    else b=3;
                }
                s+=b*(1<<(2*k));//!!!
                x1=x2;
                y1=y2;
            }
            if(!vs[x0][y0][s])
            {
                vs[x0][y0][s]=1;
                t2.step=t1.step+1;
                qu[++tail]=t2;
            }
        }
    }
    return -1;
}

int main()
{
   int num,i,j,CASE=0;
   while(~scanf("%d%d%d",&n,&m,&L))
   {
       if(n==0&&m==0&&L==0)break;
       CASE++;
       for(i=0;i<L;i++)
       {
           scanf("%d%d",&t1.x[i],&t1.y[i]);
       }
       t1.step=0;

       memset(vs,0,sizeof(vs));
       memset(map,0,sizeof(map));
       scanf("%d",&num);
       while(num--)
       {
         scanf("%d%d",&i,&j);
         map[i][j]=1;
       }
      int ans=bfs();
      printf("Case %d: %d\n",CASE,ans);
   }
   return 0;
}

  

posted @ 2012-03-02 18:53  快乐.  阅读(202)  评论(0编辑  收藏  举报