hdoj2579(Dating with girls(2)(搜索)

hdoj2579(Dating with girls(2)(广搜)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2704    Accepted Submission(s): 759


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
 

分析刚开始以为和以前的做过的迷宫问题一样,把走过的坐标标记一下,不能再次走该点,用mark[ ][ ]=1标记 
结果提交总是错,后来看到别人的博客解题,才发现此题走过的点可以重复多次走。
若你用的是mark[ ][ ]标记的话,可以试一试这组测试数据:
结果提交总是错,后来看到别人的博客解题,才发现此题走过的点可以重复多次走。
若你用的是mark[ ][ ]标记的话,可以试一试这组测试数据:



3 3 5
Y##
..#
##G

正确结果为:6


后来想到走过的点不标记不就解决了吗,可是仔细一想若给的测试数据,最终能找到终点则不标记也可以,对最后结果没影响 ,可是
若给的测试数据最终不能找到终点,由于没有标记,程序会一直重复查找进入死循环。说明还是要标记,不过要用mak[ ][ ][t%k]
标记 ,假设k=10, (t%k=1,2,3,....k-1),当mark[x][y][1].........mark[x][y][9]...mark[x][y][0],再次循环到mark[x][y][1]时,说明
该点已被走过10次了,若再次走 (即;mark[x][y][1])相当于又回到最初的起点又开始第二次循环查找,因为第一次循环没有查找到出口,
第二次仍是重复第一次的路径,仍不能找到出口,因此查找只要循环一个周期 ,就可以了(即:用mark[x][y][t%k]进行标记)

 (该题类似:ZOJ 1671(广度搜索bfs)(Walking Ant))


My  solution:
/*2015.11.3*/
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char map[110][110];
int mark[110][110][11],x,y,ex,ey,ans,r,c,k;
int b[4]={1,-1,0,0},d[4]={0,0,1,-1};
struct node
{
	int tx,ty,cnt;
}e,p;
int panduan(struct node g)
{
	int t1,t2,t3;
	t1=g.tx;
	t2=g.ty;
	t3=g.cnt;
	if(t1<0||t1>=r||t2<0||t2>=c)
	return 0;
	if(mark[t1][t2][t3%k]==1)
	return 0;
	if(map[t1][t2]=='#')
	{
		if(t3%k==0)
		return 1;
		else
		return 0;
	}
	return 1;
}
void bfs()
{
	int i;
	queue<node>q;
	e.tx=x;e.ty=y;
	e.cnt=0;
	q.push(e);
	while(!q.empty())
	{
		p=q.front();
		q.pop();
		for(i=0;i<4;i++)
		{
			e.tx=p.tx+b[i];
			e.ty=p.ty+d[i];
			e.cnt=p.cnt+1;
			if(panduan(e))
			{
				if(e.tx==ex&&e.ty==ey)
				{
					ans=e.cnt;
					return ;
				}
				mark[e.tx][e.ty][e.cnt%k]=1;
				q.push(e);
			}
		}
	}	
}
int main()
{
	int n,i,j;
	scanf("%d",&n);
	while(n--)
	{
		ans=0;
		memset(mark,0,sizeof(mark));
		scanf("%d%d%d",&r,&c,&k);
		for(i=0;i<r;i++)
		{
			getchar();/*注意输入的字符的存放格式,把字符正确放入相应变量中*/ 
			for(j=0;j<c;j++)
			{
			  scanf("%c",&map[i][j]);
			  if(map[i][j]=='Y')
			  {
			  	x=i,y=j;
			  }
			  if(map[i][j]=='G')
			  {
			  
			    ex=i,ey=j;
		      }
		   }
		}
		bfs();
		if(ans)
		printf("%d\n",ans);
		else
		printf("Please give me another chance!\n");
	  getchar();/*注意输入的字符的存放格式,把字符正确放入相应变量中*/ 
   }
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值