POJ 2688 广搜+深搜 BFS + DFS

Cleaning Robot (POJ 2688)

Time Limit: 1000MS Memory Limit: 65536K

Description

Here, we want to solve path planning for a mobile robot cleaning a rectangular room floor with furniture.

Consider the room floor paved with square tiles whose size fits the cleaning robot (1 * 1). There are 'clean tiles' and 'dirty tiles', and the robot can change a 'dirty tile' to a 'clean tile' by visiting the tile. Also there may be some obstacles (furniture) whose size fits a tile in the room. If there is an obstacle on a tile, the robot cannot visit it. The robot moves to an adjacent tile with one move. The tile onto which the robot moves must be one of four tiles (i.e., east, west, north or south) adjacent to the tile where the robot is present. The robot may visit a tile twice or more.

Your task is to write a program which computes the minimum number of moves for the robot to change all 'dirty tiles' to 'clean tiles', if ever possible.

Input

The input consists of multiple maps, each representing the size and arrangement of the room. A map is given in the following format.

w h
c11 c12 c13 ... c1w
c21 c22 c23 ... c2w
...
ch1 ch2 ch3 ... chw

The integers w and h are the lengths of the two sides of the floor of the room in terms of widths of floor tiles. w and h are less than or equal to 20. The character cyx represents what is initially on the tile with coordinates (x, y) as follows.

'.' : a clean tile
'*' : a dirty tile
'x' : a piece of furniture (obstacle)
'o' : the robot (initial position)

In the map the number of 'dirty tiles' does not exceed 10. There is only one 'robot'.

The end of the input is indicated by a line containing two zeros.

Output

For each map, your program should output a line containing the minimum number of moves. If the map includes 'dirty tiles' which the robot cannot reach, your program should output -1.

C语言代码:


#include <stdio.h>
#include <memory.h>
#define MAXN 20
typedef struct Node
{
  int step;
  int x,y;
}Node;
Node stack[10000],u;

typedef struct Point
{
  int x,y;
}Point;

Point point[15];

int min,sum;
int dist[15][15];
char map[MAXN][MAXN];
char grid[MAXN][MAXN];
int dx[] = {-1,0,0,1};
int dy[] = {0,1,-1,0};
int m,n;
int BFS(Point start, Point end)
{
  int sx = start.x;
  int sy = start.y;
  int ex = end.x;
  int ey = end.y;
  int head,tail,i,j,k;
  int flag = 0;

  for (k = 0; k < n; k ++)
  {
    for (j = 0; j < m; j ++)
    {
      map[k][j] = grid[k][j];
    }
  }

  map[sx][sy] = 'x';
  stack[0].x = sx;
  stack[0].y = sy;
  stack[0].step = 0;
  head = 0;
  tail = 1;

  while (head < tail)
  {
    u = stack[head ++];
    if (u.x == ex && u.y == ey)
    {
      flag = 1;
      break;
    }
    for (i = 0; i < 4; i ++)
    {
      if (u.x + dx[i] < n && u.x + dx[i] >= 0 && u.y + dy[i] < m && u.y + dy[i] >= 0 && map[u.x + dx[i]][u.y + dy[i]] != 'x')
      {
        stack[tail].x = u.x + dx[i];
        stack[tail].y = u.y + dy[i];
        stack[tail].step = u.step + 1;
        map[u.x + dx[i]][u.y + dy[i]] = 'x';
        tail ++;
      }
    }
  }
  if (flag)
    return u.step;
  else
    return -1;
}

int visited[15];
void DFS(int num,int add,int cur)
{
  int i;
  if (num == sum - 1)
  {
    if (add < min || min < 0)
    {
      min = add;
    }
  }
  else
  {
    for (i = 1; i < sum; i ++)
    {
      if ((add < min || min < 0) && !visited[i])
      {
        visited[i] = 1;
        DFS(num + 1,add + dist[cur][i],i);
      }
    }
  }
  visited[cur] = 0;
}
int main()
{
  int i,j,k;
  int head,tail;
  int flag;
  while (scanf("%d%d",&m,&n),m,n)
  {
    getchar();
    sum = 1;
    for (i = 0; i < n; i ++)
    {
      for (j = 0; j < m; j ++)
      {
        scanf("%c",&grid[i][j]);
        if (grid[i][j] == 'o')
        {
          point[0].x = i;
          point[0].y = j;
        }
        if (grid[i][j] == '*')
        {
          point[sum].x = i;
          point[sum].y = j;
          sum ++;
        }
      }
      getchar();
    }
    flag = 1;
    memset(dist,-1,sizeof(dist));
    for (i = 0; i < sum; i ++)
    {
      for (j = i; j < sum; j ++)
      {
        dist[i][j] = dist[j][i] = BFS(point[i],point[j]);
        if (dist[i][j] == -1)
        {
          flag = 0;
          break;
        }
      }
    }
    if (flag)
    {
      min = -1;
      memset(visited,0,sizeof(visited));
      visited[0] = 1;
      DFS(0,0,0);
      printf("%d\n",min);
    }
    else
      puts("-1");
  }
  return 0;
}

本文链接:http://bookshadow.com/weblog/2014/05/22/poj-2688-bfs-dfs/
请尊重作者的劳动成果,转载请注明出处!书影博客保留对文章的所有权利。

如果您喜欢这篇博文,欢迎您捐赠书影博客: ,查看支付宝二维码

Pingbacks已关闭。

暂无评论

张贴您的评论