HDU 2364 (记忆化BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2364
题目大意:走迷宫。从某个方向进入某点,优先走左或是右。如果左右都走不通,再考虑向前。绝对不能往后走,即使没走过。
解题思路:
还是一个关键:
每个点可以最多可以走4遍。可以从4个方向到达这个点。所以vis第三维要记录走的方向。
辅助一个route数组,用于当前方向时,应该走相对于正北坐标系的方向(即人看地图的上下左右)。
这样就算迷宫中人的方向不同,但是我们给他标记的数却是统一的,都是以他的方向,先左后右再直走。
如果相对于这个人方向的左右能走,要及时标记,不要再直走了。
最后,边界是指1,n,m,不要把1漏掉。
同时如果选择在Push之前判断是否达终点的话(这种方式可以在找到结果后及时剪枝,比较快)记得再bfs之前特判一下出发点是否已经符合要求。
因为这种方式是不会考虑出发点的。
#include "cstdio"
#include "cstring"
#include "string"
#include "iostream"
#include "queue"
using namespace std;
char map[][];
int T,n,m,sx,sy,vis[][][],dir[][]={-,,,,,-,,},route[][]={,,,,,,,,,,,,,,,};
struct status
{
int x,y,dep,dir;
status(int x,int y,int dep,int dir):x(x),y(y),dep(dep),dir(dir) {}
};
int bfs(int x,int y)
{
queue<status> Q;
for(int i=;i<;i++)
{
vis[x][y][i]=true;
Q.push(status(x,y,,i));
}
while(!Q.empty())
{
status t=Q.front();Q.pop();
bool direct=true;
for(int s=;s<;s++)
{
if(s>=&&!direct) break;
int X=t.x+dir[route[t.dir][s]][],Y=t.y+dir[route[t.dir][s]][];
if(X<||X>n||Y<||Y>m||map[X][Y]=='#') continue;
if(s==||s==) direct=false;
if(vis[X][Y][route[t.dir][s]]) continue;
vis[X][Y][route[t.dir][s]]=true;
if(X==||Y==||X==n||Y==m) {return t.dep+;}
Q.push(status(X,Y,t.dep+,route[t.dir][s]));
}
}
return -;
}
int main()
{
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
cin>>T;
string tt;
while(T--)
{
memset(vis,,sizeof(vis));
cin>>n>>m;
for(int i=;i<=n;i++)
{
cin>>tt;
for(int j=;j<tt.size();j++)
{
map[i][j+]=tt[j];
if(tt[j]=='@') {sx=i;sy=j+;}
}
}
if(sx==||sx==n||sy==||sy==m) cout<<""<<endl;
else cout<<bfs(sx,sy)<<endl;
}
}
| 11903906 | 2014-10-18 17:10:04 | Accepted | 2364 | 0MS | 428K | 1648 B | C++ | Physcal |
HDU 2364 (记忆化BFS搜索)的更多相关文章
- HDU 2579 (记忆化BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2579 题目大意:走迷宫.对于障碍点,只有当前(dep+1)%k才能走,问最少时间. 解题思路: 只有 ...
- HDU 2653 (记忆化BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2653 题目大意:迷宫中有普通点和陷阱.其中普通点可以走可以飞,但是陷阱只能飞.走耗时1,飞耗时2.但 ...
- HDU 1072(记忆化BFS)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...
- hdu 4722(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 思路:简单的记忆化搜索,留意一下A==0时的情况就可以了. #include<iostre ...
- HDU 4597 记忆化搜索
² 博弈取牌—记忆化搜索 题目描述: 有两副带有数字的牌,(数字>0)两人轮流取,取中了某张牌,自己的分数就加上牌上的数字,但只能从两端取,每人都会用最优的策略使得自己的分数最高.问A先取,他能 ...
- hdu 1514 记忆化搜索
题意是给4堆(堆的高度小于等于40)有颜色(颜色的种类小于等于20)的物品,你有一个篮子最多能装5件物品,每次从这4堆物品里面任取一件物品放进篮子里,但是取每堆物品时,必须先取上面的物品,才能取下面的 ...
- hdu 1208 记忆化搜索
题目大意:只能按照格子上的数字*方向走,从左上走到右下Sample Input42331121312313110Sample Output3 直接记忆化搜索,注意是0的情况 #include<c ...
- hdu 4960 记忆化搜索 DP
Another OCD Patient Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Ot ...
- hdu 2089 记忆化搜索写法(数位dp)
/* 记忆化搜索,第二维判断是否是6 */ #include<stdio.h> #include<string.h> #define N 9 int dp[N][2],digi ...
随机推荐
- ruby : Exception Notification
https://github.com/smartinez87/exception_notification#sections Add the following line to your applic ...
- 在线调试和演示的前端开发工具------http://jsfiddle.net/
在线调试和演示的前端开发工具------http://jsfiddle.net/
- First Missing Positive
不好想,用桶排序解决. int findMissingPostive(int A[], int n) { bucket_sort(A, n); ; i < n; i++) ) ; ; } voi ...
- tomcat配置文件之Server.xml
Server.xml包含的元素有<Server>.<Service>.<Connector>.<Engine>.<Host>.<Con ...
- error: library dfftpack has Fortran sources but no Fortran compiler found解决方法
用pip install scipy 时提示 error: library dfftpack has Fortran sources but no Fortran compiler found 解决方 ...
- linux使用技巧
<1>vim /etc/hosts.deny sshd : 192.168.0.25 :deny //ssh拒绝某ip或网段访问.(原理详见鸟哥基础版18章P56 ...
- Solr DIH导入出现 Data Config problem: 前言中不允许有内容 异常
Solr配置DIH导入时出现 “Data Config problem: 前言中不允许有内容.” 异常. <response> <lst name="responseHea ...
- Objective-C 和 C++中指针的格式和.方法 和内存分配
最近在看cocos2d-x,于是打算复习一下C++,在这里简单对比下,留个念想. 先看看oc中指针的用法 @interface ViewController : UIViewController { ...
- Flash Player 19.0.0.124 Beta + IHTMLDocument3 IHTMLDocument2 ->get_innerHTML
安装 Flash Player 19 之后 有 flash 动画的网页中 IHTMLDocument3 IHTMLDocument2 ->get_innerHTML 获取的 html 内容都是空 ...
- Java for LeetCode 191 Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...