HDU-2354-Another Brick in the Wall

来源:互联网 发布:讲文明知礼仪内容15个 编辑:程序博客网 时间:2024/05/17 23:34

Another Brick in the Wall

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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 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
35 7AABBCCDEFFGGHHIIJJKKLMNNOOPPQQRRSST5 7AABBCCDAFFBGGDIIJBKKDMNNOOPDQQRRSST6 7ABCDEABABCFEABAEAABABACDAEEBFFGAHIJKLMANOP

Sample Output
522

Source
HDU 2008'8 公开赛(非原创)

Recommend
lcy
题目网址:Another Brick in the Wall

题意:第一个数字为几组测试数据,然后给你M,N;下面是一个M行N列的矩阵,如果一个字母上下左右四个方向有相同的字母那么把他们看为一块砖,问你最少敲多少块砖可以敲出来一条裂缝把这个矩阵分为两半,裂缝必须是从第一行到最后一行的;(样例中我染红的部分即为最少敲的砖);

思路:BFS;先把第一行的都放入队列,使用优先队列广搜就行了;不过注意这里面的标记数组里面存的是步数,而且可以重复使用,比如你第一次访问到这个地方时用的步数为3,第二次为二那么就要修改;具体看代码把;

#include <bits/stdc++.h>using namespace std;int m,n,v[25][25];string MAP[25];struct node{int x,y,step;bool operator < (const node &p) const{return p.step<step;//步数少的优先 }};int bfs(){int MOVE[4][2]={{1,0},{0,1},{-1,0},{0,-1}};priority_queue<node>q;node now;for(int i=0;i<n;i++)//把第一行的步数置为1都放入队列 {now.y=0;now.x=i;now.step=1;v[now.y][now.x]=1;q.push(now);}while(!q.empty()){now=q.top();q.pop();if(now.y==m-1){return now.step;}for(int i=0;i<4;i++){node next;next.x=now.x+MOVE[i][0];next.y=now.y+MOVE[i][1];next.step=now.step;if(next.x>=0&&next.y>=0&&next.x<n&&next.y<m)//如果这个地方不越界 {if(MAP[now.y][now.x]!=MAP[next.y][next.x])//如果这个字母与上一个不一样,那么步数加1 {next.step=now.step+1;}if(v[next.y][next.x]==0||v[next.y][next.x]>next.step)//这个地方没被访问过或者是这次用的步数比上次的少那么更新这个v,next放入队列 {v[next.y][next.x]=next.step;q.push(next);}}}}return -1;}int main(){int T;cin>>T;while(T--){cin>>m>>n;for(int i=0;i<m;i++){cin>>MAP[i];}memset(v,0,sizeof(v));cout<<bfs()<<endl;}}



0 0