ACM-BFS之Dating with girls(2)——hdu2579

本文介绍了一个有趣的迷宫寻爱问题,并通过BFS(广度优先搜索)算法来解决。在特定条件下,迷宫中的障碍物会周期性消失,玩家需要找到最优路径与目标女孩相遇。文章详细解释了算法实现过程,包括如何处理障碍物消失的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dating with girls(2)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2579
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1959 Accepted Submission(s): 551

Problem Description

If you have solved the problem Dating with girls(1).I think you can solve this problem too.This problem is also about dating with girls. Now you are in a maze and the girl you want to date with is also in the maze.If you can find the girl, then you can date with the girl.Else the girl will date with other boys. What a pity!
The Maze is very strange. There are many stones in the maze. The stone will disappear at time t if t is a multiple of k(2<= k <= 10), on the other time , stones will be still there.
There are only ‘.’ or ‘#’, ’Y’, ’G’ on the map of the maze. ’.’ indicates the blank which you can move on, ‘#’ indicates stones. ’Y’ indicates the your location. ‘G’ indicates the girl's location . There is only one ‘Y’ and one ‘G’. Every seconds you can move left, right, up or down.


Input
The first line contain an integer T. Then T cases followed. Each case begins with three integers r and c (1 <= r , c <= 100), and k(2 <=k <= 10).
The next r line is the map’s description.

Output
For each cases, if you can find the girl, output the least time in seconds, else output "Please give me another chance!".

Sample Input
1
6 6 2
...Y..
...#..
.#....
...#..
...#..
..#G#.

Sample Output

7


Dating with girls之续,两道题除了故事上有衔接,其他好像没有相关的地方。。。

上一道题是排序+二分查找,这道题就是BFS。

但是,与简单的BFS不同的是,“墙” 有时候会消失,所以,一般除了极个别情况,一般都能约会成功。

这道题,从题意中可以明白,每个格子并非只能走一次。

所以这个遍历判断数组VIS需要变化一下。

曾经也做过类似的,以一个条件为基础。

条件:这次到达这里的时间短语曾经到达这里的时间,则可以走这个格子。

所以要建立一个三维数组,除了横纵坐标外,另一个就是step%k。

数组内容就是到达这个格子相应最短时间。

恩,约个会,追个妹纸真不容易啊,悲剧的程序猿= =。


/*
Author:Tree
From: http://blog.csdn.net/lttree
Dating with girls2 
hdu 2579
BFS——广度优先搜索
VIS数组变种
*/

#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
struct Coor
{
	int x,y,step;
};
int n,m,k,f_x,f_y,dis[4][2]={1, 0, 0, -1, -1, 0, 0, 1};
bool flag;
int vis[111][111][11];
char mapp[111][111];

void bfs(int x,int y)
{
	queue <Coor> q;
	int i;
	Coor pre,lst;

	memset(vis,-1,sizeof(vis));
	pre.x=x;
	pre.y=y;
	pre.step=0;

	q.push(pre);

	while( !q.empty() )
	{
		pre=q.front();
		q.pop();

		if(pre.x==f_x && pre.y==f_y)
		{
			flag=1;
			cout<<pre.step<<endl;
			return;
		}
		
		
		
		for(i=0;i<4;++i)
		{
			lst.x=pre.x+dis[i][0];
			lst.y=pre.y+dis[i][1];
			lst.step=pre.step+1;
			// 越界
			if( lst.x<0 || lst.y<0 || lst.x>=n || lst.y>=m )	continue;
			// 下一步是石头,但是不消失
			if( mapp[lst.x][lst.y]=='#' && lst.step%k!=0 )	continue;
			// 判断下一步是否曾经走过,若走过,当step%k相同时所到达时间必须少于曾经走到此处时间
			if( vis[lst.x][lst.y][lst.step%k]!=-1 && vis[lst.x][lst.y][lst.step%k]<=lst.step )	continue;

			vis[lst.x][lst.y][lst.step%k]=lst.step;
			q.push(lst);
		}
	}
}

int main()
{
	int i,j,test;
	int s_x,s_y;
	cin>>test;
	while( test-- )
	{
		cin>>n>>m>>k;
		for(i=0;i<n;++i)
			for(j=0;j<m;++j)
			{
				cin>>mapp[i][j];
				if(mapp[i][j]=='Y')
				{s_x=i;s_y=j;}
				else if(mapp[i][j]=='G')
				{f_x=i;f_y=j;}
			}
		flag=0;
		bfs(s_x,s_y);
		if(!flag)	cout<<"Please give me another chance!"<<endl;
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值