HDU - 2354 Another Brick in the Wall

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?
Input
Input to this problem will begin with a line containing a single integer X (1 ≤ X ≤ 100) indicating the number of data sets. Each data set consists of 
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.
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 the wall, to some point at the 
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.
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
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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值