• poj 2688 Cleaning Robot bfs+dfs


    题目链接

    首先bfs, 求出两两之间的距离, 然后dfs就可以。

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <algorithm>
      4 #include <cstring>
      5 #include <cmath>
      6 #include <string>
      7 #include <queue>
      8 #include <vector>
      9 #include <map>
     10 #include <set>
     11 using namespace std;
     12 #define pb(x) push_back(x)
     13 #define ll long long
     14 #define mk(x, y) make_pair(x, y)
     15 #define mem(a) memset(a, 0, sizeof(a))
     16 #define lson l, m, rt<<1
     17 #define rson m+1, r, rt<<1|1
     18 #define mem1(a) memset(a, -1, sizeof(a))
     19 #define mem2(a) memset(a, 0x3f, sizeof(a))
     20 #define rep(i, a, n) for(int i = a; i<n; i++)
     21 #define ull unsigned long long
     22 typedef pair<int, int> pll;
     23 const double PI = acos(-1.0);
     24 const int inf = 1061109567;
     25 const double eps = 1e-8;
     26 const int mod = 1e9+7;
     27 const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
     28 char c[30][30];
     29 int dis[15][15], vis[30][30], n, m, cnt, ans, used[20];
     30 struct node
     31 {
     32     int x, y, step;
     33     node(){}
     34     node(int x, int y, int step):x(x), y(y), step(step){}
     35 };
     36 int bfs(pll s, pll e) {
     37     mem(vis);
     38     vis[s.first][s.second] = 1;
     39     queue <node> q;
     40     q.push(node(s.first, s.second, 0));
     41     while(!q.empty()) {
     42         node tmp = q.front(); q.pop();
     43         if(tmp.x == e.first && tmp.y == e.second)
     44             return tmp.step;
     45         for(int i = 0; i<4; i++) {
     46             int tmpx = tmp.x + dir[i][0];
     47             int tmpy = tmp.y + dir[i][1];
     48             if(tmpx>=0&&tmpx<n&&tmpy>=0&&tmpy<m&&!vis[tmpx][tmpy]&&c[tmpx][tmpy]!='x') {
     49                 q.push(node(tmpx, tmpy, tmp.step+1));
     50                 vis[tmpx][tmpy] = 1;
     51             }
     52         }
     53     }
     54     return 0;
     55 }
     56 void dfs(int now, int num, int now_dis) {
     57     if(now_dis>=ans)
     58         return ;
     59     if(num == cnt-1) {
     60         if(now_dis<ans) {
     61             ans = now_dis;
     62         }
     63         return ;
     64     }
     65     for(int i = 1; i<cnt; i++) {
     66         if(!used[i]) {
     67             used[i] = 1;
     68             dfs(i, num+1, now_dis+dis[now][i]);
     69             used[i] = 0;
     70         }
     71     }
     72 }
     73 pll point[20];
     74 int main()
     75 {
     76     while(scanf("%d%d", &m, &n)) {
     77         if(n+m==0)
     78             break;
     79         int sx, sy;
     80         mem(dis);
     81         cnt = 1;
     82         for(int i = 0; i<n; i++)
     83             scanf("%s", c[i]);
     84         for(int i = 0; i<n; i++) {
     85             for(int j = 0; j<m; j++) {
     86                 if(c[i][j] == 'o') {
     87                     c[i][j] = '0';
     88                     point[0].first = i;
     89                     point[0].second = j;
     90                 }
     91                 if(c[i][j] == '*') {
     92                     point[cnt].first = i;
     93                     point[cnt++].second = j;
     94                 }
     95             }
     96         }
     97         for(int i = 0; i<cnt; i++) {
     98             for(int j = i+1; j<cnt; j++) {
     99                 dis[i][j] = dis[j][i] = bfs(point[i], point[j]);
    100             }
    101         }
    102         int flag = 0;
    103         for(int i = 1; i<cnt; i++) {
    104             if(dis[0][i] == 0) {
    105                 flag = 1;
    106             }
    107         }
    108         if(flag) {
    109             puts("-1");
    110             continue;
    111         }
    112         mem(used);
    113         used[0] = 1;
    114         ans = inf;
    115         dfs(0, 0, 0);
    116         printf("%d
    ", ans);
    117     }
    118 }
  • 相关阅读:
    ui优化
    《行尸走肉:行军作战》移动端优化经验
    Git master branch has no upstream branch的解决
    在线压图
    Git 分支管理-git stash 和git stash pop
    ScriptableObject
    Git解决pull无法操作成功
    Unity自带IAP插件使用(googleplay)
    Unity苹果(iOS)内购接入(Unity内置IAP)
    VMWare 装mac os x 一个必备优化神器 beamoff
  • 原文地址:https://www.cnblogs.com/yohaha/p/5019206.html
Copyright © 2020-2023  润新知