hdu 1180(广搜好题)
诡异的楼梯
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 12487 Accepted Submission(s): 3120
比
如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.Harry发现对他来说很难找到能使得他最快到达
目的地的路线,这时Ron(Harry最好的朋友)告诉Harry正好有一个魔法道具可以帮助他寻找这样的路线,而那个魔法道具上的咒语,正是由你纂写
的.
第
一行有两个数,M和N,接下来是一个M行N列的地图,'*'表示障碍物,'.'表示走廊,'|'或者'-'表示一个楼梯,并且标明了它在一开始时所处的位
置:'|'表示的楼梯在最开始是竖直方向,'-'表示的楼梯在一开始是水平方向.地图中还有一个'S'是起点,'T'是目标,0<=M,N&
lt;=20,地图中不会出现两个相连的梯子.Harry每秒只能停留在'.'或'S'和'T'所标记的格子内.
注意:Harry只能每次走到相邻的格子而不能斜走,每移动一次恰好为一分钟,并且Harry登上楼梯并经过楼梯到达对面的整个过程只需要一分钟,Harry从来不在楼梯上停留.并且每次楼梯都恰好在Harry移动完毕以后才改变方向.
**..T
**.*.
..|..
.*.*.
S....
Hint
地图如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
typedef long long LL;
char graph[][];
bool vis[][];
struct Node
{
int x,y,step;
};
Node s,t;
int dir[][] = {{-,},{,},{,-},{,}};
int n,m;
bool check(int x,int y)
{
if(x<||x>=n||y<||y>=m||vis[x][y]||graph[x][y]=='*')
return false;
return true;
}
int bfs()
{
memset(vis,false,sizeof(vis));
queue<Node> q;
vis[s.x][s.y]=true;
q.push(s);
while(!q.empty())
{
Node node = q.front();
q.pop();
if(node.x==t.x&&node.y==t.y)
{
return node.step;
}
Node next;
for(int i=; i<; i++)
{
next.x = node.x+dir[i][];
next.y = node.y+dir[i][];
next.step=node.step+;
if(!check(next.x,next.y)) continue;
if(graph[next.x][next.y]=='.')
{
vis[next.x][next.y]=true;
q.push(next);
}
if(graph[next.x][next.y]=='|')
{
if((next.step%==)&&(i==||i==)) ///now this place change to '-'
{
next.x+=dir[i][];
next.y+=dir[i][];
if(check(next.x,next.y)){
vis[next.x][next.y]=true;
q.push(next);
}
}
else if((next.step%==)&&(i==||i==)) ///stay '|'
{
next.x+=dir[i][];
next.y+=dir[i][];
if(check(next.x,next.y)){
vis[next.x][next.y]=true;
q.push(next);
}
}else{ ///隐含条件,等下一分钟
next.x= node.x;
next.y = node.y;
q.push(next);
}
}
if(graph[next.x][next.y]=='-')
{
if((next.step%==)&&(i==||i==)) ///now this place change to '|'
{
next.x+=dir[i][];
next.y+=dir[i][];
if(check(next.x,next.y)){
vis[next.x][next.y]=true;
q.push(next);
}
}
else if((next.step%==)&&(i==||i==)) ///stay '-'
{
next.x+=dir[i][];
next.y+=dir[i][];
if(check(next.x,next.y)){
vis[next.x][next.y]=true;
q.push(next);
}
}else{ ///等下一分钟
next.x= node.x;
next.y = node.y;
q.push(next);
}
}
}
}
return -;
}
int main()
{ while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=; i<n; i++)
{
scanf("%s",graph[i]);
for(int j=; j<m; j++)
{
if(graph[i][j]=='S')
{
s.x = i,s.y = j,s.step=;
graph[i][j]='.';
}
else if(graph[i][j]=='T')
{
t.x = i,t.y = j;
graph[i][j]='.';
}
}
}
int res = bfs();
printf("%d\n",res);
}
return ;
}
hdu 1180(广搜好题)的更多相关文章
- POJ3984 BFS广搜--入门题
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20816 Accepted: 12193 Descriptio ...
- hdu 1072 广搜(逃离爆炸迷宫)
题意: 在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重启器.定时炸弹的时间是6,人走一步所需要的时间是1.每次可以上.下.左.右移动一格.当人走到4时如果炸弹的时间 ...
- HDU 2717 宽搜第一题、
题意:求n到k的最小路径, n有三种变法 n+1,n-1或者2*n: 贴个广搜的模版在这里把.... 总结一下:一般涉及到求最短路的话用宽搜 #include<iostream> #in ...
- hdu 1175(广搜)
题意:容易理解... 思路:我开始的思路不好实现,而且有漏洞,时间复杂度也高,后来在网上学了下别人的方法,真心感觉很牛B,不仅代码好实现,而且时间复杂度比较低,具体看代码实现吧!! 代码实现: #in ...
- poj 1184 广搜进阶题
起初的想法果然就是一个6000000的状态的表示. 但是后面觉得还是太过于幼稚了. 可以看看网上的解释,其实就是先转换位置,然后再改变数字的大小. #include<iostream> # ...
- hdu 1072 广搜
路径是可以重复走的,但是如果再一次走过时间重置点是没有意义的 #include <iostream> #include <cstdio> #include <cstrin ...
- hdu 2612:Find a way(经典BFS广搜题)
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- hdu 1253:胜利大逃亡(基础广搜BFS)
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
随机推荐
- 设置通过Maven创建的工程的JDK的版本,更改conf/settings.xml
eclipse提示警告如下: Build path specifies execution environment J2SE-1.5. There are no JREs installed in t ...
- Django2.0里urls.py里配置的改变
从Django2.0开始,urls.py配置方法有很大改变. 1.把url函数换成path 2.不在使用^.$作为路由 3.其他地方以后再进一步研究 下面看一个列子: from django.cont ...
- Struts2和SpringMVC简单配置以及区别总结
Struts2: struts 2 是一个基于MVC(mode-view-con)设计模式的Web应用框架,是由Struts1和WebWork两个经典框架发展而来的. 工作流程: 1客户端浏览器发出H ...
- python入门:UTF-8转换成GBK编码
#!/usr/bin/env python # -*- coding:utf-8 -*- #UTF-8转换成GBK编码 #temp(临时雇员,译音:泰坡) #decode(编码,译音:迪口德) #en ...
- TB平台搭建之一
最近在搭建公司的testbench,主要有一下总结: 1.TB主要有两部分:部分一,软件部分主要用C写的,她的作用是写硬件的驱动(其实就是让核的外围设备可以正常工作或工作到特定的环境上)甚至有可能写整 ...
- 【php】php安全问题
使用 —enable-force-cgi-redirect 选项 设置 doc_root 或 user_dir 或 open_basedir PHP运行的用户身份不能为ROOT 数据库字段加密 程序不 ...
- leetcode-12-stack
409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the le ...
- selenium2设置浏览器窗口
1.窗口最大化 //设置窗口最大化driver.manage().window().maximize(); 2.指定设置窗口大小 //指定呀设置窗口的宽度为:800,高度为600Dimension d ...
- LA 7072 Signal Interference 计算几何 圆与多边形的交
题意: 给出一个\(n\)个点的简单多边形,和两个点\(A, B\)还有一个常数\(k(0.2 \leq k < 0.8)\). 点\(P\)满足\(\left | PB \right | \l ...
- UVa 1366 DP Martian Mining
网上的题解几乎都是一样的: d(i, j, 0)表示前i行前j列,第(i, j)个格子向左运输能得到的最大值. d(i, j, 1)是第(i, j)个格子向上运输能得到的最大值. 但是有一个很关键的问 ...