1254:走出迷宫

拿前面的题代码改改就行了。。这种题目就那么几个地方能挖空   

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <map>
#include <set>
#include<iomanip>
#include <bits/stdc++.h>
/***************************************/
#define  ll  long long
#define int64 __int64
#define PI 3.1415927
#define INF 99999
const int maxn = 2010;
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
int dir8[][2]={{-2,1},{-2,-1},{-2,2},{-2,-2},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1},{2,-2},{2,2}};
int dir4[4][2]={{-1,0},{1,0},{0,-1},{0,1}};


char mp[310][110];
int n,m;
int vis[310][310];
struct node {
int x,y;
int step;
};int ans=99999999;
node ed;
 void bfs(node st)
 {
    queue<node>q;
    vis[st.x][st.y]=1;
    q.push(st);

     while(!q.empty())
     {
         node cur=q.front();
         q.pop();
         if (cur.x==ed.x&&cur.y==ed.y)
         {
        ans=cur.step;
             return ;
         }

         for(int i=0;i<4;i++)
         {
              node nx;nx.x=cur.x+dir4[i][0];nx.y=cur.y+dir4[i][1];


              if(nx.x<0||nx.y<0||nx.x>=n||nx.y>=m||vis[nx.x][nx.y]||mp[nx.x][nx.y]=='#')continue;

              vis[nx.x][nx.y]=1;
              nx.step=cur.step+1;
              q.push(nx);
         }






     }



 }





int main()
{

 cin>>n>>m;


    memset(vis,0,sizeof(vis));
     node st;
 for(int i=0;i<n;i++)
 for(int j=0;j<m;j++){

    cin>>mp[i][j];
    if(mp[i][j]=='S')
    {
        st.x=i;st.y=j;
        st.step=0;
    }if(mp[i][j]=='T')
    {
        ed.x=i;ed.y=j;

    }


 }

ans=99999;
 bfs(st);



     cout<<ans<<endl;



   return 0;
}

 

程序在VC++ 6下顺利编译通过。 一、 实验目的: (1) 熟练掌握链栈的基本操作及应用。 (2) 利用链表作为栈的存储结构,设计实现一个求解迷宫的非递归程序。 二、实验内容: 【问描述】 以一个m×n的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。设计一个程序,对信任意设定的迷宫,求出一条从入口到出口的通路,或得出没有通路的结论。 【基本要求】 首先实现一个链表作存储结构的栈类型,然后编写一个求解迷宫的非递归程序。求得的通路以三元组(i,j,d)的形式输出,其中:(i,j)指示迷宫中的一个坐标,d表示走到下一坐标的方向。如:对于下列数据的迷宫,输出的一条通路为:(1,1,1),(1,2,2),(2,2,2),(3,2,3),(3,1,2),……。 【测试数据】 迷宫的测试数据如下:左上角(1,1)为入口,右下角(8,9)为出口。 1 2 3 4 5 6 7 8 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 1 0 1 0 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 0 0 0 以方阵形式输出迷宫及其通路。 输出: 请输入迷宫的长和宽:5 5 请输入迷宫内容: 0 1 1 0 0 0 0 1 1 0 1 0 0 1 1 1 0 0 1 0 1 1 0 0 0 迷宫的路径为 括号内的内容分别表示为(行坐标,列坐标,数字化方向,方向) (1,1,1,↓) (2,1,2,→) (2,2,1,↓) (3,2,1,↓) (4,2,2,→) (4,3,1,↓) (5,3,2,→) (5,4,2,→) (5,5,0,) 迷宫路径探索成功!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值