zoukankan      html  css  js  c++  java
  • 九度oj 题目1335:闯迷宫

    题目描述:

    sun所在学校每年都要举行电脑节,今年电脑节有一个新的趣味比赛项目叫做闯迷宫。
    sun的室友在帮电脑节设计迷宫,所以室友就请sun帮忙计算下走出迷宫的最少步数。
    知道了最少步数就可以辅助控制比赛难度以及去掉一些没有路径到达终点的map。
    比赛规则是:从原点(0,0)开始走到终点(n-1,n-1),只能上下左右4个方向走,只能在给定的矩阵里走。

    输入:

    输入有多组数据。
    每组数据输入n(0<n<=100),然后输入n*n的01矩阵,0代表该格子没有障碍,为1表示有障碍物。
    注意:如果输入中的原点和终点为1则这个迷宫是不可达的。

    输出:

    对每组输入输出该迷宫的最短步数,若不能到达则输出-1。

    样例输入:
    2
    0 1
    0 0
    5
    0 0 0 0 0
    1 0 1 0 1
    0 0 0 0 0
    0 1 1 1 0
    1 0 1 0 0
    样例输出:
    2
    

    8


    这个题用bfs做,用dfs会超时


    #include<stdio.h>
    #include<iostream>
    #include<string>
    #include<string.h>
    #include<algorithm>
    #include<math.h>
    #include<map>
    #include<queue>
    #include<vector>
    using namespace std;
    struct point
    {
        int x,y;
    };
    int n,Min;
    int dir[][2]= {{0,1},{0,-1},{1,0},{-1,0}};
    int mymap[100][100];
    int vis[100][100];
    queue<point>q;
    int check(int x,int y)//判断是否可以走这个点
    {
        if(x>=0&&x<n&&y>=0&&y<=n&&!vis[x][y]&&mymap[x][y]==0)return 1;
        return 0;
    }
    void dfs(int x,int y,int cnt)//dfs
    {
    
        int tmpx,tmpy,i;
        if(x==n-1&&y==n-1)
        {
            if(cnt<Min)Min=cnt;
            return ;
        }
        if(cnt>Min)return ;
        for(i=0; i<4; i++)
        {
            tmpx=x+dir[i][0];
            tmpy=y+dir[i][1];
            if(check(tmpx,tmpy))
            {
                vis[tmpx][tmpy]=1;
                dfs(tmpx,tmpy,cnt+1);
                vis[tmpx][tmpy]=0;
            }
    
    
        }
    
        return ;
    
    
    
    }
    int  bfs()//bfs
    {
    
        int i,j,tmpx,tmpy;
        point bpoint,epoint,tmp1,tmp2;
        while(!q.empty())
        {
            q.pop();
        }
        bpoint.x=0;
        bpoint.y=0;
        epoint.x=n-1;
        epoint.y=n-1;
        vis[bpoint.x][bpoint.y]=1;
        q.push(bpoint);
        while(!q.empty())
        {
            tmp1=q.front();
            q.pop();
            for(i=0; i<4; i++)
            {
                tmpx=tmp1.x+dir[i][0];
                tmpy=tmp1.y+dir[i][1];
                if(check(tmpx,tmpy))
                {
    
                    vis[tmpx][tmpy]=vis[tmp1.x][tmp1.y]+1;
                    tmp2.x=tmpx;
                    tmp2.y=tmpy;
                    q.push(tmp2);
                }
    
            }
            if(vis[n-1][n-1])
            {
                return vis[n-1][n-1]-1;
                break;
            }
        }
    }
    
    int main()
    {
        int i,j,k,l,flag;
        char ch;
        while(cin>>n)
        {
            Min=100005;
            for(i=0; i<n; i++)
                for(j=0; j<n; j++)
                    scanf("%d",&mymap[i][j]);
            if(mymap[n-1][n-1]==1||mymap[0][0]==1)
            {
                puts("-1");
                continue;
            }
            memset(vis,0,sizeof(vis));
    //        dfs(0,0,0);
    //        if(Min!=100005)
    //            printf("%d
    ",Min);
    //        else printf("-1");
    int tt=bfs();
    if(tt!=0)printf("%d
    ",tt);
    else printf("-1
    ");
    
    
        }
    
        return 0;
    
    }
    /*
    2
    0 1
    0 0
    5
    0 0 0 0 0
    1 0 1 0 1
    0 0 0 0 0
    0 1 1 1 0
    1 0 1 0 0
    */
    





  • 相关阅读:
    网络协议-网络编程学习方法介绍
    socket函数集-----网络编程必备值得拥有
    QT 等待对话框/进度--
    qt部分类释义
    Unicode 10.0版本出现了,可以表达13万个字符, 99年定制的3.0版本不超过6万个字符
    hadoop+spark+mongodb+mysql+c#
    mysql 主从复制
    Docker for Windows
    项目设计&重构&性能优化
    发布项目到 Linux 上运行 Core 项目
  • 原文地址:https://www.cnblogs.com/hjch0708/p/7554844.html
Copyright © 2011-2022 走看看