zoukankan      html  css  js  c++  java
  • [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;
              }
            }
          }
      }   
    }
    满堂花醉三千客,一剑霜寒十四州
  • 相关阅读:
    Docker部署
    编写一个脚本用户进入容器
    Shell脚本写的《俄罗斯方块》
    Linux磁盘分区(9)
    Linux任务调度(8)
    Linux权限管理(7)
    Linux组管理(6)
    Linux实用指令(5)
    C#中 char、byte、string
    编码转换
  • 原文地址:https://www.cnblogs.com/phemiku/p/11406293.html
Copyright © 2011-2022 走看看