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 be removed to create a gap from the top to the bottom. Can you determine that number for
various odd walls created by Tetrad?
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 be removed to create a gap from the top to the bottom. Can you determine that number for
various odd walls created by Tetrad?
two 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 will indicate to which brick that unit of the wall belongs to. Note
that bricks will be contiguous; each unit of a brick will be adjacent (diagonals do not count as adjacent) to another unit of that brick. Multiple
bricks may use the same characters for their representation, but any bricks that use identical characters will not be adjacent to each other. All
letters will be uppercase.
bottom of the wall. Assume that bricks are in fixed locations and do not "fall" if bricks are removed from beneath them. A gap consists of contiguous
units of removed bricks; each unit of a gap must be adjacent (diagonals do not count) to another unit of the gap.
3 5 7 AABBCCD EFFGGHH IIJJKKL MNNOOPP QQRRSST 5 7 AABBCCD AFFBGGD IIJBKKD MNNOOPD QQRRSST 6 7 ABCDEAB ABCFEAB AEAABAB ACDAEEB FFGAHIJ KLMANOP
5 2 2
题意:给你一个图,让你从图的上方走到下方,问最短步数是多少,连续的砖块是一步,具体也说不清,还是解释一下样例吧;
6 7 ABCDEAB ABCFEAB AEAABAB ACDAEEB FFGAHIJ KLMANOP比如这个样例,第一步可以走C砖,那么下一步可以走A砖,这样就可以从最上面走到最下面,所以输出最小步数是2。用step[][]存步数,然后用优先队列,bfs即可;#include <bits/stdc++.h> using namespace std; #define inf 0x3f3f3f3f int Step; int M, N; char Map[30][30]; //存图 int step[30][30]; //记录敲砖的次序 int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0}; struct node { int x , y; //x代表横坐标,y代表纵坐标 int step; //步数 bool operator <(const node &p) const { return p.step < step; } }pre, nex; bool MAP(int x, int y) { return x>=0 && y>=0 && x<N && y<M; } priority_queue<node> q; void bfs() { for(int i = 0; i < N; i++) { //敲第一行的所有砖块 pre.x = i; pre.y = 0; pre.step = 1; step[0][i] = 1; q.push(pre); } while(!q.empty()) { nex = q.top(); q.pop(); if(nex.y == M-1) { Step = nex.step; return ; } for(int i = 0; i < 4; i++) { int dx = d[i][0] + nex.x; int dy = d[i][1] + nex.y; if(MAP(dx, dy)) { //判断是否越界 pre.x = dx; pre.y = dy; pre.step = nex.step; if(Map[dy][dx] != Map[nex.y][nex.x]) { //判断两个砖块是否一样 pre.step++; //如果不一样,就把步数加一 } if(step[dy][dx] > pre.step) { //判断这一步是否是最短的一步 step[dy][dx] = pre.step; q.push(pre); } } } } } int main() { int X; scanf("%d",&X); while(X--) { scanf("%d%d",&M,&N); for(int i = 0; i < M ; i++) { scanf("%s",Map[i]); } memset(step, inf, sizeof(step));//将每块砖的次序初始化为最大 bfs(); printf("%d\n",Step); } return 0; }