hdu 2579

V - Dating with girls(2)

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status Practice HDU 2579

Appoint description: 
System Crawler  (2014-11-15)

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
using namespace std;
int n,m,k;
int x_s,y_s;
int x_e,y_e;
char map[111][111];
int step[10][111][111];
int dir[4][2]={0,1, 1,0, 0,-1, -1,0};
struct node
{
	int f,x,y;
};
int BFS()
{
	queue<node>q;
	node now,next;
	int i;
	memset(step,-1,sizeof(step));
	now.f=0;
	now.x=x_s;
	now.y=y_s;
	q.push(now);
	step[now.f][now.x][now.y]=0;
	while(!q.empty())
	{
		now=q.front();
		q.pop();
		if(now.x==x_e && now.y==y_e)	return step[now.f][now.x][now.y];
		for(i=0;i<4;i++)
		{
			next.x=now.x+dir[i][0];
			next.y=now.y+dir[i][1];
			next.f=(now.f+1)%k;
			if(next.x<0 || next.x>=n || next.y<0 || next.y>=m)	continue;
			if(map[next.x][next.y]==‘#‘ && next.f)				continue;
			if(step[next.f][next.x][next.y]!=-1)				continue;
			step[next.f][next.x][next.y]=step[now.f][now.x][now.y]+1;
			q.push(next);
		}
	}
	return -1;
}
int main()
{
	int T;
	int i,l;
	int ans;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d",&n,&m,&k);
		for(i=0;i<n;i++)
		{
			scanf("%s",map[i]);
			for(l=0;map[i][l];l++)
			{
				if(map[i][l]==‘Y‘)	{x_s=i;y_s=l;map[i][l]=‘.‘;}
				if(map[i][l]==‘G‘)	{x_e=i;y_e=l;map[i][l]=‘.‘;}
			}
		}

		ans=BFS();
		if(ans==-1)	printf("Please give me another chance!\n");
		else		printf("%d\n",ans);
	}
	return 0;
}

  有点dp的味道,,写了一个 调了一晚上没调出来,,真是弱爆了,,上标程。。。。

时间: 2024-10-06 03:27:01

hdu 2579的相关文章

hdu 2579 Dating with girls(2)

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2579 Dating with girls(2) 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 th

hdu 2579 Dating with girls(2) 【经典三维BFS】

Dating with girls(2)                                                           Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2443    Accepted Submission(s): 697 链接:http://acm.hdu.edu.cn/showp

HDU 2579 (记忆化BFS搜索)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2579 题目大意:走迷宫.对于障碍点,只有当前(dep+1)%k才能走,问最少时间. 解题思路: 只有一个关键: 每个点不是只可以走一次.最多可以走k次. 原因是对于一个点,可能是通过障碍点在k的倍数(即余数为0)步到达的,也可能是通过其它途径到达的,但是不是k的倍数(余数为1~k). 这样,判断状态是否重叠的依据就是看在(x,y)点的余数状态了.vis的第三维非常重要. #include "cst

HDU 2579 Dating with girls(2) (BFS)

Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2416    Accepted Submission(s): 690 Problem Description If you have solved the problem Dating with girls(1).I think you can

HDU 2579 Dating with girls(2) BFS 余数判重

对于石头的处理就按照每个位置的时间取k的余数判一下重复就好,其他随意写 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queue> #include <deque> #include

BFS专题

题目编号 OJ编号 题目名称 题解链接 Problem A HDU 1548 A strange lift http://www.cnblogs.com/ohyee/p/5389459.html Problem B HDU 1372 Knight Moves http://www.cnblogs.com/ohyee/p/5389471.html Problem C HDU 2717 Catch That Cow http://www.cnblogs.com/ohyee/p/5389479.htm

BFS学习总结

BFS学习总结 给你一个n*m的网格迷宫,迷宫中有些格子不能走,其他的格子都能走.然后给你起点与终点,问你从起点走到终点最少需要多少步? 上面的问题就是一个典型的BFS问题,对于这类问题来说,只要你掌握了这类问题的关键思想,其实他们都是可以用类似的思路来做的. 你可以把BFS问题想象成:从一个父亲(起点状态)生儿子(后继状态),儿子又生孙子(后继状态)的过程,只要这个家族中出生了一个满意的后代(终点状态),这个家族就不生了. 但是如果这个家族中有两个完全一样的人出生(他们的辈分不一定相同),那么

http://acm.hdu.edu.cn/showproblem.php?pid=2579

#include<stdio.h> #include<string.h> #include<queue> #define N 110 int m, n, k, x1, x2, y1, y2; char map[N][N]; int v[N][N][N];//当时间是k的倍数时,障碍消失,之后又重现,所以在平时使用的二维标记数组中再加一维 int d[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; using namespace s

HDU 2578 Dating with girls(1) [补7-26]

Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3869    Accepted Submission(s): 1196 Problem Description Everyone in the HDU knows that the number of boys is larger than the