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

 

 

 

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

 

 

//广搜,创新点在传送带,struct存下两端就可以。水果

#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;
          }
        }
      }
  }   
}

 

 

【改编】

 

//只需要把两个传送点都入队就可以了

//c
#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;
        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;
          q[++tail].x = dx;
          q[tail].y = dy;
          q[tail].t = q[head].t + 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;
            vis[dx][dy] = 1;
            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;
          }
        }
      }
  }   
}

 

posted @ 2019-08-24 22:07  QUEKI嶺冬  阅读(242)  评论(0编辑  收藏  举报
/*! Color themes for Google Code Prettify | MIT License | github.com/jmblog/color-themes-for-google-code-prettify */ .pln{color:#4d4d4c}ol.linenums{margin-top:0;margin-bottom:0;color:#8e908c}li.L0,li.L1,li.L2,li.L3,li.L4,li.L5,li.L6,li.L7,li.L8,li.L9{padding-left:1em;background-color:#fff;list-style-type:decimal!important;}@media screen{.str{color:#718c00}.kwd{color:#8959a8}.com{color:#8e908c}.typ{color:#4271ae}.lit{color:#f5871f}.pun{color:#4d4d4c}.opn{color:#4d4d4c}.clo{color:#4d4d4c}.tag{color:#c82829}.atn{color:#f5871f}.atv{color:#3e999f}.dec{color:#f5871f}.var{color:#c82829}.fun{color:#4271ae}} /*下面是我设置背景色,字体大小和字体*/ .cnblogs-markdown code{ background:#fff!important; } .cnblogs_code,.cnblogs_code span,.cnblogs-markdown .hljs{ font-size:16px!important; } .syntaxhighlighter a, .syntaxhighlighter div, .syntaxhighlighter code, .syntaxhighlighter table, .syntaxhighlighter table td, .syntaxhighlighter table tr, .syntaxhighlighter table tbody, .syntaxhighlighter table thead, .syntaxhighlighter table caption, .syntaxhighlighter textarea { font-size: 16px!important; } .cnblogs_code, .cnblogs_code span, .cnblogs-markdown .hljs{ font-family:consolas, "Source Code Pro", monaco, monospace !important; } //以上是代码高亮 /* 文字特效 */