HDU 2531 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531
题目大意: 你的身体占据多个点。每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最少步数。
解题思路:
挺有趣的一个题,每次要移动多个点。
如果只移动一个点,就是个简单粗暴的BFS。
多个点照样处理,读图的时候把扫到的第一个点当作移动点,然后vector记录下身体的其它点与该移动点的相对坐标。
BFS的时候,先看看移动点能不能动,然后再根据身体的相对坐标还原出身体的绝对坐标,看看有没有越界或是撞到障碍。
顺便检测一下是否撞到目标点。
#include "cstdio"
#include "iostream"
#include "cstring"
#include "queue"
#include "string"
#include "vector"
using namespace std;
struct status
{
int x,y,dep;
status(int x,int y,int dep):x(x),y(y),dep(dep) {}
};
char map[][];
int n,m,sx,sy,vis[][],dir[][]={-,,,,,-,,},ans;
vector<status> body;
void bfs(int sx,int sy)
{
queue<status> Q;
Q.push(status(sx,sy,));
vis[sx][sy]=true;
bool flag=false;
while(!Q.empty())
{
if(flag) break;
status t=Q.front();Q.pop();
//cout<<map[t.x][t.y]<<endl;
for(int s=;s<;s++)
{
int X=t.x+dir[s][],Y=t.y+dir[s][];
if(vis[X][Y]||X<||X>n||Y<||Y>m||map[X][Y]=='O') continue;
bool ok=true,get=false;
for(int k=;k<body.size();k++)
{
int bx=X-body[k].x,by=Y-body[k].y;
if(bx<||bx>n||by<||by>m||map[bx][by]=='O') {ok=false;break;}
if(map[bx][by]=='Q') get=true;
}
if(!ok) continue;
vis[X][Y]=true;
if(get||map[X][Y]=='Q') {flag=true;ans=min(ans,t.dep+);break;}
Q.push(status(X,Y,t.dep+));
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
string tt;
while(cin>>n>>m&&n)
{
body.clear();
memset(vis,,sizeof(vis));
bool first=false;ans=<<;
for(int i=;i<=n;i++)
{
cin>>tt;
for(int j=;j<tt.size();j++)
{
map[i][j+]=tt[j];
if(tt[j]=='D')
{
if(!first) {sx=i;sy=j+;first=true;}
else {body.push_back(status(sx-i,sy-(j+),));}
}
}
}
bfs(sx,sy);
if(ans==<<) cout<<"Impossible"<<endl;
else cout<<ans<<endl;
}
}
11882814 | 2014-10-16 01:36:06 | Accepted | 2531 | 15MS | 352K | 1971 B | C++ | Physcal |
HDU 2531 (BFS搜索)的更多相关文章
- HDU 1180 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...
- HDU 1026 (BFS搜索+优先队列+记录方案)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...
- HDU 1312 (BFS搜索模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...
- HDU 1242 (BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...
- HDU 2612 (BFS搜索+多终点)
题目链接: http://poj.org/problem?id=1947 题目大意:两人选择图中一个kfc约会.问两人到达时间之和的最小值. 解题思路: 对于一个KFC,两人的BFS目标必须一致. 于 ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU 4499.Cannon 搜索
Cannon Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...
- hdu 4294(bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4294 思路:题目的意思是说,给你n,k,则求出n的正整数倍数,使得这个数字在k进制下表示的时候需要的不 ...
随机推荐
- sqlmap如何修改线程
找到settings.py文件,具体在\lib\core\目录下找到 # Maximum number of threads (avoiding connection issues and/or Do ...
- TortoiseSVN中图标的含义
今天在使用svn时发现有好多不认识了,所以查了下svn帮助手册.借此总结了下 svn 中图标的含义 一个新检出的工作复本使用绿色的勾做重载.表示Subversion状态 正常. 在开始编辑一个文件后, ...
- TFS增加dataserver
通过之前的努力,已经搭建好了一套基本的tfs环境,包括一台nameserver和一台dataserver以及独立的nginx-tfs,而在实际应用中的分布式文件系统,只有一台dataserver明显是 ...
- petri网学习心得
本文转载自duxw,如给您带来不便之处,请联系博主. 1.Petri网书籍:<petri网导论>,吴哲辉 非常适合初学者.概念清晰,容易理解. 2.工作流书籍:<Workflow M ...
- Java for LeetCode 076 Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- Light OJ 1253 Misere Nim (尼姆博弈(2))
LightOJ1253 :Misere Nim 时间限制:1000MS 内存限制:32768KByte 64位IO格式:%lld & %llu 描述 Alice and Bob ar ...
- easyfinding(codevs 3280)
3280 easyfinding 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 给一个M 行N 列 ...
- SL4A
参考文章:http://my.oschina.net/u/1468102/blog/208687 如何安装使用SL4A http://www.ibm.com/developerworks/cn/mob ...
- [VSTS]让ADO.NET Entity Framework支持Oracle数据库(转载)
近期由于项目所需不得不研究Oracle数据库,回想上一次用Oracle还是07年的事情,实习时候做华晨宝马的项目简单接触了Oracle.这次的项目需要基于.NET平台,我个人的习惯是能用微软自带的就不 ...
- WPF中的常用类汇总:
1.FrameworkElement: WPF中大部分的控件都可以转化成FrameworkElement,利用FrameworkElement属性获取相应的值: 2.WPF获取当前工作区域的宽度和高度 ...