poj 3057 Evacuation

占位置。。

  1 /*Author :usedrose  */
  2 /*Created Time :2015/7/27 0:51:12*/
  3 /*File Name :2.cpp*/
  4 #include <cstdio>
  5 #include <iostream>
  6 #include <algorithm>
  7 #include <sstream>
  8 #include <cstdlib>
  9 #include <cstring>
 10 #include <climits>
 11 #include <vector>
 12 #include <string>
 13 #include <ctime>
 14 #include <cmath>
 15 #include <deque>
 16 #include <queue>
 17 #include <stack>
 18 #include <set>
 19 #include <map>
 20 #define INF 0x3f3f3f3f
 21 #define eps 1e-8
 22 #define pi acos(-1.0)
 23 #define MAXN 15
 24 #define MAXM 10010
 25 #define OK cout << "ok" << endl;
 26 #define o(a) cout << #a << " = " << a << endl
 27 #define o1(a,b) cout << #a << " = " << a << "  " << #b << " = " << b << endl
 28 using namespace std;
 29 typedef long long LL;
 30 const int dx[4] = { -1, 0, 0, 1 }, dy[4] = { 0, -1, 1, 0 };
 31 int V;
 32 vector<int> G[MAXM];
 33 int match[MAXM];
 34 bool used[MAXM];
 35 int dist[MAXN][MAXN][MAXN][MAXN];
 36 char field[MAXN][MAXN];
 37 int X, Y;
 38 vector<int> dX, dY, pX, pY;
 39 
 40 void addedge(int u, int v)
 41 {
 42     G[u].push_back(v);
 43     G[v].push_back(u);
 44 }
 45 
 46 bool dfs(int v)
 47 {
 48     used[v] = true;
 49     for (int i = 0; i < G[v].size(); ++i) {
 50         int u = G[v][i], w = match[u];
 51         if (w < 0 || !used[w] && dfs(w)) {
 52             match[v] = u;
 53             match[u] = v;
 54             return true;
 55         }
 56     }
 57     return false;
 58 }
 59 
 60 void bfs(int x, int y, int d[MAXN][MAXN])
 61 {
 62     queue<int> qx, qy;
 63     d[x][y] = 0;
 64     qx.push(x);
 65     qy.push(y);
 66     while (!qx.empty()) {
 67         x = qx.front(); qx.pop();
 68         y = qy.front(); qy.pop();
 69         for (int k = 0; k < 4; ++k) {
 70             int x2 = x + dx[k], y2 = y + dy[k];
 71             if (0 <= x2 && x2 < X && 0 <= y2 && y2 < Y && field[x2][y2] == '.' && d[x2][y2] < 0) {
 72                 d[x2][y2] = d[x][y] + 1;
 73                 qx.push(x2);
 74                 qy.push(y2);
 75             }
 76         }
 77     }
 78 }
 79 
 80 
 81 void solve()
 82 {
 83     int n = X*Y;
 84     dX.clear(); dY.clear();
 85     pX.clear(); pY.clear();
 86     memset(dist, -1, sizeof(dist));
 87     
 88     for (int x = 0; x < X; ++x) {
 89         for (int y = 0; y < Y; ++y) {
 90             if (field[x][y] == 'D') {
 91                 dX.push_back(x);
 92                 dY.push_back(y);
 93                 bfs(x, y, dist[x][y]);
 94             }
 95             else if (field[x][y] == '.') {
 96                 pX.push_back(x);
 97                 pY.push_back(y);
 98             }
 99 
100         }
101     }
102 
103     int d = dX.size(), p = pX.size();
104     for (int i = 0; i < n*d; ++i)
105         G[i].clear();
106     for (int i = 0; i < d; ++i) {
107         for (int j = 0; j < p; ++j) {
108             if (dist[dX[i]][dY[i]][pX[j]][pY[j]] >= 0) {
109                 for (int k = dist[dX[i]][dY[i]][pX[j]][pY[j]]; k <= n; ++k) {
110                     addedge((k - 1)*d + i, n*d + j);
111                 }
112             }
113         }
114     }
115 
116     if (p == 0) {
117         cout << 0 << endl;
118         return;
119     }
120     int num = 0;
121     memset(match, -1, sizeof(match));
122     for (int v = 0; v < n*d; ++v) {
123         memset(used, 0, sizeof(used));
124         if (dfs(v)) {
125             if (++num == p) {
126                 cout << v / d + 1 << endl;
127                 return;
128             }
129         }
130     }
131     cout << "impossible" << endl;
132 }
133 
134 
135 int main()
136 {
137     //freopen("data.in","r",stdin);
138     //freopen("data.out","w",stdout);
139     cin.tie(0);
140     ios::sync_with_stdio(false);
141     int T;
142     cin >> T;
143     while (T--) {
144         cin >> X >> Y;
145         for (int i = 0; i < X; ++i)
146             cin >> field[i];
147         solve();
148     }
149     return 0;
150 }
View Code

 

posted @ 2015-07-27 10:11  UsedRose  阅读(195)  评论(0编辑  收藏  举报