[uva11OPEN]玉米田迷宫Corn Maze(广搜bfs)

时间:2019-08-24
本文章向大家介绍[uva11OPEN]玉米田迷宫Corn Maze(广搜bfs),主要包括[uva11OPEN]玉米田迷宫Corn Maze(广搜bfs)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

第一次做MLE了…第二次WA了5个点,其实就是一个判断错了…QAQ总的来说…是个水题/板子题(逃

#include<bits/stdc++.h>
using namespace std;
#define For(i,l,r) for(register int i=l; i<r; i++)
int n,m; 
int d[4][2] = { 0, 1, 0, -1, 1, 0, -1, 0 };
bool bj,vis[301][301];
char ch[301][301];
struct cs{
    int x1,y1,x2,y2;
    bool p;
}chs[35];

struct node {
    int x, y, t;
} q[90001];

inline bool check(int x, int y)
{
  return x >= 0 && y >= 0 && x < n && y < m && vis[x][y] == 0;
} 

int main()
{
  int sx,sy,ex,ey;
  string s;
  scanf("%d%d",&n,&m);
  For(i,0,n){
      cin>>s; 
   For(j,0,m){
       if(s[j] == '=')ex = i, ey = j;
       else if(s[j] == '#')vis[i][j]=1;
       else if(s[j] == '@')sx = i, sy = j;
       else if(s[j] >= 'A' && s[j] <= 'Z'){
           int c = s[j]-'A';
        ch[i][j] = s[j];
           if (chs[c].p == 1)chs[c].x2 = i, chs[c].y2 = j;  //就是这儿的判断!555查了一晚上_(:3/)__
        else chs[c].x1 = i, chs[c].y1 = j, chs[c].p = 1;
       }
   }    
  }
  int head = 0, tail = 1;
  q[tail].x = sx, q[tail].y = sy;
  vis[sx][sy] = 1;
  while(head < tail){
      head++;
      int xn = q[head].x, yn = q[head].y;
      For(i,0,4){
          int dx = d[i][0] + xn;
        int dy = d[i][1] + yn;
        if(check(dx,dy)){
          vis[dx][dy] = 1;
          if(ch[dx][dy] >= 'A' && ch[dx][dy] <= 'Z'){
              int c = ch[dx][dy] - 'A';
              if (dx == chs[c].x1 && dy == chs[c].y1)
                dx = chs[c].x2, dy = chs[c].y2;
            else
                dx = chs[c].x1, dy = chs[c].y1;
          }
          q[++tail].x = dx;
          q[tail].y = dy;
          q[tail].t = q[head].t + 1;
          if(dx == ex && dy == ey){
              cout << q[tail].t;
              return 0;
          }
        }
      }
  }   
}

原文地址:https://www.cnblogs.com/phemiku/p/11406293.html