bnu 4358 左手定则 (搜索)

http://www.bnuoj.com/bnuoj/problem_show.php?pid=4358

【题意】:给定起始位置和方向和目的地,按照左转、前进、右转、后退的优先级递减,也就是说能左转就左转,不能则继续前进,。。。,能走到T的位置输出YES,不能则输出NO。。。

【题解】:

  首先我们知道YES肯定就是走到了,若出现NO的情况,肯定是出现了死循环,这题主要考标记,什么时候表示已经进入了死循环呢?每个位置有一个方向,我们将这个位置第一次出现方向的时候标记下来,注意是第一次的方向噢,第二次经过的时候判断,是否方向相同,相同则表示出现了跟之前一样的状态,也就是说出现了循环。。。

【code】:

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <string.h>
  4 #include <algorithm>
  5 
  6 using namespace std;
  7 
  8 char map[110][110];
  9 int cx[]={-1,0,1,0};
 10 int cy[]={0,1,0,-1};
 11 int dr[110][110];
 12 int n,m;
 13 
 14 int isCheck(int x,int y)
 15 {
 16     if(x>=0&&x<n&&y>=0&&y<m)    return 1;
 17     return 0;
 18 }
 19 
 20 int search(int sx,int sy,int d)
 21 {
 22     while(1)
 23     {
 24         if(map[sx][sy]=='T')    return 1;
 25        // cout<<sx<<" "<<sy<<endl;
 26         if(dr[sx][sy]==-1)
 27         {
 28             dr[sx][sy]=d;
 29         }
 30         else if(dr[sx][sy]==d)
 31         {
 32             return 0;
 33         }
 34         int tx,ty;
 35         //如果左边是空位
 36         int d1 = (d+3)%4;
 37         tx = sx+cx[d1];
 38         ty = sy+cy[d1];
 39         if(isCheck(tx,ty)&&map[tx][ty]!='#')//可以左转
 40         {
 41             sx = tx;
 42             sy = ty;
 43             d = d1;
 44         }
 45         else if(isCheck(tx,ty)&&map[tx][ty]=='#') //不可以左转
 46         {
 47           //   int d2 = (d+1)%4;
 48              int tx2 = sx+cx[d];
 49              int ty2 = sy+cy[d];
 50              if(isCheck(tx2,ty2))
 51              {
 52                  if(map[tx2][ty2]=='#')//前方不能走
 53                  {
 54                      int d2 = (d+1)%4;
 55                      int tx3 = sx+cx[d2];
 56                      int ty3 = sy+cy[d2];
 57                      if(isCheck(tx3,ty3))
 58                      {
 59                          if(map[tx3][ty3]!='#')//右边可以走
 60                          {
 61                               sx = tx3;
 62                               sy = ty3;
 63                               d = d2;
 64                          }
 65                          else //右边不可以走,向后走
 66                          {
 67                              int d3 = (d+2)%4;
 68                              sx = sx+cx[d3];
 69                              sy = sy+cy[d3];
 70                              d=d3;
 71                          }
 72                      }
 73                  }
 74                  else//前方能走
 75                  {
 76                        sx = tx2;
 77                        sy = ty2;
 78                  }
 79              }
 80         }
 81     }
 82 }
 83 
 84 int main()
 85 {
 86     while(~scanf("%d%d",&n,&m))
 87     {
 88         memset(dr,-1,sizeof(dr));
 89         int i,j,flag=0;
 90         for(i=0;i<n;i++)    scanf("%s",map[i]);
 91         char dir[5];
 92         scanf("%s",dir);
 93         int d;
 94         if(dir[0]=='N') d=0;
 95         else if(dir[0]=='E')    d=1;
 96         else if(dir[0]=='S')    d=2;
 97         else if(dir[0]=='W')    d=3;
 98         for(i=0;i<n;i++)
 99         {
100             for(j=0;j<m;j++)
101             {
102                 if(map[i][j]=='S')
103                 {
104                     flag = search(i,j,d);
105                     break;
106                 }
107             }
108         }
109         if(flag)    puts("YES");
110         else puts("NO");
111     }
112     return 0;
113 }
原文地址:https://www.cnblogs.com/crazyapple/p/3327127.html