Cleaning Robot
Time Limit: 1000MS | Memory Limit: 65536K |
Total Submissions: 1728 | Accepted: 738 |
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
#include<iostream>
#include<queue>
using namespace std;
#define inf 1000000000
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};//定義四個方向的數組
int n,m,l,res;
char map[21][21];
int grap[21][21]; //MAP是用來放圖的 ,GRAP是用來記錄長度的 (是髒點對髒點)
bool f[12];
struct _point
{
int x,y,step;
bool operator==(const _point& a) const //定義==這個操作是X,Y相等
{
return x==a.x&&y==a.y;
}
}s,e[12],temp;//定義一個結構體和操作 ,S用來記錄機器人的位置,E是用來記錄所有髒點,TEMP是交換用的
int dis(_point s,_point des)//廣搜
{
bool flag[21][21];
int i;
queue<_point> q;//SQL建立一個隊列
memset(flag,false,sizeof(flag));
flag[s.x][s.y]=true;
s.step=0;
q.push(s);
while(!q.empty())//當隊列裡還有點,因為使用這個來存序列
{
for(i=0;i<4;i++)
{
temp=q.front();
temp.x+=dx[i];
temp.y+=dy[i];
if(flag[temp.x][temp.y]||map[temp.x][temp.y]=='x'||temp.x<1||temp.y<1||temp.x>n||temp.y>m) continue;
temp.step++;
flag[temp.x][temp.y]=true;
q.push(temp);
if(temp==des)
return temp.step;
}
q.pop();
}
return -1;
}
void dfs(int t,int n,int pathlen)
{
if(n>=l)
{
if(pathlen<res)
res=pathlen;
return;
}
int i;
for(i=1;i<l;i++)
{
if( pathlen + grap[t][i] >= res ) continue; //成功的剪枝
if(!f[i]&&grap[t][i]!=inf)
{
f[i] = true;
dfs(i, n+1, pathlen + grap[t][i]);
f[i] = false;
}
}
}
int main()
{
freopen("in.txt","r",stdin);
freopen("out1.txt","w",stdout);
int i,j,t;
bool flag;
while(scanf("%d%d",&m,&n),m||n)
{
getchar();//如果等下是一個一個字母的讀入就需要把換行給去掉
l=0;
flag=false;
for(i=0;i<=11;i++)
for(j=0;j<=11;j++) //髒點隻有10個
grap[i][j]=inf;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='o')
s.x=i,s.y=j; //如果是機器人現在的位置,就記錄下來
if(map[i][j]=='*')
e[++l].x=i,e[l].y=j;//記錄下每個不幹淨的點
}
getchar();
}
e[0]=s; //第一個點是機器人所在的點
l++;
for(i=0;i<l&&!flag;i++)
for(j=0;j<l;j++)
{
if(i==j) continue;
t=dis(e[i],e[j]);
grap[i][j]=t;
grap[j][i]=t;
if(t==-1)
{
flag=true;
break;
}
}//用BFS算出每個髒點的距離,以便得出結論,是否能都被清理幹淨
if(flag)
{
printf("-1/n");
continue;
}
res=(1<<25);//這個就是用位運算将RES定為最大值
memset(f,0,sizeof(f));
f[0]=true;
dfs(0,1,0);
printf("%d/n",res);
}
return 0;
}