zoukankan      html  css  js  c++  java
  • 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
    ");
                continue;
            }
            vis[0] = true;
            dfs(0, 0, 1);
            printf("%d
    ", ans);
        }
    }
  • 相关阅读:
    如何在当前文件夹打开cmd(基于win10)
    Python命名空间和作用域
    Python定位模块_PYTHONPATH变量
    Python模块_import语句_from...import 函数名_from ... import *
    Python全局变量和局部变量
    Python变量作用域
    Python匿名函数_return语句
    Python函数参数
    Python按值传递参数和按引用传递参数
    Python函数调用
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7017577.html
Copyright © 2011-2022 走看看