Problem Description:
After years as a brick-layer, you ' ve been called upon to analyze the structural integrity of various brick walls built by The Tetrad Corporation. Instead
of using regular-sized bricks, the tetrad Corporation seems overly fond of bricks made out of strange shapes. The structural integrity of a wall can be
Approximated by the fewest number of bricks that could is removed to create a gap from the top to the bottom. Can you determine.
Various odd walls created by tetrad?
Input:
Input to this problem would begin with a line containing a single integer X (1≤x≤100) indicating the number of data set S. Each data set consists of
Components:
A single line, "M-N" (1≤m,n≤20) where m and N indicate the height and width (in units), respectively, of a brick wall;
A series of M lines, each N alphabetic characters in length. Each character would indicate to which brick that unit of the wall belongs to. Note
That bricks'll be contiguous; Each unit of a brick would be adjacent (Diagonals does not count as adjacent) to another unit of that brick. Multiple
Bricks the same characters for their representation, if any bricks that use identical characters would not be Adja cent to all other. All
Letters'll be uppercase.
Output:
For each data set, output the fewest number of bricks to remove to create a gap that leads from some point at the top of T He wall, to some point at the
Bottom of the wall. Assume that bricks is in the fixed locations and do not "fall" if bricks is removed from beneath them. A Gap consists of contiguous
Units of removed bricks; Each unit of a gap must is adjacent (diagonals do not count) to another unit of the gap.
Sample Input:
3
5 7
Aabbccd
Effgghh
Iijjkkl
Mnnoopp
Qqrrsst
5 7
Aabbccd
Affbggd
Iijbkkd
Mnnoopd
Qqrrsst
6 7
Abcdeab
Abcfeab
Aeaabab
Acdaeeb
Ffgahij
Klmanop
Sample Output:
522
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath > #include <vector> #include <queue> #include <stack> #include <algorithm>using namespace std ; Const int MAXN = + 10;const int INF = 0x3f3f3f3f;char m[maxn][maxn];int N, m;int step[maxn][maxn];int dir[4][2] = {- 1, 0}, {1, 0}, {0, 1}, {0, -1}};struct point{int x, y; int step; BOOL operator > (const point& RHS) Const {return step > rhs.step; }};int BFS () {for (int. i=0;i<=n;i++) {for (int j=0;j<=m;j++) step[i][j] = INF; } priority_queue<point, Vector<point>, greater<point> > Q; Point Pre, now; for (int i=1;i<=m;i++) {pre.x = 1; Pre.y = i; Pre.step = 1; Q.push (pre); } while (! Q.empty ()) {pre = Q.top (); Q.pop (); if (pre.x = = N) return pre.step; for (int. i=0;i<4;i++) {now.x = pre.x + dir[i][0]; NOW.Y = Pre.y + dir[i][1]; Now.step = Pre.step; if (now.x < 1 | | now.x > N | | now.y < 1 | | now.y > M) continue; if (m[now.x][now.y]! = M[pre.x][pre.y]) now.step++; if (Step[now.x][now.y] > Now.step) {step[now.x][now.y] = Now.step; Q.push (now); }}} return 1;} int main () {int T; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n, &m); for (int i=1;i<=n;i++) scanf ("%s", M[i] + 1); int ans = BFS (); printf ("%d\n", ans); } return 0;}
HDU 2354 Another Brick in the Wall (priority queue, BFS)