CSU 1355 Landmine clearance plan grid map clean up minimal mines make diagonal

Source: Internet
Author: User

Topic Link: Click to open the link

Description

Now there is a game map consisting of n rows of M -column cells, from top to bottom as line 1th, line 2nd, ..., nth row, from left to right in the order of 1th column, 2nd Column, ....., column m . You might want to use (x, y) to represent the cell in the y column of row x .
Currently you are located (1, 1) and you want to go to (N, M) and jump to the next map, but the map is covered with some of the same mines, the mine has a sensing radius of K, If the cell you're stepping into is not more than Kfrom a mine in Manhattan, then the mine will explode and hurt you, and the game will be over. But fortunately you can clean up any one of the mines on this map at the cost of a gold coin, so how many coins do you need to use at least to clear the mines to ensure a smooth move from (1, 1) to (N, M)? When you're in any cell, you can walk up, down, left, or right to adjacent cells, but you can't walk outside of the map.

Input

The first line of input contains an integer t (1 <= t <= 100), indicating that there is a total of T -Group test data.
For each set of test data, the first row contains three integers n, m, K (1 <= n, m<=, 0<= K < 30 ), meaning ibid. Then there are N rows, each with M characters, describing the game map. Which '. ' Represents a cell without a mine, ' * ' indicates a cell with a mine (if the mine is cleared, the lattice becomes a mine-free cell). Data ensure that the total number of mines does not exceed 200, (1, 1) and (N, M) The Manhattan distance of any one mine is greater than K.

Output

For each set of test data, one line is used to output an integer that indicates at least how many gold coins are needed to clear the mine.

Sample Input

35 5 1..*......*.....*......*. 4 6 1..*..................*. 3 3 0.*******.

Sample Output

013
Ideas:

We connect all the mines that can affect each other (the so-called interaction is that the explosion range of two mines will cover or block the road)

After that, only the following 4 lines will be on the way:

1. Lines connecting the left and upper borders

2. Left border and line with border

3. Lines connecting the lower and upper borders

4. The lower boundary and the line with the boundary

In fact, we have to do is to cut the line as little as possible, so that there is no such line after the cut to make the left and bottom boundary and the upper right edge of the link

Simplifying the lower left and upper right borders into a single point is to find a minimum cut.

Similar to the topic click the Open link

#include <cstdio> #include <algorithm> #include <string.h> #include <queue> #include < cstring> #include <cmath> #include <iostream> #include <vector>using namespace std; #include < cstdio> #include <cstring> #include <algorithm> #include <iostream>using namespace std;//point mark [0,n] const int N = 1005;const int M = 500010;const int INF = ~0u >> 2;template<class t>struct max_flow {int N;int Q [N], Sign;int head[n], level[n], cur[n], Pre[n];int nxt[m], pnt[m], E; T cap[m];void Init (int n) {this->n = n + 1; E = 0;std::fill (head, head + this->n,-1);}  Have to RW = 0void Add (int from, int. to, T C, t RW = 0) {Pnt[e] = to;cap[e] = c;nxt[e] = Head[from];head[from] = e++;p Nt[e] = From;cap[e] = rw;nxt[e] = head[to];head[to] = e++;}  BOOL Bfs (int s, int t) {sign = T;std::fill (level, Level + N,-1); int *front = Q, *tail = q;*tail++ = t; level[t] = 0;while (Front < tail && Level[s] = =-1) {int u = *front++;for (int e = Head[u]; E! =-1; e = Nxt[e]) {if (cap[e ^ 1] > 0 && level[pnt[e]] < 0) {Level[pnt[e]] = Level[u] + 1;*tail++ = Pnt[e];}}} return Level[s]! =-1;} void Push (int t, T &flow) {t mi = inf;int p = pre[t];for (int p = pre[t]; p! =-1; p = pre[pnt[p ^ 1]]) {mi = std::min (MI, cap[p]);} for (int p = pre[t]; p! =-1; p = pre[pnt[p ^ 1]]) {cap[p]-= Mi;if (!cap[p]) {sign = pnt[p ^ 1];} Cap[p ^ 1] + = mi;} Flow + = mi;} void Dfs (int u, int t, T &flow) {if (U = = t) {Push (t, flow); return;}  for (int &e = Cur[u]; e =-1; e = Nxt[e]) {if (Cap[e] > 0 && level[u]-1 = level[pnt[e]]) {Pre[pnt[e]] = E;dfs (Pnt[e], T, Flow), if (Level[sign] > Level[u]) {return;} sign = t;}}} T Dinic (int s, int t) {Pre[s] =-1; T flow = 0;while (Bfs (S, t)) {std::copy (head, head + N, cur);D FS (S, t, flow);} return flow;}}; Max_flow <int>f;typedef Pair<int, int> PII; #define PB Push_backconst int maxn = 55;int N, m, R, from, To;char Mp[maxn][maxn];vector<pii>g;int Dis (PII x, Pii y) {returnABS (X.first-y.first) + ABS (X.second-y.second);} void Build () {from = G.size () * 2 + 1, to = from + 1; F.init (to); for (int i = 0; i < g.size (); i++) {int x = g[i].first, y = g[i].second; F.add (i * 2, I * 2 + 1, 1); if (y-r <= 1 | | x + R >= N)//touch left or bottom boundary f.add (from, I * 2, INF); if (y + R >= m | | x- R <= 1)//Encounter right border or upper boundary f.add (I * 2 + 1, to, INF); for (int j = i+1; J < G.size (); j + +) {bool Touch = false;if (x = = G[j].f Irst | | y = = g[j].second) && dis (g[i], g[j]) <= 2 * r + 1) Touch = true;if (x! = G[j].first && y! = G[j].second && dis (g[i], g[j]) <= 2 * r + 2) Touch = True;if (Touch) {F.add (i * 2 + 1, J * 2, INF); F.add (J * 2 + 1, I * 2, INF);}}} void input () {g.clear (); scanf ("%d%d%d", &n, &m, &r); for (int i = 1; I <= n; i++) {scanf ("%s", Mp[i] + 1); fo R (Int j = 1; j <= M; j + +) if (mp[i][j] = = ' * ') G.PB (PII (i, J));}} int main () {int T; scanf ("%d", &t), while (t--) {input (), if (g.size () = = 0) {puts ("0"); continue;} Build ();p rintf ("%d\n", F.dinic (from, to));} return 0;}


CSU 1355 Landmine clearance plan grid map clean up minimal mines make diagonal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.