poj3083Children of the Candy Corn(dfs+bfs)

http://poj.org/problem?id=3083纠结了好久 被它的转向搞晕 了

WA两次 加一else AC。。

设置四个方向 初始化方向自己算出来 初始化在数组中 dfs+bfs

左  就是当前方向向左 左走不动就逆时针走下一个方向  依次类推 右同样的方式

View Code
  1 #include <iostream>
  2  #include<cstdio>
  3  #include<cstring>
  4  using namespace std;
  5  char s[50][50],so[2];
  6  int pr[7],dis[4][2]={-1,0,0,-1,1,0,0,1},vis[50][50],n,m,flag;
  7  int dir[2][4][4] = {{{1, 0, 3, 2}, {2, 1, 0, 3}, {3, 2, 1, 0}, {0, 3, 2, 1}},{{3, 0, 1, 2}, {0, 1, 2, 3}, {1, 2, 3, 0}, {2, 3, 0, 1}}};
  8  struct node
  9  {
 10      int x,y,num;
 11  }q[100010];
 12  int judge(int x,int y)
 13  {
 14      if(x<1||x>n||y<1||y>m)
 15      return 0;
 16      if(s[x][y]=='#')
 17      return 0;
 18      return 1;
 19  }
 20  void bfs()
 21  {
 22      int i,j,p = 0, d = 1;
 23      memset(vis,0,sizeof(vis));
 24      q[d].x = so[0];
 25      q[d].y = so[1];
 26      q[d].num = 1;
 27      vis[so[0]][so[1]] = 1;
 28      while(p!=d)
 29      {
 30          p++;
 31          int tx = q[p].x;
 32          int ty = q[p].y;
 33          int tnum = q[p].num;
 34          if(s[tx][ty]=='E')
 35          {
 36              pr[5] = tnum;
 37              break;
 38          }
 39          for(i = 0 ; i < 4 ; i++)
 40          {
 41              int nx = tx+dis[i][0];
 42              int ny = ty+dis[i][1];
 43              if(!vis[nx][ny]&&judge(nx,ny))
 44              {
 45                  vis[nx][ny] = 1;
 46                  d++;
 47                  q[d].x = nx;
 48                  q[d].y = ny;
 49                  q[d].num = tnum+1;
 50              }
 51          }
 52      }
 53  }
 54  void dfs(int x,int y,int d,int f)
 55  {
 56      pr[f]++;
 57      if(s[x][y]=='E')
 58          return ;
 59      int i,j,td,tx,ty;
 60      td = dir[f][d][0];
 61      tx = x+dis[td][0];
 62      ty = y+dis[td][1];
 63      if(judge(tx,ty))
 64            dfs(tx,ty,td,f);
 65      else
 66      {
 67          td = dir[f][d][1];
 68          tx = x+dis[td][0];
 69          ty = y+dis[td][1];
 70          if(judge(tx,ty))
 71          dfs(tx,ty,td,f);
 72          else
 73          {
 74              td = dir[f][d][2];
 75              tx = x+dis[td][0];
 76              ty = y+dis[td][1];
 77              if(judge(tx,ty))
 78              dfs(tx,ty,td,f);
 79              else
 80              {
 81                  td = dir[f][d][3];
 82                  tx = x+dis[td][0];
 83                  ty = y+dis[td][1];
 84                  if(judge(tx,ty))
 85                  dfs(tx,ty,td,f);
 86              }
 87          }
 88      }
 89  }
 90  int main()
 91  {
 92      int i,j,k,t,in;
 93      cin>>t;
 94      while(t--)
 95      {
 96          cin>>m>>n;
 97          pr[0] = 0;
 98          pr[1] = 0;
 99          for(i = 1 ; i <= n ; i++)
100          {
101              getchar();
102              for(j = 1 ; j <= m ; j++)
103              {
104                  cin>>s[i][j];
105                  if(s[i][j]=='S')
106                  {
107                      so[0] = i;
108                      so[1] = j;
109                      if(so[0]==n)
110                      in = 0;
111                      if(so[0]==1)
112                      in = 2;
113                      if(so[1]==m)
114                      in = 1;
115                      if(so[1]==1)
116                      in = 3;
117                  }
118              }
119          }
120          bfs();
121          for(i = 0 ; i < 2 ; i++)
122          {
123              memset(vis,0,sizeof(vis));
124              vis[so[0]][so[1]] = 1;
125              dfs(so[0],so[1],in,i);
126          }
127          cout<<pr[0]<<" "<<pr[1]<<" "<<pr[5]<<endl;
128      }
129      return 0;
130  }
原文地址:https://www.cnblogs.com/shangyu/p/2874121.html