Dating with girls(2)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1418    Accepted Submission(s): 393

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

 

Source

HDU 2009-5 Programming Contest

 

Recommend

lcy


解题心得:
1、这个题是一个新的类型的bfs搜索,其中有一点很重要,那就是在k的倍数的时候墙会消失,所以在处理的时候要注意每个点的状态的标记,因为可能在墙消失之后有些走了的地点还需要重新走,所以状态要分时间标记,原本想的是标记墙在的状态和墙不在的状态就可以了,但是仔细一想,每一刻的状态是相互影响的,墙消失 的前一秒对墙消失的时候有影响,所以需要标记t%k的每一个时刻的状态,由于数据量不大,所以可以如此处理。
2、在处理地图中的元素随时间会产生变化的时候我们需要分时间标记状态,看前后时间是否会互相的影响,若后一时刻需要独立的处理不能受前一个时刻的状态影响应该将状态分时间标记,当然也要注意数据量,数据量太大的时候这种方法可能并不适用。


#include<bits/stdc++.h>
using namespace std;
const int maxn = 110;
int k,n,m;
char maps[maxn][maxn];
int dir[4][2] = {1,0,0,1,-1,0,0,-1};
bool vis[maxn][maxn][15],flag;//三维数组第三维为时间的标记
struct node
{
int x,y;
int step;
} now,Next; bool check(int x,int y)
{
if(x<0 || y<0 || x>=n || y>=m)
return false;
else
return true;
} void pre_maps()
{
now.x = -1;
now.y = -1;
memset(maps,0,sizeof(maps));
scanf("%d%d%d",&n,&m,&k);
for(int i=0; i<n; i++)
scanf("%s",maps[i]);
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
if(maps[i][j] == 'Y')
{
now.x = i;
now.y = j;
now.step = 0;
return ;
}
}
} void bfs()
{
queue <node> qu;
qu.push(now);
memset(vis,0,sizeof(vis));
vis[now.x][now.y][now.step%k] = 1;
while(!qu.empty())
{
now = qu.front();
if(maps[now.x][now.y] == 'G')
{
printf("%d\n",now.step);
flag = true;
return ;
}
qu.pop();
Next.step = now.step + 1;
for(int i=0; i<4; i++)
{
Next.x = now.x + dir[i][0];
Next.y = now.y + dir[i][1];
if(check(Next.x,Next.y))
{
if((maps[Next.x][Next.y] != '#' || !(Next.step%k)) && !vis[Next.x][Next.y][Next.step%k])
{
qu.push(Next);
vis[Next.x][Next.y][Next.step%k] = 1;
}
}
}
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
flag = false;
pre_maps();
if(now.x == -1 && now.y == -1)
{
printf("Please give me another chance!\n");
continue;
}
bfs();
if(!flag)
printf("Please give me another chance!\n");
}
}


BFS:HDU2597-Dating with girls(2) (分时间标记状态)的更多相关文章

  1. hdu 2579 Dating with girls(2) (bfs)

    Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. hdoj 2579 Dating with girls(2)【三重数组标记去重】

    Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  3. hdu 2579 Dating with girls(2)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2579 Dating with girls(2) Description If you have sol ...

  4. PAT甲级:1036 Boys vs Girls (25分)

    PAT甲级:1036 Boys vs Girls (25分) 题干 This time you are asked to tell the difference between the lowest ...

  5. hdu 2578 Dating with girls(1)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2578 Dating with girls(1) Description Everyone in the ...

  6. Dating with girls(1)(二分+map+set)

    Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. hdu 2578 Dating with girls(1) (hash)

    Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. 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/Oth ...

  9. HDU 3784 继续xxx定律 & HDU 2578 Dating with girls(1)

    HDU 3784 继续xxx定律 HDU 2578 Dating with girls(1) 做3748之前要先做xxx定律  对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n+ ...

随机推荐

  1. 网页mp3不能获取,报404问题解决

    js有些时候有些报错就是很莫名其妙 一 代码没错,js,html一点红都没有 然后上正式的时候,mp3不能播放音乐,报404 二 出这个错误,我第一反应是正式的配置有问题,毕竟开发测试都可以是不是? ...

  2. java向上取整向下取整

    向上取整用Math.ceil(double a) 向下取整用Math.floor(double a) 举例: public static void main(String[] args) throws ...

  3. orcale开篇

    1.数据库系统和数据库的管理系统  数据库系统=数据库的管理系统+oper操作员+硬件2.Oracle的版本  8i/ 9i 10g/11g  12c(cloud)3.实例和数据库的关系  实例:数据 ...

  4. 一、Spring-Data-Jpa 初体验(基于SpringBoot)

    闲话少说,首先动起来(基于springboot+gradle): 1.引入依赖 dependencies { compile 'org.springframework.boot:spring-boot ...

  5. 零基础逆向工程31_Win32_05_提取图标_修改标题

    在程序中使用图标 1.加载图标 HICON hIcon; hIcon = LoadIcon (hAppInstance, MAKEINTRESOURCE (IDI_ICON)); hAppInstan ...

  6. LeetCode Word Ladder 找单词变换梯

    题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...

  7. Google Guava入门(一)

    Guava作为Java编程的助手,可以提升开发效率,对Guava设计思想的学习则极大的有益于今后的编程之路.故在此对<Getting Started with Google Guava>一 ...

  8. 小w的糖果

    题目连接 : https://ac.nowcoder.com/acm/contest/923/C 算是一道找规律的题了,因为后一个人会比前一个人多,可以理解成后一个人要继承前一个人,sum为当前糖果数 ...

  9. 三种序列化方式存取redis的方法

    常见的的序列化反序列方式的效率: protoBuf(PB) > fastjson > jackson > hessian > xstream > java 数据来自于:h ...

  10. sessionStorage 和 localStorage

    html5 中的 web Storage 包括了两种存储方式:sessionStorage 和 localStorage. sessionStorage 用于本地存储一个会话(session)中的数据 ...