您的位置:首页 > 其它

SPOJ-CLEANRBT-状压dp

2018-04-11 17:57 295 查看

CLEANRBT - Cleaning Robot

#dynamic-programming #bfs

 

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

 

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.

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

  先把所有垃圾之间的距离处理出来,将初始点视为一个特殊的垃圾点即可。
f[cur][S][j],表示已经处理了cur个垃圾,集合表示为S,且处理的最后一个垃圾是j的最小花费.
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
int fx[4][2]={1,0,0,1,-1,0,0,-1};
int e[15][15];
int n,m;
char p[25][25];
int f[2][(1<<11)+20][15];
struct Point{
int x,y;
bool operator==(Point t){
return x==t.x&&y==t.y;
}
}P[15];
#define ppi pair<Point,int>
bool vis[25][25];
int bfs(int a,int b)
{
memset(vis,0,sizeof(vis));
queue<ppi>q;
q.push(make_pair(P[a],0));
while(!q.empty()){
ppi u=q.front();
q.pop();
if(u.first==P[b]) return u.second;
if(vis[u.first.x][u.first.y]) continue;
vis[u.first.x][u.first.y]=1;
for(int i=0;i<4;++i){
int dx=u.first.x+fx[i][0];
int dy=u.first.y+fx[i][1];
if(dx<1||dy<1||dx>m||dy>n||p[dx][dy]=='x'||vis[dx][dy]) continue;
q.push(make_pair(Point{dx,dy},u.second+1));
}
}
return -1;
}
int main()
{
int i,j,k;
while(cin>>n>>m&&n&&m){
int tot=0;
int rx,ry;
for(i=1;i<=m;++i){
for(j=1;j<=n;++j){
cin>>p[i][j];
if(p[i][j]=='o'){
P[0].x=i;
P[0].y=j;
rx=i;
ry=j;
}
if(p[i][j]=='*'){
tot++;
P[tot].x=i;
P[tot].y=j;
}
}
}
for(i=0;i<=tot;++i){
for(j=i;j<=tot;++j){
if(i==j) e[i][j]=0;
else{
e[i][j]=e[j][i]=bfs(i,j);
//cout<<i<<' '<<j<<' '<<e[i][j]<<endl;
}
}
}

memset(f,inf,sizeof(f));
f[0][0][0]=0;
int cur=0;
for(int w=1;w<=tot;++w)
{
for(i=0;i<(1<<tot);++i){
for(j=0;j<=tot;++j){
if(f[cur][i][j]!=inf){
for(k=1;k<=tot;++k){
if(e[j][k]!=-1 && (i&(1<<(k-1)))==0){
f[cur^1][i|(1<<(k-1))][k]=min(
f[cur^1][i|(1<<(k-1))][k],f[cur][i][j]+e[j][k]);
}
}
}
}
}
for(i=0;i<(1<<tot);++i){
for(j=0;j<=tot;++j){
if(f[cur^1][i][j]==inf) continue;
//cout<<i<<' '<<j<<' '<<f[cur^1][i][j]<<endl;
}
}
memset(f[cur],inf,sizeof(f[cur]));
cur^=1;
}

int ans=inf;
for(i=0;i<=tot;++i) ans=min(ans,f[cur][(1<<tot)-1][i]);
if(ans==inf) ans=-1;
cout<<ans<<endl;
}
return 0;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: