HDU 2531 Catch him

http://acm.hdu.edu.cn/showproblem.php?pid=2531

题意:橄榄球诶、、、超喜欢四分卫的说。好吧,题目其实很简单,BFS搜索,不过一开始没看懂吗,为什么有多个D和Q,蛋疼过后才发现那是球员的身体的一部分。ORZ


嗯,我是把防守队员的身体保存在一个结构体里面,每次判断是否身体会触碰O即可。

/*
*     Danceonly
*/

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long LL;

#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)


struct node
{
      int x;
      int y;
}body[30];
struct abc
{
      int x,y;
      int step;
}q[100005],e;
int maze[105][105],vis[105][105];
int dir[4][2] = {1,0,0,1,-1,0,0,-1};
int N,M,L,sx,sy;
bool Isok(int x,int y)
{
      for (int i=0;i<L;i++)
      {
            int xx = x + body[i].x;
            int yy = y + body[i].y;
            if (maze[xx][yy] == 0)return 0;
      }
      return 1;
}
void gogo(int x,int y)
{
      for (int i=0;i<L;i++)
      {
            body[i].x += x;
            body[i].y += y;
      }
      return ;
}
bool Isend(int x,int y)
{
      for (int i=0;i<L;i++)
      {
            if (maze[body[i].x + x][body[i].y + y] == 2)return 1;
      }
      return 0;
}
int BFS()
{
      int tail,head,x,y,step;
      tail = head = 0;
      q[head].x = sx;
      q[head].y = sy;
      q[head].step = 0;
      vis[sx][sy] = 1;
      while (head <= tail)
      {
            x = q[head].x;
            y = q[head].y;
            step = q[head].step;
            for (int i=0;i<4;i++)
            {
                  e.x = x + dir[i][0];
                  e.y = y + dir[i][1];
                  if (vis[e.x][e.y] || Isok(e.x-sx,e.y-sy) == 0)continue ;
                  //gogo(e.x-sx,e.y-sy);
                  e.step = step + 1;
                  if (Isend(e.x-sx,e.y-sy))
                  {
                        return e.step;
                  }
                  vis[e.x][e.y] = 1;
                  q[++tail] = e;
            }
            head ++;
      }
      return -1;
}
void show()
{
      for (int i=1;i<=N;i++)
      {
            for (int j=1;j<=M;j++)
                  printf("%d ",maze[i][j]);
            printf("\n");
      }
}
int main()
{
      while (scanf("%d%d",&N,&M) == 2 && (N || M))
      {
            memset(vis,0,sizeof(vis));
            memset(maze,0,sizeof(maze));
            int c = 0;
            for (int i=1;i<=N;i++)
            {
                  char ss[105];
                  scanf("%s",ss);
                  for (int j=0;j<M;j++)
                        if (ss[j] == '.')maze[i][j+1] = 1;//could go
                        else if (ss[j] == 'D'){maze[i][j+1] = 1;body[c].x = i;body[c++].y = j+1;}
                        else if (ss[j] == 'Q')maze[i][j+1] = 2;//It is the end.
            }

            //show();
            sx = body[0].x;sy = body[0].y;
            L = c;
            //printf("%d\n",L);
            int ans = BFS();
            if (ans == -1)printf("Impossible\n");
            else printf("%d\n",ans);
      }
      return 0;
}

/*
6 6
.Q....
QQ..OO
.OO..O
...O.O
OO.O..
....DD
*/


posted @ 2013-07-29 16:25  cnwsycf  阅读(94)  评论(0编辑  收藏  举报