HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))
2016-03-16 16:37
295 查看
Cleaning Robot
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4264 Accepted: 1713
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
Source
这道题目有很多解法吧,但是我觉得简单一点就是先BFS算出起点和每个脏的点之间最短距离,然后就是一个简单的TSP问题,用状态压缩DP就可以解决了。我是一遍过了,不免有点小激动呢
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4264 Accepted: 1713
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
Source
这道题目有很多解法吧,但是我觉得简单一点就是先BFS算出起点和每个脏的点之间最短距离,然后就是一个简单的TSP问题,用状态压缩DP就可以解决了。我是一遍过了,不免有点小激动呢
#include <iostream> #include <string.h> #include <stdlib.h> #include <algorithm> #include <math.h> #include <queue> #include <stdio.h> using namespace std; #define MAX 100000000 int dis[11][11]; int dis2[11]; char a[25][25]; int dp[1<<10][11]; int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; int vis[25][25]; int st,ed; bool res; int n,m; struct Node { int x; int y; int num; }b[11]; queue<Node> q; int bfs(int x1,int y1,int x2,int y2) { Node term1; term1.x=x1;term1.y=y1;term1.num=0; vis[x1][y1]=1; q.push(term1); while(!q.empty()) { Node term=q.front(); q.pop(); if(term.x==x2&&term.y==y2) { return term.num; } for(int i=0;i<4;i++) { int xx=term.x+dir[i][0]; int yy=term.y+dir[i][1]; if(xx<1||xx>n||yy<1||yy>m) continue; if(a[xx][yy]=='x'||vis[xx][yy]) continue; vis[xx][yy]=1; Node temp;temp.x=xx;temp.y=yy;temp.num=term.num+1; q.push(temp); } } return -1; } void init() { memset(vis,0,sizeof(vis)); while(!q.empty()) q.pop(); } int main() { while(scanf("%d%d",&m,&n)!=EOF) { if(n==0&&m==0) break; res=true; getchar(); int cot=0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { scanf("%c",&a[i][j]); if(a[i][j]=='o'){st=i;ed=j;} else if(a[i][j]=='*'){b[cot].x=i;b[cot++].y=j;} } getchar(); } for(int i=0;i<cot;i++) { init();dis2[i]=bfs(st,ed,b[i].x,b[i].y); if(dis2[i]==-1) {res=false;break;} } if(!res){printf("-1\n");continue;} for(int i=0;i<cot;i++) for(int j=i+1;j<cot;j++) { init(); dis[i][j]=dis[j][i]=bfs(b[i].x,b[i].y,b[j].x,b[j].y); } int state=(1<<(cot))-1; for(int i=0;i<=state;i++) for(int j=0;j<cot;j++) dp[i][j]=MAX; for(int i=0;i<cot;i++) dp[1<<i][i]=dis2[i]; for(int i=1;i<=state;i++) { for(int j=0;j<cot;j++) { if(!((1<<j)&i)) continue; for(int k=0;k<cot;k++) { if(k==j) continue; if((1<<k)&i) continue; int ss=i+(1<<k); dp[ss][k]=min(dp[ss][k],dp[i][j]+dis[j][k]); } } } int ans=MAX; for(int i=0;i<cot;i++) ans=min(ans,dp[state][i]); printf("%d\n",ans); } return 0; }
相关文章推荐
- 简单MVC+EF框架搭建
- iOS开发——pch文件创建
- 面试经典问题---select、poll、epoll之间有什么区别
- javascript运动入门
- C#成魔之路<15> 注册表技术
- 0024-pg数据库的sequence问题
- Html5之基础-4 HTML文本
- ODOO_posbox_打印出的小票(收据)如何修改?
- poj 2478 Farey Sequence
- CUBRID学习笔记17 事务的回滚
- 主流存储引擎详解:Innodb,Tokudb、Memory、MYISAM、Federated
- push以及pop,shift,unshift
- C++之继承与派生(1)
- 阻止Eclipse一直building workspace
- 心得20160316
- 7. Reverse Integer
- Java编程之构造代码块
- iOS 关于枚举的使用
- JNI签名-android
- SIP协议研究浅析