hdu 2579 Dating with girls(2)

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
  
7
 
      这题就是有一点要注意:要开多一维数组来保存该点的步数状态,因为那些障碍物会在第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;
}
 
 

你可能感兴趣的:(with)