https://www.spoj.com/problems/CLEANRBT/
此题与tyvj1288类似,传送门。
https://omarsebres.com/2012/05/21/1702-cleaning-robot/
http://xoptutorials.com/index.php/2017/01/01/spojcleanrbt/
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
IThe 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
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)
'*' : 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.
Example
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 Output: 8 49 -1
此题与tyvj1288类似,传送门。
bfs出每个点到其他点的距离,之后用next_permutation即可。
int
const
MAX = 25;
int
w, h;
bool
visited[MAX][MAX];
int
dx[] = {-1, 1, 0, 0};
int
dy[] = {0, 0, -1, 1};
char
grid[MAX][MAX];
int
dirtIndx;
int
dirtX[11];
int
dirtY[11];
int
arr[11];
int
dist[MAX][MAX][MAX][MAX];
int
const
INF = 62500;
inline
bool
ok(
int
x,
int
y) {
return
x >= 0 && x < h && y >= 0 && y < w;
}
int
solve(
int
xx,
int
yy,
int
gx,
int
gy) {
memset
(visited,
false
,
sizeof
(visited));
queue<pair<pair<
int
,
int
>,
int
> > q;
visited[xx][yy] =
true
;
q.push(make_pair(make_pair(xx, yy), 0));
while
(!q.empty()) {
int
curx = q.front().first.first;
int
cury = q.front().first.second;
int
cost = q.front().second;
q.pop();
if
(curx == gx && cury == gy)
return
cost;
for
(
int
i = 0; i < 4; ++i) {
int
nx = dx[i] + curx;
int
ny = dy[i] + cury;
if
(ok(nx, ny) && grid[nx][ny] !=
'x'
) {
if
(!visited[nx][ny]) {
visited[nx][ny] =
true
;
q.push(make_pair(make_pair(nx, ny), cost + 1));
}
}
}
}
return
INF;
}
int
main() {
while
(
scanf
(
"%d%d"
, &w, &h)) {
if
(w == 0 && h == 0)
break
;
int
sx, sy;
dirtIndx = 0;
for
(
int
i = 0; i < h; ++i) {
scanf
(
"%s"
, grid[i]);
for
(
int
j = 0; j < w; ++j)
if
(grid[i][j] ==
'o'
) {
sx = i;
sy = j;
}
else
if
(grid[i][j] ==
'*'
) {
dirtX[dirtIndx] = i;
dirtY[dirtIndx] = j;
++dirtIndx;
}
}
for
(
int
i = 0; i < MAX; ++i)
for
(
int
j = 0; j < MAX; ++j)
for
(
int
k = 0; k < MAX; ++k)
for
(
int
l = 0; l < MAX; ++l)
dist[i][j][k][l] = INF;
for
(
int
i = 0; i < dirtIndx; ++i)
dist[sx][sy][dirtX[i]][dirtY[i]] = solve(sx, sy, dirtX[i], dirtY[i]);
for
(
int
i = 0; i < dirtIndx; ++i)
for
(
int
j = i; j < dirtIndx; ++j) {
int
xx = dirtX[i];
int
yy = dirtY[i];
int
gx = dirtX[j];
int
gy = dirtY[j];
dist[xx][yy][gx][gy] = solve(xx, yy, gx, gy);
dist[gx][gy][xx][yy] = dist[xx][yy][gx][gy];
}
for
(
int
i = 0; i < dirtIndx; ++i) arr[i] = i;
int
res = INF;
do
{
int
sum = 0;
sum += dist[sx][sy][dirtX[arr[0]]][dirtY[arr[0]]];
for
(
int
i = 1; i < dirtIndx; ++i) {
int
dd = arr[i - 1];
int
gd = arr[i];
sum += dist[dirtX[dd]][dirtY[dd]][dirtX[gd]][dirtY[gd]];
}
res = min(res, sum);
}
while
(next_permutation(arr, arr + dirtIndx));
res >= INF ?
puts
(
"-1"
) :
printf
(
"%d\n"
, res);
}
return
0;
}