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

    迷宫问题
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 10528   Accepted: 6260

    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

    bfs。路径打印比较麻烦,借鉴栈的思想。

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <string>
     4 #include <queue>
     5 #include <stack>
     6 #include <algorithm>
     7 #include <iostream>
     8 using namespace std;
     9 int map[6][6];
    10 int fa[30];
    11 int dx[4]={-1,1,0,0};//上下左右
    12 int dy[4]={0,0,-1,1};
    13 int main(){
    14     //freopen("D:\INPUT.txt","r",stdin);
    15     int i,j;
    16     for(i=0;i<5;i++){
    17         for(j=0;j<5;j++){
    18             scanf("%d",&map[i][j]);
    19         }
    20     }
    21     queue<int> q;
    22     q.push(0*5+0);
    23     map[0][0]=1;//已经访问过
    24     int now;
    25     while(!q.empty()){
    26         now=q.front();
    27         q.pop();
    28         int x=now/5;
    29         int y=now%5;
    30         if(x==4&&y==4){
    31             break;
    32         }
    33         for(i=0;i<4;i++){
    34             int xx=x+dx[i];
    35             int yy=y+dy[i];
    36             if(xx>=0&&xx<5&&yy>=0&&yy<5&&!map[xx][yy]){
    37                 map[xx][yy]=1;
    38                 fa[xx*5+yy]=x*5+y;//保存父节点
    39                 q.push(xx*5+yy);
    40             }
    41         }
    42     }
    43     int x=4,y=4;
    44     int print[30];
    45     i=0;
    46     while(x||y){
    47         now=x*5+y;
    48         print[i++]=now;
    49         int f=fa[now];
    50         x=f/5;
    51         y=f%5;
    52     }
    53     print[i]=0;
    54     for(;i>=0;i--){
    55         printf("(%d, %d)
    ",print[i]/5,print[i]%5);
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    【转】框架集中framespacing、border和frameborder属性的关系
    使用ArcGIS GP服务之二手工建模
    使用ArcGIS GP服务之五 JavaScript的调用
    使用ArcGIS GP服务之三发布前的准备
    计算GPS WGS_84 两点的距离
    使用ArcGIS GP服务之四GP服务发布
    计算GPS WGS_84 两点的距离 更加细腻的算法
    QueryPerformanceFrequency
    cocos2dx App 图标
    cocos2dx CCTimer::timerWithTarget回调
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4645763.html
Copyright © 2011-2022 走看看