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
 
Recommend
lcy
 
每秒移动一格, 只能上下左右移动
这道题是一个搜索的题目, 因为要求到达的最短时间, 用BFS会更好一些,
需要注意的一点是, 石头会在某个时间消失, 这样的话, 不仅在搜索时需要结合当前时间判断此地块是否有石头, 路径判重时也要注意
普通的搜索题目判重时是不能重复经过同一地块(会导致状态相同,从而重复搜索),  而这道题目中两个状态相同是指在同一地块的前提下, 距离下一次石头消失的时间相同. 因此需要用一个三维数组, vis[MAXN][MAXN][K_MAXN] 标记状态
代码:
 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int n,m,k,sx,sy,ex,ey;
struct haha
{
int x, y, step;
}q,temp;
char map[][];
int dir[][]={,,,-,,,-,};
int vis[][][];
void BFS()
{
int i;
q.x=sx;
q.y=sy;
q.step=;
queue<struct haha>que;
memset(vis,,sizeof(vis));
vis[sx][sy][]=;
que.push(q);
while(!que.empty())
{
temp=que.front();
que.pop();
if(temp.x==ex&&temp.y==ey)
{
printf("%d\n",temp.step);
return ;
}
for(i=;i<;i++)
{
int xx,yy;
xx=temp.x+dir[i][];
yy=temp.y+dir[i][];
if(xx>=&&xx<n&&yy>=&&yy<m&&(map[xx][yy]!='#'||(temp.step+)%k==)&&!vis[xx][yy][(temp.step+)%k])
{
q.step=temp.step+;
q.x=xx;
q.y=yy;
vis[xx][yy][(temp.step+)%k]=;
que.push(q);
}
}
}
printf("Please give me another chance!\n");
}
int main()
{
int i,j,cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%d %d %d",&n,&m,&k);
for(i=;i<n;i++)
{
scanf("%s",map[i]);
for(j=;j<m;j++)
{
if(map[i][j]=='Y') {sx=i;sy=j;}
else if(map[i][j]=='G') {ex=i;ey=j;}
}
}
BFS();
}
return ;
}

HDU2579--Dating with girls(2)--(DFS, 判重)的更多相关文章

  1. UVa 10400 - Game Show Math 游戏中的数学 dfs+判重

    题意:给出一些数字和一个目标数字,要求你在数字间添加+-*/,让表达式能达到目标数字,运算符号的优先级都是一样的. 由于数据量很大,本来想用map<string>判重的,结果还是超时了,然 ...

  2. POJ 2458 DFS+判重

    题意: 思路: 搜+判重 嗯搞定 (听说有好多人用7个for写得-.) //By SiriusRen #include <bitset> #include <cstdio>0 ...

  3. 【DFS+小操作判重】【HDU2610+HDU2611】Sequence

    题意 2610 按照长度优先 位置次之 输出所有不递减序列 2611 按照长度优先 大小次之 输出所有不递减序列 题解不写了 来源于http://www.cnblogs.com/wally/archi ...

  4. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  5. hdu 2610 2611 dfs的判重技巧

    对于全排列枚举的数列的判重技巧 1:如果查找的是第一个元素 那么 从0开始到当前的位置看有没有出现过这个元素 出现过就pass 2: 如果查找的不是第一个元素 那么 从查找的子序列当前位置的前一个元素 ...

  6. 八数码问题+路径寻找问题+bfs(隐式图的判重操作)

    Δ路径寻找问题可以归结为隐式图的遍历,它的任务是找到一条凑够初始状态到终止问题的最优路径, 而不是像回溯法那样找到一个符合某些要求的解. 八数码问题就是路径查找问题背景下的经典训练题目. 程序框架 p ...

  7. poj 3131 双向搜索+hash判重

    题意: 初始状态固定(朝上的全是W,空格位置输入给出),输入初始状态的空格位置,和最终状态朝上的位置,输出要多少步才能移动到,超过30步输出-1. 简析: 每一个格子有6种状态,分别是 0WRB, 1 ...

  8. POJ1011【判重剪枝】

    题意: 给你一堆棒子,这些棒子是你从一堆一样的棒子折断而来的, 现在你忘记了是从那一堆一样的棒子的长度,让你写一个程序,求最短的长度. 思路: 首先这个棒长肯定是和的约数,且大于最大值. 然后是sor ...

  9. BFS+Hash(储存,判重) HDOJ 1067 Gap

    题目传送门 题意:一个图按照变成指定的图,问最少操作步数 分析:状态转移简单,主要是在图的存储以及判重问题,原来队列里装二维数组内存也可以,判重用神奇的hash技术 #include <bits ...

随机推荐

  1. Openjudge-计算概论(A)-计算三角形面积

    描述: 平面上有一个三角形,它的三个顶点坐标分别为(x1, y1), (x2, y2), (x3, y3),那么请问这个三角形的面积是多少. 输入输入仅一行,包括6个单精度浮点数,分别对应x1, y1 ...

  2. 高性能MySQL第2,3章性能相关 回顾笔记

    1.  基准测试(benchmark)   不管是新手还是专家都要熟悉基准测试,benchmark测试是对系统的一种压力测试,目标是为了掌握在特定压力下系统的行为.也有其他原因:如重现系统状态,或者是 ...

  3. linux配置永久ip不生效解决方法

    本文原文来自 http://blog.csdn.net/zymx14/article/details/51472239 linux下使用ifconfig eth0 ip地址可以设置ip地址 ,命令为: ...

  4. ios压缩图片

    /** *  压缩图片到指定文件大小 * *  @param image 目标图片 *  @param size  目标大小(最大值) * *  @return 返回的图片文件 */ - (NSDat ...

  5. 2.Thread中的实例方法

    (转自:http://www.cnblogs.com/xrq730/p/4851233.html) Thread类中的方法调用方式: 1.this.XXX 这种调用方式表示的线程是:线程实例本身 2. ...

  6. POJ1613 147/思维题

    题目链接[https://www.oj.swust.edu.cn/problem/show/1613] 题意:输出第K小的由1.4.7数字组成的数字. 解题过程:1.4.7.11.14.17.41.4 ...

  7. python中telnetlib模块的使用

    一.Windows下开启Telnet服务 (详见:与Win7防火墙无缝结合 Telnet功能测试) 1.Windows 2000/XP/2003/Vista:默认已安装但禁止了Telnet服务 (1) ...

  8. aspx中如何加入javascript

    Response.Write("<script>window.location ='aaa.aspx';</script>"); Response.Writ ...

  9. Handling Captcha | Webdriver

    http://seleniumworks.blogspot.kr/2013/09/handling-captcha-webdriver.html Make use of the 'input' tag ...

  10. kindle使用参考

    转载链接:http://blog.sina.com.cn/nuanfengjia 今天买的kindle499刚刚到货了,体验略差,还有一个就是无按键,完全不会玩,只能自己慢慢摸索了. [新Kindle ...