zoukankan      html  css  js  c++  java
  • K

    定义一个二维数组: 

    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)
    /*
    * @Author: lyuc
    * @Date:   2017-05-02 16:31:37
    * @Last Modified by:   lyuc
    * @Last Modified time: 2017-05-02 16:49:57
    */
    
    #include <iostream>
    #include <stdio.h>
    #include <queue>
    #include <string.h>
    using namespace std;
    struct node{
        int x,y;
        int step;
        node(){}
        node(int a,int b,int c){
            x=a;
            y=b;
            step=c;
        }
    };
    int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    int mapn[7][7];
    int path[30];//用来记录路径
    bool vis[7][7];
    bool ok(int x,int y){
        if(x<0||x>=5||y<0||y>=5||mapn[x][y]||vis[x][y]) return false;
        return true; 
    }
    int bfs(){
        queue<node>q;
        node start,tmp;
        q.push(node(0,0,0));
        vis[0][0]=true;
        while(!q.empty()){
            start=q.front();
            q.pop();
            if(start.x==4&&start.y==4)
                return start.step;
            for(int i=0;i<4;i++){
                tmp.x=start.x+dir[i][0];
                tmp.y=start.y+dir[i][1];
                tmp.step=start.step+1;
                if(ok(tmp.x,tmp.y)==false){
                    continue;    
                } 
                vis[tmp.x][tmp.y]=true;
                path[tmp.x*5+tmp.y]=start.x*5+start.y;
    
                q.push(tmp);
            }
        }
        return -1;
    }
    void print(int u){
        if(path[u]==-1){
            return;
        }
        print(path[u]);
        printf("(%d, %d)
    ",u/5,u%5);
    
    }
    void init(){
        memset(vis,false,sizeof vis);
        memset(path,-1,sizeof path);
    }
    int main(){
        // freopen("in.txt","r",stdin);
        while(scanf("%d",&mapn[0][0])!=EOF){
            init();
            for(int i=1;i<5;i++){
                scanf("%d",&mapn[0][i]);
            }
            for(int i=1;i<5;i++){
                for(int j=0;j<5;j++){
                    scanf("%d",&mapn[i][j]);
                }
            }
            bfs();
            puts("(0, 0)");
            print(24);
        }
        return 0;
    }
  • 相关阅读:
    oracle计算记录条数
    oracle 尽量多使用COMMIT
    oracle 用TRUNCATE替代DELETE
    oracle 删除重复记录
    oracle整合简单,无关联的数据库访问
    oracle使用DECODE函数来减少处理时间
    oralce 减少访问数据库的次数
    古城钟楼微博地支报时程序铛,100行代码实现,价值一天20万粉丝
    C#中HttpWebRequest与HttpWebResponse的使用方法
    C#中用HttpWebRequest中发送GET/HTTP/HTTPS请求
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6796915.html
Copyright © 2011-2022 走看看