题目链接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搜索)的更多相关文章

  1. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  2. HDU 1026 (BFS搜索+优先队列+记录方案)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...

  3. HDU 1312 (BFS搜索模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...

  4. HDU 1242 (BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...

  5. HDU 2612 (BFS搜索+多终点)

    题目链接: http://poj.org/problem?id=1947 题目大意:两人选择图中一个kfc约会.问两人到达时间之和的最小值. 解题思路: 对于一个KFC,两人的BFS目标必须一致. 于 ...

  6. hdu 1240:Asteroids!(三维BFS搜索)

    Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  7. 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 ...

  8. HDU 4499.Cannon 搜索

    Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  9. hdu 4294(bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4294 思路:题目的意思是说,给你n,k,则求出n的正整数倍数,使得这个数字在k进制下表示的时候需要的不 ...

随机推荐

  1. [BZOJ1786][BZOJ1831]逆序对

    [BZOJ1786][BZOJ1831]逆序对 试题描述 输入 输出 输入示例 - - 输出示例 数据规模及约定 见“输入” 题解 首先这题有一个性质,即,填的数从左到右一定不降.证明不妨读者自己yy ...

  2. MySQL报错:Packets larger than max_allowed_packet are not allowed 的解决方案

    在导大容量数据特别是CLOB数据时,可能会出现异常:“Packets larger than max_allowed_packet are not allowed”. 这是由于MySQL数据库有一个系 ...

  3. 2.7 编程之美--最大公约数的3种解法[efficient method to solve gcd problem]

    [本文链接] http://www.cnblogs.com/hellogiser/p/efficient-method-to-solve-gcd-problem.html [题目] 求两个正整数的最大 ...

  4. Android 使用dip单位进行布局的一点知识

    先看看怎么算出一个设备的dpi, 其实就是算出对角线上有多少个px,之后除上屏幕尺寸.比如,1280*720 的10.1寸设备,dpi = (sqrt(1280*1280+720*720))  / 1 ...

  5. Android 中的Resource

    Android与ios相比,各种各样Resource算个独特之处.详情请参见官网Resource Types Resource有许多种,常见的有图像资源,布局资源,等等.每一种资源的位置都是固定的,这 ...

  6. Java for LeetCode 203 Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  7. mybatis异常:Improper inline parameter map format. Should be: #{propName,attr1=val1,attr2=val2}问题分析及解决

    转载自:http://blog.csdn.net/jackpk/article/details/44158701 mybatis异常:Improper inline parameter map for ...

  8. HDU1568斐波那契推理

    Fibonacci Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. HDU1286新朋友欧拉函数版

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  10. [MAC] mac系统如何截图

    mac自带截图工具,因此不需要安装任何第三方软件,便可以实现屏幕截图,截图的方法有若干种,下面介绍最简单的方法:通过快捷键进行截图: 全屏截图: 同时按住键盘左下方的  command   和   s ...