POJ 2688 Cleaning Robot (BFS+DFS)

本文探讨了一种针对移动机器人在充满家具的矩形房间中清洁地面的路径规划算法。算法的目标是最小化从初始位置到达所有脏瓷砖所需的移动次数,同时考虑到房间中可能存在的障碍物。通过广度优先搜索和图的遍历策略,该算法确定了机器人能否及如何访问所有脏瓷砖,以实现最高效的清洁任务。
Cleaning Robot
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4952 Accepted: 1921

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

 

 

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 21
#define N 33
#define MOD 1000000
#define INF 1000000009
const double eps = 1e-9;
const double PI = acos(-1.0);
/*
给定障碍,起始点 
问最少花费多少时间遍历所有点
*/
int m, n, index[MAXN][MAXN], ans, cnt;
char g[MAXN][MAXN];
bool been[MAXN][MAXN],vis[MAXN];
int X[4] = { -1,0,1,0 }, Y[4] = { 0,1,0,-1 };
struct node
{
    int x, y, t;
    node(int _x,int _y,int _t):x(_x),y(_y),t(_t){}
};
vector<node> pos;
struct edge
{
    int to, cost;
    edge(int _t,int _c):to(_t),cost(_c){}
};
vector<edge> E[MAXN];
bool bfs(int sx, int sy)
{
    memset(been, false, sizeof(been));
    queue<node> q;
    q.push(node(sx, sy, 0));
    been[sx][sy] = true;
    while (!q.empty())
    {
        node t = q.front();
        q.pop();
        if (g[t.x][t.y] == 'o' || g[t.x][t.y] == '*')
        {
            E[index[sx][sy]].push_back(edge(index[t.x][t.y], t.t));
        }
        for (int i = 0; i < 4; i++)
        {
            int nx = t.x + X[i], ny = t.y + Y[i];
            if (nx >= 0 && ny >= 0 && nx < n&&ny < m && !been[nx][ny] && g[nx][ny] != 'x')
            {
                been[nx][ny] = true;
                q.push(node(nx, ny, t.t + 1));
            }
        }
    }
    if (E[index[sx][sy]].size() != cnt)
        return false;
    else
        return true;
}
void dfs(int k, int time,int tmp)
{
    if (time > ans) return;
    if (tmp == cnt)
    {
        ans = min(time, ans);
        return;
    }
    for (int i = 0; i < E[k].size(); i++)
    {
        if (!vis[E[k][i].to])
        {
            vis[E[k][i].to] = true;
            dfs(E[k][i].to, time + E[k][i].cost,tmp+1);
            vis[E[k][i].to] = false;
        }
    }
}
int main()
{
    while (scanf("%d%d", &m, &n), n + m)
    {
        ans = INF;
        pos.clear();
        for (int i = 0; i < MAXN; i++)
            E[i].clear();
        memset(vis, false, sizeof(vis));
        cnt = 1;
        for (int i = 0; i < n; i++)
        {
            scanf("%s", g[i]);
            for (int j = 0; j < m; j++)
            {
                if ( g[i][j] == '*')
                {
                    index[i][j] = cnt++;
                    pos.push_back(node(i, j, 0));
                }
                else if (g[i][j] == 'o')
                {
                    index[i][j] = 0;
                    pos.insert(pos.begin(),node(i,j,0));
                }
            }
        }
        bool f = false;
        for (int i = 0; i < pos.size(); i++)
        {
            if (!bfs(pos[i].x, pos[i].y))
            {
                f = true;
                break;
            }
        }
        if (f)
        {
            printf("-1\n");
            continue;
        }
        vis[0] = true;
        dfs(0, 0, 1);
        printf("%d\n", ans);
    }
}

 

转载于:https://www.cnblogs.com/joeylee97/p/7017577.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值