POJ 2688 FOJ 1634 Cleaning Robot

来源:互联网 发布:style 用js赋值怎么写 编辑:程序博客网 时间:2024/06/05 06:52
Cleaning Robot
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 936Accepted: 391

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

849-1

Source

 
很好玩的题目,感觉很实用....
1.bfs,算出2点之间的所有距离
2.dfs得到结果
通过这题偶知道了居然可以这样dfs....看来以前还是太菜了也............
  1. #include <iostream>
  2. #include <queue>
  3. #include <algorithm>
  4. using namespace std;
  5. #define inf 1000000000
  6. const int dx[4]={0,1,0,-1};
  7. const int dy[4]={1,0,-1,0};
  8. int n,m,l,res,mm;
  9. char map[21][21],a[12];
  10. int grap[21][21];
  11. bool f[12];
  12. struct _point
  13. {
  14.     int x,y;
  15.     int step;
  16.     bool operator==(const _point& a) const
  17.     {
  18.         return x==a.x&&y==a.y;
  19.     }
  20. }s,e[12],temp;
  21. int dis(_point s,_point des)
  22. {
  23.     bool flag[21][21];
  24.     int i;
  25.     queue<_point> q;
  26.     memset(flag,false,sizeof(flag));
  27.     flag[s.x][s.y]=true;
  28.     s.step=0;
  29.     q.push(s);
  30.     while(!q.empty())
  31.         {
  32.             for(i=0;i<4;i++)
  33.                 {
  34.                     temp=q.front();
  35.                     temp.x+=dx[i];
  36.                     temp.y+=dy[i];
  37.                     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;
  38.                     temp.step++;
  39.                     flag[temp.x][temp.y]=true;
  40.                     q.push(temp);
  41.                     if(temp==des)
  42.                         return temp.step;
  43.                 }
  44.             q.pop();
  45.         }
  46.     return -1;  
  47. }
  48. void dfs(int t, int n, int pathlen)
  49. {
  50.         if( n >= l ){
  51.                 if( pathlen < res )
  52.                         res = pathlen;
  53.                 return;
  54.         }
  55.         int i;        
  56.         for(i=1;i<=l; i++){
  57.                 if( pathlen + grap[t][i] >= res ) continue
  58.                 if(!f[i]&&grap[t][i]!=inf){
  59.                         f[i] = true;
  60.                         dfs(i, n+1, pathlen + grap[t][i]);
  61.                         f[i] = false;
  62.                 }
  63.         }
  64. }
  65. int main()
  66. {
  67.     int i,j,k,t;
  68.     bool flag;
  69.     while(scanf("%d%d",&m,&n)!=EOF,m||n)
  70.         {
  71.             getchar();
  72.             l=k=0;
  73.             flag=false;
  74.             for(i=0;i<=11;i++)
  75.                 for(j=0;j<=11;j++)
  76.                     grap[i][j]=inf;
  77.             for(i=0;i<=n+1;i++)
  78.                 map[i][0]=map[i][m+1]='.';
  79.             for(i=0;i<=m+1;i++)
  80.                 map[0][i]=map[n+1][i]='.';
  81.             for(i=1;i<=n;i++)
  82.             {
  83.                 for(j=1;j<=m;j++)
  84.                 {
  85.                     scanf("%c",&map[i][j]);
  86.                     if(map[i][j]=='o')
  87.                         s.x=i,s.y=j;
  88.                     if(map[i][j]=='*')
  89.                         e[l].x=i,e[l++].y=j;
  90.                 }
  91.                 getchar();
  92.             }
  93.             for(i=l;i>=1;i--)
  94.                 e[i]=e[i-1];
  95.             e[0]=s;
  96.             l++;
  97.             for(i=0;i<l&&!flag;i++)
  98.                 for(j=0;j<l;j++)
  99.                     {
  100.                         if(i==j) continue;
  101.                         t=dis(e[i],e[j]);
  102.                         grap[i][j]=t;
  103.                         grap[j][i]=t;
  104.                         if(t==-1) {flag=true;break;}
  105.                     }
  106.                 if(flag)
  107.                     {
  108.                         cout<<-1<<endl;
  109.                         continue;
  110.                     }
  111.                 res=(1<<20);
  112.                 memset(f, 0, sizeof(f));
  113.                 f[0]=true;
  114.                 dfs(0,1,0);
  115.                 printf("%d/n",res);
  116.         }
  117. }
原创粉丝点击