Description
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
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
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
题意:给出一个地图,’.’表示地板,可到达,’x’表示家具,不可到达,’o’表示机器人的位置,’*’表示脏地板,
要求机器人清理完所有的脏地板,并且所走距离最小,如无解,输出-1。机器人从一块地板只能沿着上下左右的某一
方向走到另一块地板,且距离为1。
题目模型:
将机器人的位置和各个脏地板的位置看成一个图的各个顶点,那么题目的本质就是要求在一个无向图 中,遍历各个顶点至少一次,使得总路径最小。
经过分析后,可以发现此题即是典型的TSP问题,也就是旅行商问题或汉密尔顿通路。此题要求的就是 旅行商问题的最优值。
下面是一些引用证明:http://blog.sina.com.cn/s/blog_6e6227d5010124yg.html思路: 比赛的时候碰到这题两个小时都在搞这道题,以为一个bfs暴力就过了结果自己测试出好多样例错误就不知道怎
么改了;看了题解才知道是dfs+bfs
典型的acm开拓思维的题型,第一次遇见;
先bfs求出任意两点之间的最短距离,然后dfs求出哈密顿图的最短距离即可,
将机器人和脏地板看成顶点,先求出顶点两两之间的最短距离,此时问题便转换成了求解汉密尔顿通路的最优值,
因此只要从起点出发再进行一次DFS即可求出最优解即可。AC代码求解最短路径是用BFS的,因为这相当于一个迷宫,
直接BFS即可求出各顶点的最短路径。
证明:
这里有一个地方需要证明一下,就是求出顶点两两之间的最短路径后为什么就变成了求解汉密尔顿通路。 
假设有三个顶点A,B,C,已求出A到C的最短距离为d1,A到B的最短距离为d2,B到C的最短距离为d3。求最 短距离时从顶点A出发到顶点C不必再经过其他顶点。假设从A出发到C需要经过B使得总距离更小,也就是说d2+d3<d1
那么d1必定不是A到C的最短距离,此时d2+d3才是A到C的最短距离,与假设矛盾,因此得证。
由此可推断出,只要各顶点沿着最短路径到其他顶点,那么从一顶点到另一顶点直接到达即可,不必再经过其他顶点来使距离变小, 因此各顶点经过一次即可使得最距离最小。经过最短路处理后的图变成了求解汉密尔顿通路。
ac代码:#include<stdio.h>
#include<string.h>
#include<queue>
#define INF 0x7fffffff//设置的无穷大
using namespace std;
struct node
{ int x,y;
int step;
}p[22*22],s,e;
queue<node>Q;
int use[22];//标记这些污点哪些没走过;
int w,h,book[22][22];//标记此点是否走过;
int dis[25][25],ans,count;
char map[25][25];
int go[4][2]={0,1,1,0,-1,0,0,-1};
int check(node a)//判断是否可以走
{ if(a.x<0||a.x>=h||a.y<0||a.y>=w||book[a.x][a.y]==1||map[a.x][a.y]=='x')
return 0;
return 1;
}
int bfs()
{ memset(book,0,sizeof(book));
while(!Q.empty())
Q.pop();
int i;
Q.push(s);
while(!Q.empty())
{ node l,g;
g=Q.front();
Q.pop();
for(i=0;i<4;i++)
{ l=g;
l.x+=go[i][0];
l.y+=go[i][1];
l.step++;
if(check(l)==0)
continue;
book[l.x][l.y]=1;//标记该点
if(l.x==e.x&&l.y==e.y)
return l.step;
Q.push(l);
}
}
return INF;
}
void dfs(int s,int num,int len)
{
int i;
if(num==0&&len<ans)
{ ans=len;
return ;
}
if(len>ans)
return ;
for(i=0;i<count;i++)
{ if(!use[i]&&dis[s][i]!=INF)
{ use[i]=1;
dfs(i,num-1,len+dis[s][i]);
use[i ]=0;
}
}
return ;
}
int main()
{ int i,j,start,flag;
while(scanf("%d%d",&w,&h)!=EOF)
{
if(w==0&&h==0)
break;
count=0;
flag=0;
for(i=0;i<h;i++)
{ scanf("%s",map[i]);
for(j=0;j<w;j++)
{ if(map[i][j]=='o'||map[i][j]=='*')
{ p[count].x=i;
p[count].y=j;//存入结构体,后面好两两求最短距离;
if(map[i][j]=='o')
start=count;
count++;
}
dis[i][j]=INF;
}
}
for(i=0;i<count;i++)//求出任意两点之间的最短距离;
{ for(j=i+1;j<count;j++)
{
s=p[i];
e=p[j];
s.step=0;
dis[i][j]=dis[j][i]=bfs();
if(dis[i][j]==INF)//说明有一个点无法到达直接输出-1即可;
{ flag=1;
break;}
}
}
if(flag)
printf("-1\n");
else
{ ans=INF;
memset(use,0,sizeof(use));
use[start]=1;//标记这些找到的点是否走过(污点)
dfs(start,count-1,0);
printf("%d\n",ans);
}
}
return 0;
}
 
                   
                   
                   
                   
                            
 
                             本文介绍了一种解决机器人清扫房间中所有脏地板并寻找最短路径的方法。通过将问题转化为求解汉密尔顿通路的最优值问题,利用BFS求最短路径,再结合DFS搜索所有可能的路径组合。
本文介绍了一种解决机器人清扫房间中所有脏地板并寻找最短路径的方法。通过将问题转化为求解汉密尔顿通路的最优值问题,利用BFS求最短路径,再结合DFS搜索所有可能的路径组合。
           
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
                     
              
             
                   3286
					3286
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
					 
					 
					


 
            