您的位置:首页 > 其它

hdu1401(双广搜)

2014-01-30 20:02 134 查看
已知开始状态和目标状态,并且具有可逆性,采用双广搜;

用八维hash数组记录四个棋子的位置(要对四个棋子的坐标排序,迫使棋子一一对应)。

下面的代码是参考大神写的,自己刚学双搜。

#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
#include<math.h>

#define eps 1e-9
#define P system("pause")
using namespace std;
struct point {
   int x,y;     
   friend bool operator<(point a,point b)
   {
       if(a.x!=b.x) return a.x<b.x;
       return a.y<b.y;       
   }  
};
struct node
{
     point s[4];   //记录四个棋子的位置 
     int step;     
};
char hash[8][8][8][8][8][8][8][8];        //如果是1则q1中的节点经过了,2则q2中的节点经过了,0表示为经过 
queue<node> q1,q2;
int d[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

void setflag(node a,char k)
{
       hash[a.s[0].x][a.s[0].y][a.s[1].x][a.s[1].y][a.s[2].x][a.s[2].y][a.s[3].x][a.s[3].y]=k;   
}

char getflag(node a)
{
     return  hash[a.s[0].x][a.s[0].y][a.s[1].x][a.s[1].y][a.s[2].x][a.s[2].y][a.s[3].x][a.s[3].y];    
}

bool judge(node &t,int i,int j,int m)                 //这个写的很巧妙,自己也写了个,总是wa,最后还是跟着答案写了
{
    if( m==1 )
   {
       if( t.step >=4 )//最多移动4步 
           return false;
       t.step ++;    
   }   
    
   t.s[i].x += d[j][0];
   t.s[i].y += d[j][1];
    if( t.s[i].x >= 0 && t.s[i].x < 8 && t.s[i].y >=0 && t.s[i].y<8 )
    {     int k;
          for(k=0; k<4 ; k++)
          {                 
             if(i!=k)
             {
                  if( t.s[i].x==t.s[k].x&&t.s[i].y==t.s[k].y )
                  if(m == 1) return judge(t, i , j , 2);
                  else return false;         
             }                      
          }
         if( k>=4 )
       { 
         sort( t.s , t.s + 4 );
         return true;
       } 
    } 
   return false;
}

bool dbfs()
{
       int i,j;
       char k;    
       node u;
       while(!q1.empty()||!q2.empty())
       {
             if(!q1.empty())
             {                            
                   for(i=0;i<4;i++)
                      for(j=0;j<4;j++)
                      {
                           u=q1.front();           
                           if(judge(u,i,j,1)){
                               k=getflag(u);
                               if(k==2) return 1;
                                else if(k==0){
                                     sort(u.s,u.s+4);        
                                     setflag(u,1);
                                     q1.push(u);
                                }
                           }            
                      }
                  q1.pop();                  
             }
             if(!q2.empty())
             {
                  for(i=0;i<4;i++)
                      for(j=0;j<4;j++)
                      {
                            u=q2.front();
                            if(judge(u,i,j,1)){   //判断可行,主要求u 
                                 k=getflag(u);
                                 if(k==1) return 1;
                                 else if(k==0) {
                                     sort(u.s,u.s+4);
                                     setflag(u,2);
                                     q2.push(u);    
                                 }                     
                            }                                 
                      }                       
                  q2.pop();
             }
       }     
       return 0;
}

int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);cc
    int i,j;
    node A,B;
    while(scanf("%d%d",&i,&j)!=EOF)
    {
          memset(hash,0,sizeof(hash));                         
          while(!q1.empty()) q1.pop(); 
          while(!q2.empty()) q2.pop();                        
          i--;j--;
          A.s[0].x=i; A.s[0].y=j;
          for(i=1;i<4;i++){
              scanf("%d%d",&A.s[i].x,&A.s[i].y);
              A.s[i].x--;A.s[i].y--;
          }
          sort(A.s,A.s+4);
          setflag(A,1);
          for(i=0;i<4;i++){
              scanf("%d%d",&B.s[i].x,&B.s[i].y);
              B.s[i].x--;B.s[i].y--;
          } 
          sort(B.s,B.s+4);
          setflag(B,2);
          A.step=B.step=0;
          q1.push(A);
          q2.push(B);
          if(dbfs())  printf("YES\n");
          else printf("NO\n"); 

                                   
    }                         

  //  P;                               
    return 0;    
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: