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
题意:
给你给出一个nm的矩阵。‘o’表示机器人的位置,‘x’表示障碍物,‘.’表示平地,’'表示垃圾。现在问你机器人最少走几步可以把所有的障碍物全部清理完。
思路:
先用bfs或者dijksta求出任意垃圾之间的距离,求出机器人和垃圾之间的距离。
然后使用TSP来解决就好了。
AC代码:
#include<bits/stdc++.h>
#include<queue>
#include<string>
#include<string.h>
#include<iostream>
#include<stdio.h>
#include<algorithm>
#define MAXN 30
#define maxn 100000
#define SIGMA_SIZE 4
#define mod 10007
#define INF 0x3f3f3f3f
#define inf 0x8f
using namespace std;
typedef long long LL;
int n,m,cnt,sum;
struct node
{
int x,y,d;
};
int dis[MAXN][MAXN];
int dp[1<<12][30];
char mp[MAXN][MAXN];
bool vis[MAXN][MAXN];
int num[500];
int dic_x[4]={0,0,-1,1};
int dic_y[4]={-1,1,0,0};
void bfs(int x,int y)
{
queue<node> Q;
memset(vis,false,sizeof(vis));
Q.push({x,y,0});int st=x*m+y;
vis[x][y]=true;
if(num[st]==-1)
num[st]=++cnt;
while(!Q.empty())
{
node X=Q.front();
Q.pop();
int et=X.x*m+X.y;
if(num[et]==-1&&mp[X.x][X.y]=='*')
{
num[et]=++cnt;
}
if(num[et]!=-1)
{
dis[num[st]][num[et]]=dis[num[et]][num[st]]=X.d;
}
for(int i=0;i<4;i++)
{
int xx=X.x+dic_x[i];
int yy=X.y+dic_y[i];
if(xx<1||xx>n||yy<1||yy>m||vis[xx][yy]||mp[xx][yy]=='x')
continue;
Q.push({xx,yy,X.d+1});
vis[xx][yy]=true;
}
}
}
void init()
{
cnt=0;
memset(num,-1,sizeof(num));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(mp[i][j]=='*')
bfs(i,j);
else if(mp[i][j]=='o')
{
num[i*m+j]=0;
bfs(i,j);
}
}
}
memset(dp,INF,sizeof(dp));
for(int i=0;i<cnt;i++)
{
dp[1<<i][i+1]=dis[i+1][0];
}
dp[0][0]=0;
}
int TSP()
{
int state=1<<cnt;
for(int i=1;i<=state;i++)
{
for(int j=0;j<cnt;j++)
{
if(i&(1<<j))
{
for(int k=0;k<cnt;k++)
{
if((i&(1<<k))&&k!=j)
dp[i][j+1]=min(dp[i][j+1],dp[i^(1<<j)][k+1]+dis[k+1][j+1]);
}
}
}
}
int minn=INF;
for(int i=0;i<=cnt;i++)
minn=min(minn,dp[state-1][i]);
return minn;
}
bool check(int x,int y)
{
int ans=0;
memset(vis,false,sizeof(vis));
queue<node> Q;
Q.push({x,y});
vis[x][y]=true;
while(!Q.empty())
{
node X=Q.front();Q.pop();
if(mp[X.x][X.y]=='o'||mp[X.x][X.y]=='*')
ans++;
for(int i=0;i<4;i++)
{
int xx=X.x+dic_x[i];
int yy=X.y+dic_y[i];
if(xx<1||xx>n||yy<1||yy>m||vis[xx][yy]||mp[xx][yy]=='x')
continue;
vis[xx][yy]=true;
Q.push({xx,yy});
}
}
if(ans==sum)
return true;
return false;
}
int main()
{
while(scanf("%d %d",&m,&n)!=EOF)
{
if(m==0||n==0)
break;
sum=0;int st,et;
for(int i=1;i<=n;i++)
{
scanf("%s",mp[i]+1);
for(int j=1;j<=m;j++)
{
if(mp[i][j]=='*'||mp[i][j]=='o')
{sum++;st=i;et=j;}
}
}
if(!check(st,et))
{
printf("-1\n");
continue;
}
init();
printf("%d\n",TSP());
}
return 0;
}