zoukankan      html  css  js  c++  java
  • poj_3984_迷宫问题_(bfs+记录路径)


      这道题明明就很水,结果我做了一个下午,囧。

                              迷宫问题
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
    Submit Status

    Description

    定义一个二维数组: 

    int maze[5][5] = {
    0, 1, 0, 0, 0,
    0, 1, 0, 1, 0,
    0, 0, 0, 0, 0,
    0, 1, 1, 1, 0,
    0, 0, 0, 1, 0,
    };

    它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

    Input

    一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

    Output

    左上角到右下角的最短路径,格式如样例所示。

    Sample Input

    0 1 0 0 0
    0 1 0 1 0
    0 0 0 0 0
    0 1 1 1 0
    0 0 0 1 0

    Sample Output

    (0, 0)
    (1, 0)
    (2, 0)
    (2, 1)
    (2, 2)
    (2, 3)
    (2, 4)
    (3, 4)
    (4, 4)




    要记录路径,其实只需要维护一个数组,记录加入队列的点的前导节点(即是从哪个点到达这个点的),
    然后再从终点反向寻找路径,用一个数组保存起来,
    最后将记录结果的数组反向输出。

    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    const int MAXN=5;
    int maze[MAXN][MAXN];
    int dx[4]={0,0,1,-1};
    int dy[4]={1,-1,0,0};
    struct Point
    {
      int x,y;
    }pre[MAXN][MAXN];
    bool vis[MAXN][MAXN];
    void bfs();
    int main()
    {
      // while(true)      第一次交的时候提示 Output Limit Exceeded,,以前还不知道有这个呢,,,可是没有多输出什么啊,应该是死循环了,试着把这里注释,结果ac了。
      //{
        for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
        scanf("%d",&maze[i][j]);
        memset(vis,false,sizeof(vis));
        bfs();
      // }
      return 0;
    }
    void bfs()
    {
      queue<Point>q;
      Point start;
      start.x=0;
      start.y=0;
      q.push(start);
      vis[0][0]=true;
      while(q.size())
      {
        Point p=q.front();
        q.pop();
        if(p.x==4&&p.y==4)
        {
          break;
        }
        for(int i=0;i<4;i++)
        {
          Point np;
          np.x=p.x+dx[i];
          np.y=p.y+dy[i];
          if(!vis[np.x][np.y]&&np.x>=0&&np.x<5&&np.y>=0&&np.y<5&&maze[np.x][np.y]==0)
          {
            q.push(np);
            pre[np.x][np.y].x=p.x;
            pre[np.x][np.y].y=p.y;
            vis[np.x][np.y]=true;
          }
        }
      }
      Point ans[30];
      int i=4,j=4;
      int tot=0;
      while(true)
      {
        if(i==0&&j==0)
        break;
        ans[tot].x=pre[i][j].x;
        ans[tot++].y=pre[i][j].y;
        int t=i;
        i=pre[t][j].x;
        j=pre[t][j].y;

        /*我刚开始这里是写

          i=pre[i][j].x;

          j=pre[i][j].y;

        明显是我sb了,这里 i 改变后,后面j=pre[i][j].y 的 i 已经不是 i 了

        */
      }
      for(int i=tot-1;i>=0;i--)
      {
        printf("(%d, %d) ",ans[i].x,ans[i].y);   //注意,这里有空格的。
      }
      printf("(4, 4) ");
      return ;
    }













  • 相关阅读:
    opensuse使用zypper安装软件
    补习系列(1)-springboot项目基础搭建课
    补习系列-springboot-使用assembly进行项目打包
    log4j2 使用纪要
    mongos-sharding连接池配置
    maven-代码风格检查工具
    mtools-你可能没用过的mongodb神器
    mongodb分布式集群搭建手记
    mongodb分片扩展架构
    mongodb副本集高可用架构
  • 原文地址:https://www.cnblogs.com/-maybe/p/4242378.html
Copyright © 2011-2022 走看看