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

                            迷宫问题
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 27854   Accepted: 16056

    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

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int map[6][6];
    int ans=0x7f7f7f7f;
    int dx[4]={1,-1,0,0};
    int dy[4]={0,0,1,-1};
    int ansx[40],ansy[40];
    int tmpx[40],tmpy[40];
    void dfs(int x,int y,int step){
        if(step>ans)    return ;
        if(x==4&&y==4){
            if(step>ans)    return ;
            ans=step; 
            for(int i=1;i<=step;i++){
                ansx[i]=tmpx[i];
                ansy[i]=tmpy[i];
            }
            return ; 
        }
        for(int i=0;i<4;i++){
            int cx=x+dx[i];
            int cy=y+dy[i];
            if(cx>=0&&cx<=4&&cy>=0&&cy<=4&&!map[cx][cy]){
                tmpx[step+1]=cx;
                tmpy[step+1]=cy;
                map[cx][cy]=1;
                dfs(cx,cy,step+1);
                map[cx][cy]=0;
            }
        }
    }
    int main(){
        for(int i=0;i<5;i++)
            for(int j=0;j<5;j++)
                scanf("%d",&map[i][j]);
        dfs(0,0,0);
        printf("(0, 0)
    ");
        for(int i=1;i<=ans;i++)
            cout<<"("<<ansx[i]<<", "<<ansy[i]<<")"<<endl;
    }
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    html笔记之表单标签
    删除重复字符
    OC点语法和变量作用域
    PHP-You don’t have permissions to access xxx on this server!
    从70周年阅兵式寻找使命
    做市场需要的智能眼镜可以赚钱
    《华为你学不会》读书笔记
    做智能眼镜是为了更方便地拍摄
    智能眼镜是头戴式摄像机
    DA14580_583_DK_II开发板入门笔记
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/8414119.html
Copyright © 2011-2022 走看看