Dating with girls(2)

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

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
 
思路:若重新走到某位置所需的步数模K的值不同,那么可重复进队。否则意味着进入死循环。
#include <cstdio>
#include <string.h>
#include <queue>
using namespace std;
const int MAXN=;
struct Node{
int y,x,step;
Node(){}
Node(int y,int x,int step)
{
this->y=y;
this->x=x;
this->step=step;
}
};
char mz[MAXN][MAXN];
int n,m,k;
int sy,sx;
int dy[]={,,,-};
int dx[]={,,-,};
int vis[MAXN][MAXN][MAXN];
void bfs()
{
memset(vis,,sizeof(vis));
queue<Node> que;
que.push(Node(sy,sx,));
vis[sy][sx][%k]=;
while(!que.empty())
{
Node now=que.front();que.pop();
if(mz[now.y][now.x]=='G')
{
printf("%d\n",now.step);
return ;
}
for(int i=;i<;i++)
{
int ny=now.y+dy[i];
int nx=now.x+dx[i];
int ns=now.step+;
if(<=ny&&ny<n&&<=nx&&nx<m&&!vis[ny][nx][ns%k])
{
if(mz[ny][nx]!='#')
{
vis[ny][nx][ns%k]=;
que.push(Node(ny,nx,ns));
}
else
{
if(ns%k==)
{
vis[ny][nx][ns%k]=;
que.push(Node(ny,nx,ns));
}
}
}
}
}
printf("Please give me another chance!\n");
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<n;i++)
{
scanf("%*c");
for(int j=;j<m;j++)
{
scanf("%c",&mz[i][j]);
if(mz[i][j]=='Y')
{
sy=i;
sx=j;
}
}
}
bfs();
}
return ;
}

HDU2579(bfs迷宫)的更多相关文章

  1. bfs—迷宫问题—poj3984

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20591   Accepted: 12050 http://poj ...

  2. uva 816 - Abbott&#39;s Revenge(有点困难bfs迷宫称号)

    是典型的bfs,但是,这个问题的目的在于读取条件的困难,而不是简单地推断,需要找到一种方法来读取条件.还需要想办法去推断每一点不能满足条件,继续往下走. #include<cstdio> ...

  3. BFS迷宫搜索路径

    #include<graphics.h> #include<stdlib.h> #include<conio.h> #include<time.h> # ...

  4. bfs迷宫

    链接:https://ac.nowcoder.com/acm/contest/338/BSleeping is a favorite of little bearBaby, because the w ...

  5. BFS迷宫问题

    链接:https://ac.nowcoder.com/acm/challenge/terminal来源:牛客网 小明现在在玩一个游戏,游戏来到了教学关卡,迷宫是一个N*M的矩阵. 小明的起点在地图中用 ...

  6. 【OpenJ_Bailian - 2790】迷宫(bfs)

    -->迷宫  Descriptions: 一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由n * n的格点组成,每个格点只有2种状态,.和#,前者表示可以通行后者表示不 ...

  7. ACM/ICPC 之 BFS-简单障碍迷宫问题(POJ2935)

    题目确实简单,思路很容易出来,难点在于障碍的记录,是BFS迷宫问题中很经典的题目了. POJ2935-Basic Wall Maze 题意:6*6棋盘,有三堵墙,求从给定初始点到给定终点的最短路,输出 ...

  8. (BFS)poj2935-Basic Wall Maze

    题目地址 题目与最基本的BFS迷宫的区别就是有一些障碍,可以通过建立三维数组,标记某个地方有障碍不能走.另一个点是输出路径,对此建立结构体时要建立一个pre变量,指向前一个的下标.这样回溯(方法十分经 ...

  9. 3299: [USACO2011 Open]Corn Maze玉米迷宫

    3299: [USACO2011 Open]Corn Maze玉米迷宫 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 137  Solved: 59[ ...

随机推荐

  1. MySql增加用户、授权、修改密码等语句

    1. mysql 增加新用户: insert into mysql.user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject) value ...

  2. ubuntu安装Qt5

    1.ubuntu 10.04 desktop amd64 问题: 1.1. 没有GLIBCXX_3.4.15版本,或是更高的版本 http://blog.chinaunix.net/uid-91530 ...

  3. 表单验证-JS实现

    //获取下一个span,可以通过这个对象给状态 function gspan(cobj){ while(true){ if(cobj.nextSibling.nodeName!="SPAN& ...

  4. Spring Boot入门——web相关配置

    1.Servlet 引用HttpServlet接口,采用原生的Servlet进行请求响应 2.Listener 引用ServletContextListener,常用于Web缓存 3.Filter 引 ...

  5. js 图片加载失败处理方法

    在项目中不可避免会用到图片,尤其是列表,有时候图片会加载失败:这样就会显示一个很难看的坏图片缩略图:下面介绍两种方法,解决这个问题: 1.如果在你的项目中有引入jQuery插件,你可以使用error( ...

  6. python基础2 - 运算符

    3. 运算符 3.1 算数运算符 算数运算符是 运算符的一种 是完成基本的算术运算使用的符号,用来处理四则运算 运算符 描述 实例 + 加 10 + 20 = 30 - 减 10 - 20 = -10 ...

  7. WPF绑定数据源之RelativeSource

    Command="{Binding ConfirmRegisterCommand}" CommandParameter="{Binding RelativeSource= ...

  8. selenium webdriver如何添加cookie

    一. webdriver中常用的cookie方法 webdriver中提供了操作cookie的相关方法: get_cookies()                  获得cookie信息 add_c ...

  9. 为mac终端添加tree命令

    原文:http://superuser.com/questions/359723/mac-os-x-equivalent-of-the-ubuntu-tree-command/ 整理步骤如下: $ t ...

  10. spring boot + thymeleaf 报错 org.thymeleaf.exceptions.TemplateInputException

    org.thymeleaf.exceptions.TemplateInputException: Error resolving template "admin/verifyPassword ...