HDU1180:诡异的楼梯
传送门
题意
迷宫搜索
分析
这题写起来挺简单的,锻炼搜索基本功,一开始用记忆化搜索TLE了,改用访问标记,0ms过了,用优先队列保证终点最快达到,我会在代码中提供一些强力数据
trick
1.遇到梯子分能过(+1s)与不能过(+2s)入队列
2.一定有可行解
代码
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define ll long long
#define F(i,a,b) for(int i=a;i<=b;++i)
#define R(i,a,b) for(int i=a;i<b;++i)
#define mem(a,b) memset(a,b,sizeof(a))
int m,n,sx,sy,ex,ey,ans;
char s[25];
int mp[25][25],vis[25][25];
struct node
{
int x,y,time;
bool operator<(const node &p)const
{
return time>p.time;
}
};
int a[4][2]={0,1,1,0,0,-1,-1,0};
priority_queue<node>pq;
bool check(int x,int y)
{
if(x<1||y<1||x>m||y>n||vis[x][y]||mp[x][y]==0) return 0;return 1;
}
void bfs()
{
node tmp;
tmp.x=sx,tmp.y=sy,tmp.time=0;
while(!pq.empty()) pq.pop();
pq.push(tmp);
ans=0;mem(vis,0);
vis[sx][sy]=1;
while(!pq.empty())
{
tmp=pq.top();pq.pop();
if(tmp.x==ex&&tmp.y==ey) { ans=tmp.time;return ; }
R(i,0,4)
{
int xx=tmp.x+a[i][0],yy=tmp.y+a[i][1];
if(mp[xx][yy]==2||mp[xx][yy]==3)
{
if(mp[xx][yy]==2)
{
if((tmp.time+i)&1)
{
if(!check(xx+a[i][0],yy+a[i][1])) continue;
node p={xx+a[i][0],yy+a[i][1],tmp.time+1};
pq.push(p);vis[p.x][p.y]=1;
continue;
}
else
{
if(!check(xx+a[i][0],yy+a[i][1])) continue;
node p={xx+a[i][0],yy+a[i][1],tmp.time+2};
pq.push(p);vis[p.x][p.y]=1; continue;
}
}
else
{
if((tmp.time+i)%2==0)
{
if(!check(xx+a[i][0],yy+a[i][1])) continue;
node p={xx+a[i][0],yy+a[i][1],tmp.time+1};
pq.push(p);vis[p.x][p.y]=1; continue;
}
else
{
if(!check(xx+a[i][0],yy+a[i][1])) continue;
node p={xx+a[i][0],yy+a[i][1],tmp.time+2};
pq.push(p);vis[p.x][p.y]=1; continue;
}
}
}
else
{
if(!check(xx,yy)) continue;
node p={xx,yy,tmp.time+1};
vis[p.x][p.y]=1;
pq.push(p);
}
}
}
}
int main()
{
while(scanf("%d %d",&m,&n)!=EOF)
{
F(i,1,m)
{
scanf("%s",s);
F(j,0,n-1)
{
if(s[j]=='S') sx=i,sy=j+1;
if(s[j]=='T') ex=i,ey=j+1;
mp[i][j+1]=1;
if(s[j]=='*') mp[i][j+1]=0;
if(s[j]=='|') mp[i][j+1]=2;
if(s[j]=='-') mp[i][j+1]=3;
}
}
bfs();
printf("%d\n",ans);
}
return 0;
}
/*
附上几组测试数据
5 5
**..T
**.*.
**|..
**.**
S..**
5 5
**..T
**.*.
**-..
**.**
S..**
5 5
.|.-T
-*-*|
.*.|.
-*-**
S|.**
5 5
S....
-|-|-
.....
-|-|-
....T
1 3
S-T
1 3
S|T
1 5
S|.|T
1 5
S-.-T
1 5
S|.-T
1 5
S-.|T
答案是:
8 7 7 8 1 2 4 3 3 2
*/
HDU1180:诡异的楼梯的更多相关文章
- HDU1180:诡异的楼梯(bfs+优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=1180 Problem Description Hogwarts正式开学以后,Harry发现在Hogwarts里, ...
- hdu1180 诡异的楼梯 bfs
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1180/ 题目和不同的bfs有个不同的地方就是存在横着的或者竖着的楼梯,楼梯每过一个时刻就改变一次横竖的走向,人可 ...
- 诡异的楼梯(bfs)hdu1180
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submiss ...
- hdu 1180 诡异的楼梯
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Subm ...
- hdu 1180 诡异的楼梯 (bfs)
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Sub ...
- HDU 1180 诡异的楼梯(BFS)
诡异的楼梯 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- hdu 1180诡异的楼梯(bfs)
诡异的楼梯 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total Submis ...
- hdu 1180:诡异的楼梯(BFS广搜)
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Subm ...
- HDU 1180 诡异的楼梯(超级经典的bfs之一,需多回顾)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1180 诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- make only output error/warning message( 编译时,只输出错误信息和警告信息)
make > /dev/null 这样,正常的信息被重定向输出到/dev/null,错误和警告信息会输出到标准错误设备(standard error,相对于标准输入/输出设备来说).
- Linux中的进程与线程
介绍了Linux下fork()创建进程以及使用pthread_create()创建线程的方法 1. 基于进程的斐波那契数列 在下面的代码中,由子进程进行斐波那契数列的输出,父进程要等待子进程输出完毕, ...
- django学习之- CSRF及中间件
CSRF # 表示django全局发送post请求均需要字符串验证功能:防止跨站请求伪造的功能工作原理:客户端访问服务器端,在服务器端正常返回给客户端数据的时候,而外返回给客户端一段字符串,等到客户端 ...
- Codeforces Manthan, Codefest 18 (rated, Div. 1 + Div. 2) D,E
D. Valid BFS? time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Cooking Schedule Problem Code: SCHEDULE(优先队列)
Cooking Schedule Problem Code: SCHEDULE Chef is a well-known chef, and everyone wishes to taste his ...
- CodeForces 598C Nearest vectors
这题对精度要求很高.用atan2吧... #include<iostream> #include<cstring> #include<cmath> #include ...
- google --SwitchyOmega and switchysharp ***
https://github.com/FelisCatus https://chrome.google.com/webstore/search/Proxy%20SwitchySharp%20?hl=z ...
- Could not find leader nimbus
运行storm ui, 然后访问storm ui 的网页的时候,死活跑不起来.后面,根据下面这篇文章的说法, 停止zookeeper 之后,删掉zookeeper 上面的storm 节点, 然后再重启 ...
- [RxJS] Chain RxJS Operators Together with a Custom `pipe` Function using Array.reduce
Instead of writing complex operators, it's usually best to write simple, single-purpose operators th ...
- leetcode:238. Product of Array Except Self(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...