Cleaning Robot
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4614 | Accepted: 1839 |
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.
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.
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
题意大致是,一个机器人打扫卫生,如果走最少的步数把每个dirty的点走完
首先利用广搜找出每两个点之间的最短距离(中间可以经过其他dirty的点,因为没有注意这个点,结果一直WA....),然后构建一个二维数组,用来存储两个点之间的距离,way[i][j]表示从i点到j点的距离,way[i][i]=0,way[i][j]=way[j][i]=bfs算出来的最短路径,然后利用深搜进行搜索,找出走过每个点且路径最短的路径,输出路径的长度
AC代码:
#include<iostream>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define MAX 22
#define inf 99999999
using namespace std;
int d[4][2]={{0,-1}, {-1,0}, {0,1}, {1,0}};
char map[MAX][MAX];
int book[MAX][MAX];
int vis[13];
int way[13][13];
int n,m,k,flag,Min;
struct dirt{
int x,y;
}num[13];
struct node{
int x,y;
int step;
};
queue<node> road;
int bfs(int sx, int sy, int ex, int ey)
{
memset(book,0,sizeof(book));
while(!road.empty())
road.pop();
node now,next;
now.x=sx, now.y=sy, now.step=0;
road.push(now);
book[sx][sy]=1;
while(!road.empty())
{
now=road.front();
road.pop();
for(int i=0;i<4;i++)
{
next.x=now.x+d[i][0];
next.y=now.y+d[i][1];
next.step=now.step+1;
if(next.x<0 || next.x>=m || next.y<0 || next.y>=n || book[next.x][next.y] || map[next.x][next.y]=='x')
continue;
if(next.x==ex && next.y==ey)
return next.step;
else //可以走其它的dirty点
{
book[next.x][next.y]=1;
road.push(next);
}
/*if(map[next.x][next.y]=='*')
{
book[next.x][next.y]=1;
if(next.x==ex && next.y==ey)
{
return next.step;
}
}
else
{
book[next.x][next.y]=1;
road.push(next);
}*/
}
}
return inf;
}
void dfs(int x, int cnt, int len)//x代表的是要探索的点
{
if(len>Min)
return;
if(cnt==k)
{
Min=(Min>len)?len:Min;
return;
}
for(int i=0;i<=k;i++)
{
if(vis[i]==0 && way[x][i])
{
vis[i]=1;
dfs(i,cnt+1,len+way[x][i]);
vis[i]=0;
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m) && n && m)
{
k=0;
getchar();
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='o')
{
num[0].x=i;
num[0].y=j;
map[i][j]='.';
}
else if(map[i][j]=='*')
{
num[++k].x=i;
num[k].y=j;
}
}
getchar();
}
if(k==0)
{
printf("0\n");
continue;
}
flag=1;
int temp;
for(int i=0;i<=k;i++)
way[i][i]=0;
for(int i=0;i<=k && flag;i++)
{
for(int j=i+1;j<=k;j++)
{
temp=bfs(num[i].x,num[i].y,num[j].x,num[j].y);
way[i][j]=way[j][i]=temp;//temp代表i到j的点的距离
if(temp==inf)
{
flag=0; break;
}
}
}
if(temp==inf)
{
printf("-1\n");
continue;
}
Min=inf;
memset(vis,0,sizeof(vis));
vis[0]=1;
dfs(0,0,0);
printf("%d\n",Min);
}
return 0;
}