poj 2688 Cleaning Robot (tsp问题)

Cleaning Robot
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4073   Accepted: 1659

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

______

好蠢,竟然没看出来这道题的不同之处,以为就是个搜

然后样例什么的都过了...

结果显然wa...

然后才发现,这道题应该是tsp问题.

解法是先跑一遍bfs,

对于所有的脏点和起点,得到没两个点之间的距离.

然后跑一遍dfs,枚举出所有的组合,同时更新答案.

晚安.

/*************************************************************************
    > File Name: code/poj/rr2688.cpp
    > Author: 111qqz
    > Email: rkz2013@126.com 
    > Created Time: 2015年08月16日 星期日 03时39分34秒
 ************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define y0 abc111qqz
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define tm crazy111qqz
#define lr dying111qqz
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)  
typedef long long LL;
typedef unsigned long long ULL;
const int inf = 0x7fffffff;
const int N=25;
int w,h;
char maze[N][N];
int dist[N][N];
int cnt;//机器人与脏地的个数
int tag[N][N];//标记
bool vist[N][N];
struct node
{
    int x,y;
    int step;
    bool ok ()
    {
    if (x<1||x>h||y<1||y>w||vist[x][y]||maze[x][y]=='x')
        return false;
    return true;
    }
}pos[N*N];

node robot;
int dir[4][2]={0,-1,0,1,-1,0,1,0};
void bfs(node p,int po)
{
    vist[p.x][p.y]=1;
    queue<node>q;
    q.push(p);
    while(!q.empty())
    {
        node cur=q.front();
        q.pop();
        if(maze[cur.x][cur.y]=='o' || maze[cur.x][cur.y]=='*')
            dist[po][tag[cur.x][cur.y]]=cur.step;
        node next;
        next.step=cur.step+1;
        for(int i=0;i<4;i++)
        {
            next.x=cur.x+dir[i][0];
            next.y=cur.y+dir[i][1];
            if(!next.ok())
                continue;
            q.push(next);
            vist[next.x][next.y]=1;
        }
    }
}

int ans=inf;
bool vis[N];
void dfs(int x,int step,int s)
{
    if(step==cnt)
    {
        if(s<ans)
            ans=s;
        return ;
    }
    if(s>ans)
        return ;
    for(int j=1;j<=cnt;j++)
    {
        if(vis[j])
            continue;
        vis[j]=1;
        dfs(j,step+1,s+dist[x][j]);
        vis[j]=0;
    }
}

int main()
{
    while(~scanf("%d%d",&w,&h))
    {
        if(w==0&&h==0)
            break;
       // getchar();
        cnt=0;
        memset(pos,0,sizeof(pos));
        memset(tag,0,sizeof(tag));
        for(int i=1;i<=h;i++)
        {
            scanf("%s",maze[i]+1);
            for(int j=1;j<=w;j++)
                if (maze[i][j]=='o')
                {
                     pos[++cnt].x=i;
                    pos[cnt].y=j;
                    robot.x=i;
                    robot.y=j;
                    tag[i][j]=cnt;
                }
                else if(maze[i][j]=='*')
                {
                     pos[++cnt].x=i;
                    pos[cnt].y=j;
                    tag[i][j]=cnt;
                }
        }
        for(int i=1;i<=cnt;i++)
            for(int j=1;j<=cnt;j++)
                if(i !=j)
                    dist[i][j]=inf;
                else
                    dist[i][j]=0;
        for(int i=1;i<=cnt;i++)
        {    
            memset(vist,0,sizeof(vist));
            pos[i].step=0;
            bfs(pos[i],i);
        }
        bool flag=1;
        for(int i=1;i<=cnt && flag;i++)
            for(int j=1;j<=cnt && flag;j++)
                if(dist[i][j]==inf)
                     flag=0;

        if(flag==0)
        {
            puts("-1");
            continue;
        }
        memset(vis,0,sizeof(vis));
        vis[tag[robot.x][robot.y]]=1;
        ans=inf;
        dfs(tag[robot.x][robot.y],1,0);
        printf("%d
",ans);

    }
    return 0;
}
原文地址:https://www.cnblogs.com/111qqz/p/4733541.html