zoukankan      html  css  js  c++  java
  • poj 3322 不错的搜索题,想通了就很简单的。

    每个格子有3种状态,放置这1*2的长方体的3中方法,然后广搜就OK了,因为每移动一格都是一步,所以广搜就是最短路了。

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<cstdlib>
      5 #include<cmath>
      6 #include<algorithm>
      7 #include<queue>
      8 using std::queue;
      9 struct node
     10 {
     11        int x1,x2,y1,y2,s;
     12        int step;
     13 }cur,nt;
     14 int const N = 510;
     15 int n,m,ex,ey;
     16 char gra[N][N];
     17 int state[N][N][3];
     18 int dirx[4]={-1,1,0,0};
     19 int diry[4]={0,0,-1,1};
     20 bool Ok(int x,int y)
     21 {
     22      return gra[x][y]=='#'||gra[x][y]=='E';
     23 }
     24 bool judge(int x1,int x2,int y1,int y2)
     25 {
     26      return gra[x1][y1]=='#'||gra[x1][y2]=='#'||gra[x2][y1]=='#'||gra[x2][y2]=='#';
     27 }
     28 void Swap(int &a,int &b)
     29 {
     30     int t=a;
     31     a=b;
     32     b=t;
     33 }
     34 int bfs(node tmp)
     35 {
     36     queue<node> q;
     37     while(!q.empty())q.pop();
     38     cur=tmp;
     39     cur.step=0;
     40     if(cur.s==0)state[cur.x1][cur.y1][0]=0;
     41     if(cur.s==1)state[cur.x1][cur.y1][1]=0,state[cur.x2][cur.y1][1]=0;
     42     if(cur.s==2)state[cur.x1][cur.y1][2]=0,state[cur.x1][cur.y2][2]=0;
     43     q.push(cur);
     44     while(!q.empty())
     45     {
     46           cur=q.front();
     47           q.pop();
     48           if(cur.s==0)
     49           {
     50              for(int i=0;i<4;i++)
     51              {
     52                  nt.x1=cur.x1+dirx[i];
     53                  nt.x2=cur.x1+2*dirx[i];
     54                  nt.y1=cur.y1+diry[i];
     55                  nt.y2=cur.y1+2*diry[i];
     56                  nt.step=cur.step+1;
     57                  if(judge(nt.x1,nt.x2,nt.y1,nt.y2))continue;
     58                  if(nt.x1>nt.x2)Swap(nt.x1,nt.x2);
     59                  if(nt.y1>nt.y2)Swap(nt.y1,nt.y2);
     60                  if(i<2)
     61                  {
     62                     nt.s=1;
     63                     if(state[nt.x1][nt.y1][nt.s]==-1)
     64                     {
     65                        state[nt.x1][nt.y1][nt.s]=nt.step;
     66                        q.push(nt);
     67                     }
     68                  }
     69                  else
     70                  {
     71                     nt.s=2;
     72                     if(state[nt.x1][nt.y1][nt.s]==-1)
     73                     {
     74                        state[nt.x1][nt.y1][nt.s]=nt.step;
     75                        q.push(nt);
     76                     }
     77                  }
     78              }
     79           }
     80           else
     81             if(cur.s==1)
     82             {
     83                for(int i=0;i<2;i++)
     84                {
     85                    if(i)
     86                    {
     87                       nt.x1=nt.x2=cur.x1-1;
     88                       nt.y1=nt.y2=cur.y1;
     89                    }
     90                    else
     91                    {
     92                       nt.x1=nt.x2=cur.x2+1;
     93                       nt.y1=nt.y2=cur.y1;
     94                    }
     95                    if(Ok(nt.x1,nt.y1))continue;
     96                    nt.step=cur.step+1;
     97                    nt.s=0;
     98                    if(state[nt.x1][nt.y1][nt.s]==-1)
     99                    {
    100                       state[nt.x1][nt.y1][nt.s]=nt.step;
    101                       if(nt.x1==ex&&nt.y1==ey)return nt.step;
    102                       q.push(nt);
    103                    }
    104                }
    105                for(int i=0;i<2;i++)
    106                {
    107                    nt.x1=cur.x1,nt.x2=cur.x2;
    108                    if(i)nt.y1=cur.y1-1,nt.y2=cur.y1-1;
    109                    else nt.y1=cur.y1+1,nt.y2=cur.y1+1;
    110                    if(judge(nt.x1,nt.x2,nt.y1,nt.y2))continue;
    111                    nt.s=1;
    112                    nt.step=cur.step+1;
    113                    if(state[nt.x1][nt.y1][nt.s]==-1)
    114                    {
    115                        state[nt.x1][nt.y1][nt.s]=nt.step;
    116                        q.push(nt);
    117                    }
    118                }
    119             }
    120             else
    121               if(cur.s==2)
    122               {
    123                  for(int i=0;i<2;i++)
    124                  {
    125                      if(i)
    126                      {
    127                         nt.y1=nt.y2=cur.y1-1;
    128                         nt.x1=nt.x2=cur.x1;
    129                      }
    130                      else
    131                      {
    132                         nt.y1=nt.y2=cur.y2+1;
    133                         nt.x1=nt.x2=cur.x1;
    134                      }
    135                      if(Ok(nt.x1,nt.y1))continue;
    136                      nt.step=cur.step+1;
    137                      nt.s=0;
    138                      if(state[nt.x1][nt.y1][nt.s]==-1)
    139                      {
    140                         state[nt.x1][nt.y1][nt.s]=nt.step;
    141                         if(nt.x1==ex&&nt.y1==ey)return nt.step;
    142                         q.push(nt);
    143                      }
    144                  }
    145                  for(int i=0;i<2;i++)
    146                  {
    147                      nt.y1=cur.y1,nt.y2=cur.y2;
    148                      if(i)nt.x1=cur.x1-1,nt.x2=cur.x1-1;
    149                      else nt.x1=cur.x1+1,nt.x2=cur.x1+1;
    150                      if(judge(nt.x1,nt.x2,nt.y1,nt.y2))continue;
    151                      nt.s=2;
    152                      nt.step=cur.step+1;
    153                      if(state[nt.x1][nt.y1][nt.s]==-1)
    154                      {
    155                          state[nt.x1][nt.y1][nt.s]=nt.step;
    156                          q.push(nt);
    157                      }
    158                  }
    159               }
    160     }
    161     return -1;
    162 }
    163 bool dfs(int x,int y)
    164 {
    165      return gra[x+1][y]!='#'||gra[x-1][y]!='#'||gra[x][y+1]!='#'||gra[x][y-1]!='#'||gra[x+1][y+1]!='#'||gra[x+1][y-1]!='#'||gra[x-1][y+1]!='#'||gra[x-1][y-1]!='#';
    166 }
    167 int main()
    168 {
    169     int x1,y1,x2,y2;
    170     while(~scanf("%d %d",&n,&m)&&(n+m)!=0)
    171     {
    172           x1=x2=-1,y1=y2=-1;
    173           for(int i=0;i<n;i++)
    174           {
    175               scanf("%s",gra[i]);
    176               for(int j=0;j<m;j++)
    177               {
    178                   for(int k=0;k<3;k++)state[i][j][k]=-1;
    179                   if(gra[i][j]=='X'&&x1==-1&&y1==-1)
    180                      x1=i,y1=j;
    181                   else
    182                     if(gra[i][j]=='X'&&x2==-1&&y2==-1)
    183                        x2=i,y2=j;
    184                   if(gra[i][j]=='O')
    185                      ex=i,ey=j;
    186               }
    187           }
    188           int f=dfs(ex,ey);
    189           if(!f)
    190           {
    191              printf("Impossible\n");
    192              continue;
    193           }
    194           node tmp;
    195           if(x2==-1&&y2==-1)tmp.x1=x1,tmp.y1=y1,tmp.s=0;
    196           else
    197           {
    198              if(x1==x2)tmp.s=2;
    199              else tmp.s=1;
    200              tmp.x1=x1,tmp.y1=y1;
    201              tmp.x2=x2,tmp.y2=y2;
    202           }
    203           int ans=bfs(tmp);
    204           if(ans==-1)printf("Impossible\n");
    205           else printf("%d\n",ans);
    206     }
    207     return 0;
    208 }
    View Code
  • 相关阅读:
    hdu-2084 数塔
    hdu-2151 Worm
    hdu-1234开门人和关门人
    hdu-1715大菲波数
    linux应用之vsftp服务的安装及配置(centos)
    linux应用之tomcat的安装及配置(centos)
    linux应用之nginx的安装及配置(centos)
    linux启动过程详解
    linux应用之Mongodb的安装及配置(centos)
    linux应用之apache服务的安装及配置(centos)
  • 原文地址:https://www.cnblogs.com/nuoyan2010/p/3103865.html
Copyright © 2011-2022 走看看