首页 > ACM题库 > HDU-杭电 > hdu 2354 Another Brick in the Wall-最短路径[解题报告]C++
2014
01-05

hdu 2354 Another Brick in the Wall-最短路径[解题报告]C++

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
QQRRSST
5 7
AABBCCD
AFFBGGD
IIJBKKD
MNNOOPD
QQRRSST
6 7
ABCDEAB
ABCFEAB
AEAABAB
ACDAEEB
FFGAHIJ
KLMANOP

样例输出:

5
2
2


思路:初始化step[][]==inf,然后如果当前点p.step

#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;
    for (j = adj .begin(); j != adj .end(); ++j)
    {
    degree[*j]++;
    }
    }

    为什么每遍历一条链表,要首先将每个链表头的顶点的入度置为0呢?
    比如顶点5,若在顶点1、2、3、4的链表中出现过顶点5,那么要增加顶点5的入度,但是在遍历顶点5的链表时,又将顶点5的入度置为0了,那之前的从顶点1234到顶点5的边不是都没了吗?