hdu 逃离迷宫

这题真是坑啊. . . . . 

思路比较特别:

每次储存转折一次的所有点(4个方向)并标记,然后从这些点中依次储存转折一次的所有点,这样就把最短转折标记了

注意事项:

1 尼玛这个题是先输入y坐标再输入x坐标的

2 如果碰到某个点被标记了,那么应该跳过它,比如下面这个样例数据:

0 0 0 0 0 2

0 6 5 7 0 4

0 2 0 0 0 0

0 1 0 4 0 8

3 0 0 7 3 9

输入为 2 1 5 5 5

代码如下:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int n,m,k,sx,sy,ex,ey,visit[105][105];
char map[105][105];
struct node {
	int x;
	int y;
	int turn;
};
bool bfs(){
	queue<node>q;
	while(!q.empty())q.pop();
	node d,D;
	int move[4][2]={0,1, 1,0, 0,-1, -1,0};
	d.x=sx;d.y=sy;d.turn=0;
	q.push(d);
	while(!q.empty()){
		d=q.front();
		q.pop();
		if(d.x==ex && d.y==ey )return true;
		for(int i=0;i<4;i++){
			D.x=d.x+move[i][0];
			D.y=d.y+move[i][1];
			D.turn=d.turn+1;
			while(D.x>=1 && D.y>=1 && D.x<=n && D.y<=m && map[D.x][D.y]=='.' && D.turn<=k){
				if(!visit[D.x][D.y]){
				//	cout<<D.x<<" "<<D.y<<endl;
					visit[D.x][D.y]=D.turn;
					q.push(D);
				}
				D.x+=move[i][0];
				D.y+=move[i][1];
			}
		}
	}
	return false;
} 
int main(){
	int test;
	cin>>test;
	while(test--){
		memset(visit,0,sizeof(visit));
		memset(map,0,sizeof(map));
		cin>>n>>m;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				cin>>map[i][j];
			}
		}
		cin>>k>>sy>>sx>>ey>>ex;
		k++;
	//	cout<<"**"<<k<<endl;
		if(bfs())cout<<"yes\n";
		else cout<<"no\n";
		/*for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				cout<<visit[i][j]<<" ";
			}
			cout<<endl;
		}
		cout<<endl;*/
	}
} 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值