zoukankan      html  css  js  c++  java
  • c++ 迷宫问题

    迷宫问题

    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)
    

    Source

    // -*- C++ -*-
    //===----------------------------- he.cpp ---------------------------------===//
    //
    //                     The LLVM Compiler Infrastructure
    //
    // This file is dual licensed under the MIT and the University of Illinois Open
    // Source Licenses. See LICENSE.TXT for details.
    //
    //===----------------------------------------------------------------------===//
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int g[6][6];
    bool used[6][6];
    int sx = 0, sy = 0, ex = 4, ey = 4;
    int dx[4] = {-1, 1, 0, 0};
    int dy[4] = {0, 0, -1, 1};
    struct Point {
        int x, y;
        int steps;
        int p[30];
    } que[30], v, u, s;
    
    void bfs() {
        s.x = sx;
        s.y = sy;
        s.steps = 0;
        s.p[s.steps] = 0;
        int f = 0, e = 0;
        que[e++] = s;
        used[sx][sy] = true;
        while (f <= e) {
            u = que[f++];
            if (u.x == ex && u.y == ey) {
                int xx = 0, yy = 0;
                cout << "(" << xx << ", " << yy << ")" << endl;
                for (int i = 1; i <= u.steps; ++i) {
                    xx = xx + dx[u.p[i]];
                    yy = yy + dy[u.p[i]];
                    cout << "(" << xx << ", " << yy << ")" << endl;
                }
                return;
            }
            for (int i = 0; i < 4; ++i) {
                int nx = u.x + dx[i];
                int ny = u.y + dy[i];
                if (nx >= 0 && nx < 5 && ny >= 0 && ny < 5 && g[nx][ny] != 1 && !used[nx][ny]) {
                    v.x = nx;
                    v.y = ny;
                    v.steps = u.steps + 1;
                    for (int j = 0; j < u.steps; ++j) {
                        v.p[j] = u.p[j];
                    }
                    v.p[v.steps] = i;
                    que[e++] = v;
                    used[nx][ny] = true;
                }
            }
        }
    }
    
    int main() {
        for (int i = 0; i < 5; ++i) {
            for (int j = 0; j < 5; ++j) {
                cin >> g[i][j];
            }
        }
        memset(used, false, sizeof(used));
        bfs();
        return 0;
    }
    /*
     * 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
    
    */
    
  • 相关阅读:
    Rational工具介绍(转)
    MySQL表的存储引擎
    2009 年度最佳 jQuery 插件
    09年关门歇业的15大网站 雅虎旗下4网站上榜
    [转载]windows2003上IIS+PyISAPIe1.1..0部署成功
    安装IronPythonStudio出错:已解决
    C# 开源项目
    Cassandra在windows平台下安装布署,对No SQL感兴趣的朋友可以进来看看
    【转载】开发人员用的一些好网站
    [转载]用来武装Firebug的十三款Firefox插件
  • 原文地址:https://www.cnblogs.com/LJA001162/p/11440203.html
Copyright © 2011-2022 走看看