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;
              }
            }
          }
      }   
    }
    满堂花醉三千客,一剑霜寒十四州
  • 相关阅读:
    CombineTextInputFormat 案例
    FileInputFormat 和 CombineTextInputFormat 切片机制
    MapTask 并行度决定机制
    微信小程序tab切换时echarts不显示问题 及使用 小程序中使用echarts图表显示模糊
    微信小程序添加卡券到微信卡包,使用wx.addCard()方法传参及整体流程
    React 项目结构和组件命名规范
    微信小程序实现分享至朋友圈的功能来啦
    MongoDB简介
    NoSQL 简介
    Redis 主从复制、哨兵和集群原理与区别
  • 原文地址:https://www.cnblogs.com/phemiku/p/11406293.html
Copyright © 2011-2022 走看看