迷宫 plus (深搜 dfs)

题目描述
给定n*m的迷宫 (2<n,m<24) ,迷宫中’0’代表路,'1’代表墙,'2’代表起点,'3’代表终点,求起点到终点最短距离

输入
多实例测试
第一行输入n,m
下面是n行,每行输入m个字符
输出
输出起点到终点最短距离
样例输入
5 3
0 0 0
0 1 0
0 2 0
0 1 3
0 0 0
样例输出
2
提示
多组样例输入

输入保证有一条路从起点到终点
题解:

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
using namespace std;
bool vst[100][100];
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
int fang[105][105];
int migong[105][105];
int n,m,sum=0,en1,en2,st1,st2;
struct state
{
    int x,y;
};
state t,a[30];
bool youshu(state s)
{
    if(!vst[s.x][s.y]&&s.x>=0&&s.x<n&&s.y>=0&&s.y<m)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int bfs(state st,int n,int m)
{
   sum=0;//为走通该路的最短长度
   queue<state>q;
    state now,next;
    q.push(st);
    vst[st.x][st.y]=1;
    int k=0,i,j;
    while(!q.empty())
    {
        now=q.front();
        for(i=0; i<4; i++)
        {
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            if(youshu(next))
            {
                fang[next.x][next.y]=i;
                q.push(next);
                vst[next.x][next.y]=1;
            }
        }
        q.pop();
        if(now.x==en1&&now.y==en2)
        {
            a[k++]=now;
            next=now;
            while(1)
            {
                switch(fang[now.x][now.y])
                {
                case 0:
                    next.y=now.y-1;
                    break;
                case 1:
                    next.y=now.y+1;
                    break;
                case 2:
                    next.x=now.x-1;
                    break;
                case 3:
                    next.x=now.x+1;

                }
                a[k++]=next;
                now=next;
                if(next.x==st1&&next.y==st2)
                    break;
            }
            k--;
            while(k>0)
            {
                sum++;
                k--;
            }
            //return ;
        }
    }
    return sum;
}
int main()
{

    int i,j;
    while(cin>>n>>m)
    {
        memset(vst,0,sizeof(vst));
    for(int o=0; o<n; o++)
    {
        for(int y=0; y<m; y++)
        {
            cin>>migong[o][y];
            if(migong[o][y]==1)
            {
                vst[o][y]=1;
            }
            if(migong[o][y]==2)
            {
                st1=o;
                st2=y;
            }
            if(migong[o][y]==3)
            {
                en1=o;
                en2=y;
            }
        }
    }
    t.x=st1;
    t.y=st2;
    cout<<bfs(t,n,m)<<endl;
    }return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值