HDU 2354 Another Brick in the Wall(优先队列,bfs)

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:

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
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
const int MAXN = 100 + 10;
const int INF = 0x3f3f3f3f;
char m[MAXN][MAXN];
int N, M;
int step[MAXN][MAXN];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
struct Point
{
    int x, y;
    int step;
    bool operator > (const Point& rhs) const
    {
        return step > rhs.step;
    }
};
int bfs()
{
    for(int i=0;i<=N;i++)
    {
        for(int j=0;j<=M;j++)
            step[i][j] = INF;
    }
    priority_queue<Point, vector<Point>, greater<Point> > Q;
    Point pre, now;
    for(int i=1;i<=M;i++)
    {
        pre.x = 1;
        pre.y = i;
        pre.step = 1;
        Q.push(pre);
    }
    while(!Q.empty())
    {
        pre = Q.top();
        Q.pop();
        if(pre.x == N) return pre.step;
        for(int i=0;i<4;i++)
        {
            now.x = pre.x + dir[i][0];
            now.y = pre.y + dir[i][1];
            now.step = pre.step;
            if(now.x < 1 || now.x > N || now.y < 1 || now.y > M)
                continue;
            if(m[now.x][now.y] != m[pre.x][pre.y])
                now.step++;
            if(step[now.x][now.y] > now.step)
            {
                step[now.x][now.y] = now.step;
                Q.push(now);
            }
        }
    }
    return 1;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &N, &M);
        for(int i=1;i<=N;i++)
            scanf("%s", m[i] + 1);
        int ans = bfs();
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-08-08 22:07:19

HDU 2354 Another Brick in the Wall(优先队列,bfs)的相关文章

hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要花费1s+数字大小的时间. 比较麻烦的是需要记录路径.还要记录是在走路还是在打怪. 因为求最短路,所以可以使用bfs. 因为进过每一个点花费时间不同,所以可以使用优先队列. 因为需要记录路径,所以需要开一个数组,来记录经过节点的父节点.当然,记录方法不止一种. 上代码—— 1 #include <c

HDU 4198 Quick out of the Harbour(优先队列 + bfs)

解题思路: 直接的bfs,因为和时间有关,需要采用优先队列. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include

hdu 4006 The kth great number (优先队列+STB+最小堆)

The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 6637    Accepted Submission(s): 2671 Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a roun

HDU 1242 Rescue(优先队列+bfs)

题目地址:HDU 1242 这个题相比于普通的bfs有个特殊的地方,经过士兵时会额外消耗时间,也就是说此时最先搜到的时候不一定是用时最短的了.需要全部搜一遍才可以.这时候优先队列的好处就显现出来了.利用优先队列,可以让队列中的元素按时间排序,让先出来的总是时间短的,这样的话,最先搜到的一定是时间短的,就不用全部搜一遍了.PS:我是为了学优先队列做的这题..不是为了这题而现学的优先队列.. 代码如下: #include <iostream> #include <stdio.h> #i

HDU 1874-畅通工程续(Dijkstra+优先队列)

畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 28578    Accepted Submission(s): 10382 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行

HDU 5349 MZL&#39;s simple problem(优先队列)

MZL's simple problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 776    Accepted Submission(s): 375 Problem Description A simple problem Problem Description You have a multiple set,and now

hdu 1026 Ignatius and the Princess I (BFS+优先队列)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11700    Accepted Submission(s): 3653Special Judge Problem Description The Princess has been abducted by the BEelzebub

Ignatius and the Princess I (hdu 1026 优先队列+bfs+输出路径)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14624    Accepted Submission(s): 4634 Special Judge Problem Description The Princess has been abducted by the BEelzeb

HDU 1253 胜利大逃亡 NYOJ 523【BFS】

胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24608    Accepted Submission(s): 9427 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的