Skip to main content
  1. Posts/

hdu 2531 Catch him (bfs)

·3 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

Catch him
#

**Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 664 Accepted Submission(s): 307 **

Problem Description

在美式足球中,四分卫负责指挥整只球队的进攻战术和跑位,以及给接球员传球的任务。四分卫是一只球队进攻组最重要的球员,而且一般身体都相对比较弱小,所以通常球队会安排5-7名大汉来保护他,其中站在四分卫前方、排成一线的5名球员称为进攻锋线,他们通常都是135公斤左右的壮汉。

对防守方来说,攻击对手的四分卫当然是最直接的限制对手进攻的方法。如果效果好,就可以在对方四分卫传球之前将其按翻在地,称之为擒杀。擒杀是最好的鼓舞防守队士气的方法,因为对方连传球的机会都没有,进攻就结束了,还必须倒退一些距离开球。凶狠的擒杀甚至能够将对方的四分卫弄伤,从而迫使对方更换这个进攻核心。 在本题中,输入给出准备擒杀四分卫的防守球员的位置、对方每个进攻锋线球员的位置以及对方四分卫的位置,你的任务是求出这名准备擒杀的防守球员至少要移动多少步,才能够擒杀对方四分卫。 假设对方进攻锋线和四分卫在这个过程中都不会移动。只有1名防守球员,防守球员只要碰到对方四分卫就算擒杀。 所有的球员都是一块连续的、不中空的2维区域。防守球员不可以从进攻锋线的身体上穿过,也不可以从界外穿过(只能走空地)。 防守队员不可以转动身体,只能平移。防守队员的身体所有部分向同一个方向(上、下、左、右)移动1格的过程叫做1步。

Input

输入包含多组数据。每组数据第一行都是两个整数H,W(0输入保证符合上面的条件。防守球员的身体总共不超过20格。

Output

对每组数据,输出包含擒杀所需最少步数的一行。如果不能擒杀,输出带’Impossible’的一行。

Sample Input

6 6 .Q…. QQ..OO .OO..O …O.O OO.O.. ….DD 7 7 .Q….. QQ.OOO. …O… O…… OO..OO. .O….. …..DD 0 0

Sample Output

Impossible 9

这题最开始昨天晚上看到直接懵了...

人的身体有多部分== 还不规则..怎么搞...

然后昨天睡觉的时候灵光一现(大雾

今天起来搞搞搞…

竟然1A....爽坏了

其实很简单.

就是只考虑一个点.

其他点存与最初这个点的相对位移.

然后只做这第一个点的bfs

只不过判断条件的时候要判断所有点的

以及是否摸(?)到四分卫的时候也要判断多个点的就好了.

  1/*************************************************************************
  2> File Name: code/hdu/2531.cpp
  3> Author: 111qqz
  4> Email: rkz2013@126.com
  5> Created Time: 2015年10月11日 星期日 23时14分43秒
  6************************************************************************/
  7
  8#include<iostream>
  9#include<iomanip>
 10#include<cstdio>
 11#include<algorithm>
 12#include<cmath>
 13#include<cstring>
 14#include<string>
 15#include<map>
 16#include<set>
 17#include<queue>
 18#include<vector>
 19#include<stack>
 20#include<cctype>
 21
 22#define yn hez111qqz
 23#define j1 cute111qqz
 24#define ms(a,x) memset(a,x,sizeof(a))
 25using namespace std;
 26const int dx4[4]={1,0,0,-1};
 27const int dy4[4]={0,-1,1,0};
 28typedef long long LL;
 29typedef double DB;
 30const int inf = 0x3f3f3f3f;
 31const int N=1E2+5;
 32int w,h;
 33char maze[N][N];
 34bool v[N][N];
 35int cnt ;
 36int fx[30];
 37int fy[30];
 38struct node
 39{
 40int x,y;
 41int d;
 42bool ok()
 43{
 44if (x<0||y<0||x>=w||y>=h) return false;
 45if (maze[x][y]=='O') return false;
 46if (v[x][y]) return false;
 47 47
 48for ( int i =2 ; i <= cnt ; i++)
 49{
 50int xx = x + fx[i];
 51int yy = y + fy[i];
 52if (xx<0||yy<0||xx>=w||yy>=h) return false;
 53if (maze[xx][yy]=='O') return false;
 54}
 55return true;
 56}
 57 57
 58bool get()
 59{
 60if (maze[x][y]=='Q') return true;
 61for ( int i = 2 ; i <= cnt ; i++)
 62{
 63int xx = x + fx[i];
 64int yy = y + fy[i];
 65if (maze[xx][yy]=='Q')
 66{
 67return true;
 68}
 69}
 70return false;
 71}
 72}s;
 73 73
 74bool bfs()
 75{
 76 76
 77queue<node>q;
 78q.push(s);
 79 79
 80while (!q.empty())
 81{
 82node pre = q.front();q.pop();
 83 83
 84//    printf("x: %d  y: %d d: %d n",pre.x,pre.y,pre.d);
 85
 86if (pre.get())
 87{
 88printf("%dn",pre.d);
 89return true;
 90}
 91 91
 92for ( int i = 0 ; i < 4 ; i++)
 93{
 94node next;
 95next.x = pre.x + dx4[i];
 96next.y = pre.y + dy4[i];
 97next.d = pre.d + 1;
 98if (next.ok())
 99{
100v[next.x][next.y] = true;
101q.push(next);
102}
103}
104}
105return false;
106
107}
108int main()
109{
110#ifndef  ONLINE_JUDGE
111freopen("in.txt","r",stdin);
112#endif
113
114while (scanf("%d %d",&w;,&h;)!=EOF&&w;&&h)
115{
116ms(v,false);
117cnt = 0 ;
118for ( int i = 0 ; i < w ; i++) scanf("%s",maze[i]);
119
120for ( int i = 0 ; i < w ; i++)
121for ( int j = 0 ; j < h ; j++)
122{
123if (maze[i][j]=='D')
124{
125cnt++;
126if (cnt==1)
127{
128fx[cnt] =  i;
129fy[cnt] =  j;
130s.x = i;
131s.y = j;
132s.d = 0 ;
133v[i][j] = true;
134}
135else
136{
137fx[cnt] = i - fx[1]; //相对位移.
138fy[cnt] = j - fy[1];
139}
140}
141}
142
143if (!bfs())
144{
145puts("Impossible");
146}
147}
148
149
150
151#ifndef ONLINE_JUDGE
152fclose(stdin);
153#endif
154return 0;
155}

Related

codeforces #324 div 2 A. Olesya and Rodion

·1 min
A. Olesya and Rodion time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Olesya loves numbers consisting of n digits, and Rodion only likes numbers that are divisible by t. Find some number that satisfies both of them.

51nod_learn_greedy_活动安排2

·2 mins
有若干个活动,第i个开始时间和结束时间是[Si,fi),同一个教室安排的活动之间不能交叠,求要安排所有活动,最少需要几个教室?