wzoi.cc简易小迷宫AC代码

600多的一题,欢迎观看!

题目描述:

有一个 m 行 n 列的迷宫,|表示不通,*表示通行,每一步可以沿上下左右四个方向,

还有四个整数 a, b, c, d,要求从 a 行 b 列走到 c 行 d 列的最少步数。 

样例输入:
3 4 1 1 2 4
****
*||*
****
样例输出:
4
提示:

m,n<=10

时间限制: 1000ms
空间限制: 256MB


来源: USACO

解析

广搜可以解决,上代码!

制做不易,点个赞

#include<bits/stdc++.h>		//包括万能库
using namespace std;		//使用默认的命名空间
int maps[1010][1010],f[1010][1010];
int u[5]= {0,0,0,1,-1};   //定义4个方向的增量
int w[5]= {0,1,-1,0,0};
int n,m,n1,n2,m1,m2;
int x,y,step;
string ch;
struct data {
	int x,y,step; 
} ;
queue<data> q;

int main() {	
	cin>>n>>m>>n1>>n2>>m1>>m2;
	for(int i=1; i<=n; i++) {
		cin>>ch;
		for(int j=0; j<m; j++) {
			if(ch[j]=='*') maps[i][j+1]=0;
			else maps[i][j+1]=1;
		}
	}
	q.push(  data {n1,n2,0 }); 
	f[ n1 ][ n2 ] = 1;
	while( !q.empty() ) { 
		data head=q.front();
		q.pop();
		for(int i=1;i<=4;i++){
			int x=head.x+u[i];
			int y=head.y+w[i];
			if(x<1||x>n||y<1||y>m||maps[x][y]||f[x][y])
			continue;
			q.push(  data {x,y,head.step+1}); 
	f[x][y] = 1;
	if(x==m1&&y==m2){
		cout<<head.step+1;
		return 0;
	}
		}
	}
	return 0;
}

结束了!

 

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值