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: [email protected]
    > 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\n",ans);

    }
    return 0;
}
时间: 2025-01-05 15:10:03

poj 2688 Cleaning Robot (tsp问题)的相关文章

POJ - 2688 Cleaning Robot

题意:求回收所有垃圾的最短路 思路:先BFS处理两个垃圾的距离,然后DFS记忆化搜索 dp[i][state]表示处理到第i个后状态是state的最短路 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> using namespace std; const int

poj 2688 cleaning robot(bfs+dfs)

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

poj 2688 状态压缩dp解tsp

题意: 裸的tsp. 分析: 用bfs求出任意两点之间的距离后可以暴搜也可以用next_permutation水,但效率肯定不如状压dp.dp[s][u]表示从0出发访问过s集合中的点,目前在点u走过的最短路程. 代码: //poj 2688 //sep9 #include <iostream> #include <queue> using namespace std; const int maxW=32; const int maxN=12; int dx[4]={-1,1,0,

POJ 1573:Robot Motion

Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10267   Accepted: 5001 Description A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in

Cleaning Robot POJ - 2688

题目链接:https://vjudge.net/problem/POJ-2688 题意:在一个地面上,有一个扫地机器人,有一些障碍物,有一些脏的地砖,问,机器热能不能清扫所有的地砖, (机器人不能越过障碍物),如果能,需要得到机器人移动最少步数. 思路:可以把扫地机器人和地砖编号,然后得出编号之间的相互距离,那么就变成了一个图问题. 用bfs来得出编号之间的距离. 再用dfs来填边,得到最少移动步数,注意要剪支,不然就T了... 1 #include <iostream> 2 #include

POJ 2376 Cleaning Shifts(轮班打扫)

Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided t

POJ 2376 Cleaning shifts 贪心 基础题

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13042   Accepted: 3373 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one co

[POJ 2376] Cleaning Shifts

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12874   Accepted: 3331 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one co

POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)

Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000