Dating with girls(2)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2705    Accepted Submission(s):
759

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
 

题意:给一个迷宫Y是起点,G是终点,#是石头不能通过 .是路可以走,但是当走到的步数step%k==0时#全部变为路可以通过,问从起点到终点的最少步数

题解:此题不用标记走过的路,但要避免走的路径重复走,同一个路径只在同一个时刻走过(当再次走到这个点且step%k刚好再次与此时相同时 不可以走),用一个三维数组标记

从起点到终点,bfs,不同的是对于图中的点,可能走多次,分别是在不同的时刻。
vis[ t%k ][ i ][ j ]=1:表示坐标( i,j )在 t %k 时刻走过,接下来再出现 t%k 就不用再走一次了。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define MAX 110
#define INF 0x7ffff
using namespace std;
int n,m,k;
char map[MAX][MAX];
int vis[MAX][MAX][MAX];
int b,e;
struct node
{
int x,y;
int step;
friend bool operator <(node a,node b)
{
return a.step>b.step;
}
};
int judge(int a,int b)
{
if(a<0||a>=n||b<0||b>=m)
return 0;
return 1;
}
void bfs()
{
int move[4][2]={1,0,-1,0,0,1,0,-1};
node beg,end;
priority_queue<node>q;
while(!q.empty())
q.pop();
memset(vis,0,sizeof(vis));
beg.x=b;
beg.y=e;
beg.step=0;
vis[0][b][e]=1;
q.push(beg);
while(!q.empty())
{
end=q.top();
q.pop();
if(map[end.x][end.y]=='G')
{
printf("%d\n",end.step);
return ;
}
for(int i=0;i<4;i++)
{
beg.x=end.x+move[i][0];
beg.y=end.y+move[i][1];
if(judge(beg.x,beg.y))
{
if(map[beg.x][beg.y]!='#')
{
beg.step=end.step+1;
if(!vis[beg.step%k][beg.x][beg.y])
{
vis[beg.step%k][beg.x][beg.y]=1;
q.push(beg);
}
}
else
{
beg.step=end.step+1;
if(!vis[beg.step%k][beg.x][beg.y]&&beg.step%k==0)
{
vis[beg.step%k][beg.x][beg.y]=1;
q.push(beg);
}
}
}
}
}
printf("Please give me another chance!\n");
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(i=0;i<n;i++)
scanf("%s",map[i]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(map[i][j]=='Y')
{
b=i;
e=j;
}
}
}
bfs();
}
return 0;
}

  

hdoj 2579 Dating with girls(2)【三重数组标记去重】的更多相关文章

  1. hdu 2579 Dating with girls(2)

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

  2. 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 ...

  3. 【HDOJ】2579 Dating with girls(2)

    简单BFS. /* 2579 */ #include <iostream> #include <queue> #include <cstdio> #include ...

  4. 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 ...

  5. 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 ...

  6. 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+ ...

  7. hdu 2578 Dating with girls(1) 满足条件x+y=k的x,y有几组

    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)

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

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

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

随机推荐

  1. use worker without js file

    var blob = new Blob(['onmessage=function(e){postMessage(e.data);}']); debugger; // Obtain a blob URL ...

  2. xss攻击入门

    xss表示Cross Site Scripting(跨站脚本攻击),它与SQL注入攻击类似,SQL注入攻击中以SQL语句作为用户输入,从而达到查询/修改/删除数据的目的,而在xss攻击中,通过插入恶意 ...

  3. 关于SQL语句中SUM函数返回NULL的解决办法

    SUM 是SQL语句中的标准求和函数,如果没有符合条件的记录,那么SUM函数会返回NULL. 但多数情况下,我们希望如果没有符合条件记录的情况下,我们希望它返回0,而不是NULL,那么我们可以使用例如 ...

  4. Linux巡检

    # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # hostn ...

  5. 练习--LINUX进程间通信之信号SIGNAL

    同样的,信号也不要太迷信可靠信号及不及靠信号,实时或非实时信号. 但必须要了解这些信号之间的差异,函数升级及参数,才能熟练运用. ~~~~~~~~~~~~~~~~ 信号本质 信号是在软件层次上对中断机 ...

  6. UVALive 5903 Piece it together

    一开始用的STL一直超时不能过,后来发现AC的代码基本都用的普通邻接表,然后改了一下13s,T=T,效率太低了.然后把某大神,详情戳链接http://acm.hust.edu.cn/vjudge/pr ...

  7. js设置radio选中

    在页面数据绑定时,经常会遇到给radio设置选中,以下是我写的js方法,经测试可以使用.欢迎拍砖 <html> <head> <script type="tex ...

  8. SQLite入门与分析(六)---再谈SQLite的锁

    写在前面:SQLite封锁机制的实现需要底层文件系统的支持,不管是Linux,还是Windows,都提供了文件锁的机制,而这为SQLite提供了强大的支持.本节就来谈谈SQLite使用到的文件锁——主 ...

  9. 乱想-What&Why

    今天去海淀书城看书,目的很明确,本来是想买<WCF技术剖析>的下册(2010年3月份买了上册,当时下册没出来),谁知这本书不单卖,要和上册一起卖,扫兴. 兴致减半,索性找了旁边的基本Jav ...

  10. 学习笔记-[Maven实战]-第三章:Maven使用入门(2)

    使用maven执行编译和测试 1.maven执行编译 (1).在pom.xml上点右键,选择Maven build... (2).在Goals里输入clean complie,执行编译 执行结果: [ ...