HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))

本文介绍了一个清洁机器人在矩形房间中进行路径规划的问题,通过BFS算法计算最短距离,并采用状态压缩DP解决TSP问题,最终实现了最小移动步数的计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cleaning Robot
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4264 Accepted: 1713
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
Source

这道题目有很多解法吧,但是我觉得简单一点就是先BFS算出起点和每个脏的点之间最短距离,然后就是一个简单的TSP问题,用状态压缩DP就可以解决了。我是一遍过了,不免有点小激动呢

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stdio.h>

using namespace std;
#define MAX 100000000
int dis[11][11];
int dis2[11];
char a[25][25];
int dp[1<<10][11];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int vis[25][25];
int st,ed;
bool res;
int n,m;
struct Node
{
    int x;
    int y;
    int num;
}b[11];
queue<Node> q;
int bfs(int x1,int y1,int x2,int y2)
{
    Node term1;
    term1.x=x1;term1.y=y1;term1.num=0;
    vis[x1][y1]=1;
    q.push(term1);
    while(!q.empty())
    {
        Node term=q.front();
        q.pop();
        if(term.x==x2&&term.y==y2)
        {
            return term.num;
        }
        for(int i=0;i<4;i++)
        {
            int xx=term.x+dir[i][0];
            int yy=term.y+dir[i][1];
            if(xx<1||xx>n||yy<1||yy>m)
                continue;
            if(a[xx][yy]=='x'||vis[xx][yy])
                continue;
            vis[xx][yy]=1;
            Node temp;temp.x=xx;temp.y=yy;temp.num=term.num+1;
            q.push(temp);
        }

    }
    return -1;
}
void init()
{
    memset(vis,0,sizeof(vis));
    while(!q.empty())
        q.pop();
}
int main()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        if(n==0&&m==0)
            break;
        res=true;
        getchar();
        int cot=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%c",&a[i][j]);
                if(a[i][j]=='o'){st=i;ed=j;}
                else if(a[i][j]=='*'){b[cot].x=i;b[cot++].y=j;}
            }
            getchar();
        }
        for(int i=0;i<cot;i++)
            {
                init();dis2[i]=bfs(st,ed,b[i].x,b[i].y);
                if(dis2[i]==-1)
                {res=false;break;}
            }
        if(!res){printf("-1\n");continue;}
        for(int i=0;i<cot;i++)
            for(int j=i+1;j<cot;j++)
               {
                   init();
                   dis[i][j]=dis[j][i]=bfs(b[i].x,b[i].y,b[j].x,b[j].y);
               }
        int state=(1<<(cot))-1;
        for(int i=0;i<=state;i++)
            for(int j=0;j<cot;j++)
                dp[i][j]=MAX;
        for(int i=0;i<cot;i++)
            dp[1<<i][i]=dis2[i];
        for(int i=1;i<=state;i++)
        {
            for(int j=0;j<cot;j++)
            {
                if(!((1<<j)&i))
                    continue;
                for(int k=0;k<cot;k++)
                {
                    if(k==j) continue;
                    if((1<<k)&i) continue;
                    int ss=i+(1<<k);
                    dp[ss][k]=min(dp[ss][k],dp[i][j]+dis[j][k]);
                }
            }
        }
        int ans=MAX;
        for(int i=0;i<cot;i++)
            ans=min(ans,dp[state][i]);
        printf("%d\n",ans);
    }
        return 0;

}

转载于:https://www.cnblogs.com/dacc123/p/8228771.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值