Dating with girls(2)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 802 Accepted Submission(s): 195
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
这题就是有一点要注意:要开多一维数组来保存该点的步数状态,因为那些障碍物会在第k的倍数消失,所以在该点来走过也是可以再走的,所以要开多一维数组保存步数状态,就是这么多!
代码:
#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <queue>
using namespace std;
int xx[4] = {1, -1, 0, 0};
int yy[4] = {0, 0, -1, 1};
char a[105][105];
int map[105][105][15]; //开3维数组保存该点步数
int x1, y1, x2, y2;
int N, M, K;
bool flag;
struct node
{
int x, y, step;
}n, m;
int main()
{
int i, j, k, t;
scanf("%d", &t);
while(t--)
{
scanf("%d %d %d", &N, &M, &K);
for(i = 0; i < N; i++)
{
getchar();
for(j = 0; j < M; j++)
{
scanf("%c", &a[i][j]);
if(a[i][j] == 'Y')
x1 = i, y1 = j; //找'Y'坐标
if(a[i][j] == 'G')
x2 = i, y2 = j; //找'G'坐标
}
}
for(i = 0; i < N; i++)
for(j = 0; j < M; j++)
for(k = 0; k < K; k++)
map[i][j][k] = 1000000; //初始化
flag = false;
map[x1][y1][0] = 0;
n.x = x1; n.y = y1; n.step = 0;
queue<node> Q;
Q.push(n);
while(!Q.empty())
{
m = Q.front();
Q.pop();
if(m.x == x2 && m.y == y2) //到达目标
{
flag = true;
break;
}
for(i = 0; i < 4; i++)
{
n.x = m.x + xx[i];
n.y = m.y + yy[i];
n.step = m.step + 1;
if(n.x>=0 && n.x<N && n.y>=0 && n.y<M) //判断是否越界
{
//判断该点是否可行和该点步数是否大于该点曾经步数
if(a[n.x][n.y] == '#' && n.step%K != 0) continue;
if(n.step >= map[n.x][n.y][n.step%K]) continue;
map[n.x][n.y][n.step%K] = n.step; //更新该点步数
Q.push(n);
}
}
}
if(flag) printf("%d\n", m.step);
else printf("Please give me another chance!\n");
}
return 0;
}
分享到:
相关推荐
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu2101AC代码
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
搜索 dfs 解题代码 hdu1241
2-sat---hdu3062,代码详尽,清晰,格式规范,亲测无误。
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
hdu1290 解题报告 献给杭电五十周年校庆的礼物 (切西瓜问题,即平面分割空间)
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 1166线段树代码
HDU最全ac代码
自己做的HDU ACM已经AC的题目
hdu动态规划算法集锦
hdu题目分类
HDU图论题目分类