2014
01-05

# 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 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.

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.

3
5 7
AABBCCD
EFFGGHH
IIJJKKL
MNNOOPP
5 7
AABBCCD
AFFBGGD
IIJBKKD
MNNOOPD
6 7
ABCDEAB
ABCFEAB
AEAABAB
ACDAEEB
FFGAHIJ
KLMANOP

5
2
2

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define inf 1<<30
struct Node{
int x,y,step;
bool operator < (const Node &p) const {
return p.step<step;
}
};
int n,m;
int Step[22][22];
char map[22][22];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

int bfs(){
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
Step[i][j]=inf;
priority_queue<Node>Q;
Node p,q;
for(int i=1;i<=m;i++){
p.x=1,p.y=i,p.step=1;
Q.push(p);
}
while(!Q.empty()){
p=Q.top();
Q.pop();
if(p.x==n)return p.step;
for(int i=0;i<4;i++){
q.x=p.x+dir[i][0];
q.y=p.y+dir[i][1];
q.step=p.step;
if(q.x<1||q.x>n||q.y<1||q.y>m)continue;
if(map[q.x][q.y]!=map[p.x][p.y])q.step++;
if(Step[q.x][q.y]>q.step){
Step[q.x][q.y]=q.step;
Q.push(q);
}
}
}
return 1;
}

int main(){
//  freopen("1.txt","r",stdin);
int _case;
scanf("%d",&_case);
while(_case–){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%s",map[i]+1);
int ans=bfs();
printf("%d\n",ans);
}
return 0;
}

1. 在方法1里面：

//遍历所有的边，计算入度
for(int i=0; i<V; i++)
{
degree = 0;