SPOJ-CLEANRBT-状压dp
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; }
- SPOJ 2829 TLE - Time Limit Exceeded(状压DP)
- SPOJ BALNUM Balanced Numbers 状压+数位DP
- Spoj 9894 Tichu 状压dp
- HDU4856 Tunnels —— BFS + 状压DP
- [luoguP2831] 愤怒的小鸟(状压DP)
- zjnu1745 DOMINE(状压dp)
- hdu 4539(状压dp)
- BZOJ 1231 状压DP
- HDU 4026 Unlock the Cell Phone 状压dp(类似TSP)
- UESTC 882 冬马党 --状压DP
- BZOJ 2064: 分裂 状压dp
- POJ:1185-炮兵阵地(状压dp入门)
- Light OJ 1194 Colored T-Shirts (状压DP)
- HDU 4336 Card Collector(容斥原理 or 状压求期望dp)
- #NOIP模拟赛#相似字符串(树形DP + 状压)
- ZOJ3777 - Problem Arrangement(状压DP)
- 小C打比赛(状压dp)
- poj 2663 Tri Tiling 状压dp
- POJ 2686 Traveling by Stagecoach(状压DP)
- hdu 3001 Travelling (TSP问题,状压dp)