zoukankan      html  css  js  c++  java
  • 【POJ

    -->迷宫问题

    Descriptions:

    定义一个二维数组: 

    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)

    题目链接

    https://vjudge.net/problem/POJ-3984

    没有用到队列,想了一个染色+dfs的方法,直接就dfs走一遍地图,然后按步数染色,最后按步数从大到小输出即可,头脑简单的我直接就暴力搜了

    AC代码:

    #include <iostream>
    #include <cstdio>
    #include <fstream>
    #include <algorithm>
    #include <cmath>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <cstring>
    #include <map>
    #include <stack>
    #include <set>
    using namespace std;
    typedef long long ll;
    const int maxx=100;
    const int n=5;
    int mp[maxx][maxx];//原始地图
    int vis[maxx][maxx];//路径(染色)
    int dx[maxx];//输出路径
    int dy[maxx];
    void dfs(int i,int j)
    {
        if(mp[i][j]==1 || i<1 || j>n || i>n || j<1)
            return;
        mp[i][j]=1;
        if(!mp[i][j+1] && j+1>=1 && j+1<=n)//向右
        {
            vis[i][j+1]=vis[i][j]+1;
            dfs(i,j+1);
        }
         if(!mp[i+1][j] && i+1>=1 && i+1<=n)//向下
        {
            vis[i+1][j]=vis[i][j]+1;
            dfs(i+1,j);
        }
        if(!mp[i-1][j] && i-1>=1 && i-1<=n)//向上
        {
            vis[i-1][j]=vis[i][j]+1;
            dfs(i-1,j);
        }
        if(!mp[i][j-1] && j-1>=1 && j-1<=n)//向左
        {
            vis[i][j-1]=vis[i][j]+1;
            dfs(i,j-1);
        }
    }
    int main()
    {
        memset(vis,0,sizeof(vis));
        memset(mp,0,sizeof(mp));
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                cin >> mp[i][j];
        vis[1][1]=1;
        dfs(1,1);
        //路径直观图,可以看走的过程,需要看的把注释去掉(力荐!!!)
       /* for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                printf("%4d ",vis[i][j]);
            }
            cout << endl;
        }*/
        int g=vis[n][n];
        int x,y;
        x=n,y=n;
        dx[g]=n;
        dy[g]=n;
        while(g!=0)//按步数从大到小输出
        {
            g--;
            if(vis[x-1][y]==g)
            {
                dx[g]=x-1;
                dy[g]=y;
                x--;
            }
            else if(vis[x+1][y]==g)
            {
                dx[g]=x+1;
                dy[g]=y;
                x++;
            }
            else if(vis[x][y+1]==g)
            {
                dx[g]=x;
                dy[g]=y+1;
                y++;
            }
            else if(vis[x][y-1]==g)
            {
                dx[g]=x;
                dy[g]=y-1;
                y--;
            }
        }
        for(int i=1; i<=vis[n][n]; i++)
        {
            cout << "(" << dx[i]-1 << "," << " " <<dy[i]-1 << ")" << endl;
        }
    }
  • 相关阅读:
    让我们一起Go(八)
    让我们一起Go(七)
    让我们一起Go(六)
    VTemplate模板引擎的使用入门篇
    超时时间已过或服务器未响应的解决方法
    SQL Server 2005使用BCP命令将数据表导出到Excel第一行包括表头
    VTemplate模板引擎的使用进阶篇
    VTemplate模板引擎的使用认识篇
    免费开源的模板引擎VTemplate
    .NET 4.5 中新提供的压缩类
  • 原文地址:https://www.cnblogs.com/sky-stars/p/11178554.html
Copyright © 2011-2022 走看看