zoukankan      html  css  js  c++  java
  • SPOJ-CLEANRBT-状压dp

    CLEANRBT - Cleaning Robot

    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的最小花费.
      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 #define inf 0x3f3f3f3f
      4 int fx[4][2]={1,0,0,1,-1,0,0,-1};
      5 int e[15][15];
      6 int n,m;
      7 char p[25][25];
      8 int f[2][(1<<11)+20][15];
      9 struct Point{
     10     int x,y;
     11     bool operator==(Point t){
     12         return x==t.x&&y==t.y;
     13     }
     14 }P[15];
     15 #define ppi pair<Point,int>
     16 bool vis[25][25];
     17 int bfs(int a,int b)
     18 {
     19     memset(vis,0,sizeof(vis));
     20     queue<ppi>q;
     21     q.push(make_pair(P[a],0));
     22     while(!q.empty()){
     23         ppi u=q.front();
     24         q.pop();
     25         if(u.first==P[b]) return u.second;
     26         if(vis[u.first.x][u.first.y]) continue;
     27         vis[u.first.x][u.first.y]=1;
     28         for(int i=0;i<4;++i){
     29             int dx=u.first.x+fx[i][0];
     30             int dy=u.first.y+fx[i][1];
     31             if(dx<1||dy<1||dx>m||dy>n||p[dx][dy]=='x'||vis[dx][dy]) continue;
     32             q.push(make_pair(Point{dx,dy},u.second+1));
     33         }
     34     }
     35     return -1;
     36 }
     37 int main()
     38 {
     39     int i,j,k;
     40     while(cin>>n>>m&&n&&m){
     41         int tot=0;
     42         int rx,ry;
     43         for(i=1;i<=m;++i){
     44             for(j=1;j<=n;++j){
     45                 cin>>p[i][j];
     46                 if(p[i][j]=='o'){
     47                     P[0].x=i;
     48                     P[0].y=j;
     49                     rx=i;
     50                     ry=j;
     51                 }
     52                 if(p[i][j]=='*'){
     53                     tot++;
     54                     P[tot].x=i;
     55                     P[tot].y=j;
     56                 }
     57             }
     58         }
     59         for(i=0;i<=tot;++i){
     60             for(j=i;j<=tot;++j){
     61                 if(i==j) e[i][j]=0;
     62                 else{
     63                     e[i][j]=e[j][i]=bfs(i,j);
     64                     //cout<<i<<' '<<j<<' '<<e[i][j]<<endl;
     65                 }
     66             } 
     67         }
     68         
     69         memset(f,inf,sizeof(f));
     70         f[0][0][0]=0;
     71         int cur=0;
     72         for(int w=1;w<=tot;++w)
     73     {
     74         for(i=0;i<(1<<tot);++i){
     75             for(j=0;j<=tot;++j){
     76                 if(f[cur][i][j]!=inf){
     77                     for(k=1;k<=tot;++k){
     78                         if(e[j][k]!=-1 && (i&(1<<(k-1)))==0){
     79                             f[cur^1][i|(1<<(k-1))][k]=min(
     80                             f[cur^1][i|(1<<(k-1))][k],f[cur][i][j]+e[j][k]);
     81                         }
     82                     }
     83                 }
     84             }
     85         }
     86         for(i=0;i<(1<<tot);++i){
     87             for(j=0;j<=tot;++j){
     88                 if(f[cur^1][i][j]==inf) continue;
     89                 //cout<<i<<' '<<j<<' '<<f[cur^1][i][j]<<endl;
     90             }
     91         }
     92         memset(f[cur],inf,sizeof(f[cur]));
     93         cur^=1;
     94     }
     95     
     96     int ans=inf;
     97     for(i=0;i<=tot;++i) ans=min(ans,f[cur][(1<<tot)-1][i]);
     98     if(ans==inf) ans=-1;
     99     cout<<ans<<endl;
    100     }
    101     return 0;
    102 }
  • 相关阅读:
    PHP 操作redis常用方法代码
    PHP 大型网站 高并发大流量解决方案
    Nginx 负载均衡
    maven中mirror与repositories的关系
    Java事件机制---自定义事件
    sql优化几个方面
    临时表的概念
    sql产生临时表
    MySQL rows
    工厂模式 https://www.jianshu.com/p/6dfb5b66d088
  • 原文地址:https://www.cnblogs.com/zzqc/p/8797379.html
Copyright © 2011-2022 走看看