zoukankan      html  css  js  c++  java
  • POJ3984 BFS广搜--入门题

    迷宫问题
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 20816   Accepted: 12193

    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 "iostream"
    #include "queue"
    using namespace std;
    int a[5][5];
    bool visit[5][5];
    int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
    struct Node{
        int x,y;
        int s;///路径长度
        int l[30];///每走一步的方向
    };///通过记录方向来记录路径
    bool judge(int x,int y)
    {
        if(x<0||x>=5||y<0||y>=5)
            return true;
        if(visit[x][y])
            return true;
        if(a[x][y]==1)
            return true;
        return false;
    }
    Node& bfs()
    {
        queue<Node> que;
        Node cur,next;
        cur.x=0;cur.y=0;cur.s=0;
        visit[0][0]=1;
        que.push(cur);
        while(que.size())
        {
            cur=que.front();
            que.pop();
            if(cur.x==4&&cur.y==4)
                return cur;
    
            int i,nx,ny;
            for(i=0;i<4;i++)
            {
                nx=cur.x+dx[i];
                ny=cur.y+dy[i];
                if(judge(nx,ny))
                    continue;
                next=cur;
                next.x=nx;
                next.y=ny;
                next.s=cur.s+1;
                next.l[cur.s]=i;
                visit[nx][ny]=1;
                que.push(next);
            }
        }
        return cur;
    }
    int main()
    {
        for(int i=0;i<5;i++)
        {
            for(int j=0;j<5;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        Node ans=bfs();
        printf("(0, 0)
    ");
        int x=0,y=0;
        for(int i=0;i<ans.s;i++)
        {
            x+=dx[ans.l[i]];
            y+=dy[ans.l[i]];
            printf("(%d, %d)
    ",x,y);
        }
        return 0;
    }
     
  • 相关阅读:
    Spark学习(一)Spark初识
    service mysqld restart mysqld: 未被识别的服务
    Spark学习(二)Spark 版本 WordCount
    java.sql.SQLException: Incorrect string value: '\xE4\xB8\xAD\xE9\x83\xA8' for column 'area' at row 1
    centos 6.8 yum源不可用安装报YumRepo Error: All mirror URLs are not using ftp, http[s] or file
    互联网运维装腔指南
    PHP生成一段时间之间的月份列表
    sql根据分组后排序取第一条数据
    sql 多行拼接 成一行
    js 常用汇总
  • 原文地址:https://www.cnblogs.com/kimsimple/p/6705124.html
Copyright © 2011-2022 走看看