北大poj-2688

时间:2023-03-09 16:53:37
北大poj-2688
Cleaning Robot
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4395   Accepted: 1763

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.

Sample Input

7 5
.......
.o...*.
.......
.*...*.
.......
15 13
.......x.......
...o...x....*..
.......x.......
.......x.......
.......x.......
...............
xxxxx.....xxxxx
...............
.......x.......
.......x.......
.......x.......
..*....x....*..
.......x.......
10 10
..........
..o.......
..........
..........
..........
.....xxxxx
.....x....
.....x.*..
.....x....
.....x....
0 0

Sample Output

8
49
-1 分析:BFS得到邻接矩阵,这样就是一个固定起点的TSP问题,再用递归DFS(也叫回溯法)+剪枝,就可以得到答案。
问题:输入数据是连续的,直到0 0终止。倒腾了好久都是WA,居然是这个原因。。。。
 #include <stdio.h>
#include <stdlib.h> #define MAX_MAX 65535
#define MAX_ROOM 25
#define MAX_DIRT 15
#define Q_LEN 10000 typedef struct
{
int x;
int y;
int step;
}T_Node; const int deltax[] = {-, , , };
const int deltay[] = {, , , -}; T_Node gatDirt[MAX_DIRT];
T_Node queue[Q_LEN];
int gwLen;
int gwWide;
int gwDirtNum = ;
char gawMap[MAX_ROOM][MAX_ROOM];
int gawDist[MAX_DIRT][MAX_DIRT]; int BFS(T_Node *ptStart, T_Node *ptEnd)
{
int head = ;
int tail = ;
int direction = ;
char Map[MAX_ROOM][MAX_ROOM];
queue[head] = *ptStart; int i,j;
for(j=; j<gwLen; j++)
{
for(i=; i<gwWide; i++)
{
Map[j][i] = gawMap[j][i];
}
} Map[ptStart->y][ptStart->x] = 'x';
while(head != tail)
{
for(direction=; direction<; direction++)
{
if(queue[head].x + deltax[direction] <
|| queue[head].x + deltax[direction] >= gwWide
|| queue[head].y + deltay[direction] <
|| queue[head].y + deltay[direction] >= gwLen)
continue;
queue[tail].x = queue[head].x + deltax[direction];
queue[tail].y = queue[head].y + deltay[direction];
if(queue[tail].x == ptEnd->x && queue[tail].y == ptEnd->y)
{
return queue[head].step + ;
}
if(Map[queue[tail].y][queue[tail].x] != 'x')
{
queue[tail].step = queue[head].step + ;
Map[queue[tail].y][queue[tail].x] = 'x';
tail++;
}
}
head++;
}
return -;
} int gawIsCleaned[MAX_DIRT];
int gwBest = MAX_MAX; void DFS(int sum, int position, int deep)
{
int k = ;
int ThisSum = sum;
deep++;
if(deep == gwDirtNum)
if(sum < gwBest)
{
gwBest = sum;
return;
}
for(k=; k<gwDirtNum; k++)
{
if(gawDist[position][k] == || gawIsCleaned[k] ==)
{
continue;
}
sum += gawDist[position][k];
if(sum > gwBest)
break;
gawIsCleaned[position] = ;
DFS(sum, k, deep);
sum = ThisSum;
gawIsCleaned[position] = ;
}
return;
} int main(void)
{
int i,j;
while(scanf("%d %d", &gwWide, &gwLen))
{
getchar();
if(gwWide == || gwLen == )
{
return ;
}
gwDirtNum = ;
for(j=; j<gwLen; j++)
{
for(i=; i<gwWide; i++)
{
scanf("%c", &gawMap[j][i]);
if(gawMap[j][i] == '*')
{
gatDirt[gwDirtNum].x = i;
gatDirt[gwDirtNum].y = j;
gwDirtNum++;
}
if(gawMap[j][i] == 'o')
{
gatDirt[].x = i;
gatDirt[].y = j;
}
}
getchar();
}
for(j=; j<gwDirtNum; j++)
{
for(i=j+; i<gwDirtNum; i++)
{
gawDist[j][i] = BFS(&gatDirt[i], &gatDirt[j]);
if(gawDist[j][i] == -)
{
gwBest = ;
break;
}
if(j != ) gawDist[i][j] = gawDist[j][i];
}
} if(gwBest == )
{
gwBest = MAX_MAX;
printf("-1\n");
}
else
{
gwBest = MAX_MAX;
DFS(, , );
printf("%d\n", gwBest);
}
}
return ;
}