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. MySQL三种存储引擎总结

    MySQL三种存储引擎 MyISAM.InnoDB.MEMORY 1.MyISAM MyISAM,3.23.34a前的默认存储引擎. 优缺点 优点 在于占用空间小,处理速度快. 缺点 不支持事务的完整 ...

  2. 解决pyhton aiohttp ssl:None [[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)

    解决pyhton aiohttp ssl:证书报错问题, 错误信息> Cannot connect to host oapi.dingtalk.com:443 ssl:None [[SSL: C ...

  3. 转---JS 获取鼠标左右键

    原文:http://blog.csdn.net/mine3333/article/details/7291557 function test() { alert(event.x+" &quo ...

  4. 微信小程序:从本地相册选择图片或使用相机拍照。

    wx.chooseImage(OBJECT) 从本地相册选择图片或使用相机拍照. OBJECT参数说明: 示例代码: wx.chooseImage({ count: 1, // 默认9 sizeTyp ...

  5. 一些C/C++中的函数

    项目中使用到的C/C++中的一些函数,记录下来加以理解和掌握. 1.memset( ) memset是计算机中C/C++语言函数.将s所指向的某一块内存中的前n个 字节的内容全部设置为ch指定的ASC ...

  6. Flash图表控件FusionCharts调整图表百分比大小

    用户可以为图表的宽度和高度设置百分比值,用来替代绝对的像素值. 以百分比的方式调整图表,首先需要更新HTML代码,如下所示: <div id="chartContainer" ...

  7. RxJava 1升级到RxJava 2过程中踩过的一些“坑”

    RxJava2介绍 RxJava2 发布已经有一段时间了,是对 RxJava 的一次重大的升级,由于我的一个库cv4j使用了 RxJava2 来尝鲜,但是 RxJava2 跟 RxJava1 是不能同 ...

  8. 腾讯云服务器CVM购买详细过程 选择我们需要的腾讯云服务器

    腾讯云服务商有云服务器.云数据库.CDN.云存储等产品,其中较多的用户会选择腾讯云服务器,因为用途比较广泛,比如用来软件的运行以及网站建设,如今一般都是用云服务器,而不是用虚拟主机,毕竟虚拟主机的性价 ...

  9. JQuery笔录

    1.jQuery 的 hide() 函数,隐藏了 HTML 文档中所有的 <p> 元素.<script type="text/javascript">$(d ...

  10. 20.JSON

    JSON是javascript的一个子集,利用js中的一些儿模式来表示结构化数据.不是只有javascript才使用JSON,JSON是一种数据格式,很多编程语言都有针对JSON的解析器和序列化器. ...