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. 13.padding和margin,几种参数

    这篇会很短. 那么如上图所示,margin指的是外边距,padding指的是内边距,border自有其像素宽度,element在1335乘以392的地方. margin和padding一样总共有四个, ...

  2. Android客户端与PC服务端、android服务端通过WiFi通信

    前期准备:我的是Linux Mint操作系统(总之折腾的过程中怀疑过是不是系统的问题),首先是要创建wifi热点给android手机使用,这个时候笔记本作为通信的服务器端,android手机作为客户端 ...

  3. kickstart2019 round_A B. Parcels

    思路: 利用了曼哈顿距离和切比雪夫距离之间的转化. 参考: https://blog.csdn.net/Dylan_Frank/article/details/88985444 https://www ...

  4. MobaXterm连接远程Linux服务器

    MobaXterm是一个X服务器和一组的Unix命令(GNU/ Cygwin的)封装在一个单一的便携式exe文件的增强终端. MobaXterm包括一个巨大的multitab原生的Windows终端. ...

  5. 常用的CSS属性列表汇总

    常用的CSS属性列表汇总 近期教学给学员总结常用的CSS属性,方便学习查询,正好发上来也给大家分享一下. 表格最右列的数字标识支持的CSS最低版本. 01. CSS背景属性(Background) 属 ...

  6. 编译安装PHP-7.1.8

    安装依赖包: 1.安装yasm cd /usr/local/src tar zxvf yasm-1.3.0.tar.gz cd yasm-1.3.0 ./configure make make ins ...

  7. python 之开发工具 sublimetext 3

    一.前言 由于个人工作内容太过于繁杂,记忆力又不好,为日后使用的方便,故简单的记录了本篇关于sublimetext 3的初始化安装和部分插件内容的记录.目前最新的版本也是3.0以上版本了,故我这里使用 ...

  8. AD7190的小总结

    1.单次转换模式 通过配置“模式寄存器的MD2.MD1.MD0为001”,便可启动单次转换. 流程“上电 -> 单次转换 -> 省电模式 ” , 片内振荡上电需要大约1ms.   单次转换 ...

  9. cms-框架搭建

    1.web.xml中的配置,在配置中主要有: 1.1.过滤器: 1.1.1:shiro权限过滤器 1.1.2:字符编码过滤器 1.2.监听器: 1.2.1:spring监听器 1.3.servlet ...

  10. ubuntu下JDK安装(更新旧版本JAVA)

    1.sudo apt-get install openjdk-8-jre openjdk-8-jdk 2.默认会安装在 路径为 /usr/lib/jvm/java-7-openjdk-amd64 下面 ...