hdu 2354 Integer Intervals【Bfs+暴力】

来源:互联网 发布:单片机智能车 编辑:程序博客网 时间:2024/06/13 04:27

Integer Intervals

Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 13671

 

Accepted: 5824

Description

An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b. 
Write a program that: finds the minimal number of elements in a set containing at least two different integers from each interval.

Input

The first line of the input contains the number of intervals n, 1 <= n <= 10000. Each of the following n lines contains two integers a, b separated by a single space, 0 <= a < b <= 10000. They are the beginning and the end of an interval.

Output

Output the minimal number of elements in a set containing at least two different integers from each interval.

Sample Input

4

3 6

2 4

0 2

4 7

Sample Output

4

Source

CEOI 1997

Another Brick in the Wall

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 324    Accepted Submission(s): 122

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

 

 

Source

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

 

题目大意:有n*m那么大个矩阵,里边每个字母表示不同种类的障碍,求一个从第一行到最后一行使用最少种类的障碍数。

 

思路:暴力Bfs,枚举起点,从1枚举到m,从所有起点都走一遍Bfs,开始用二维判重+优先队列,发现有bug,要改成三维判重才行,但是感觉三维判重还是有点复杂,所以还是搞二维判重了,只不过复杂度相对高一些,不过100*100的图也是很小的,所以我就暴力判断了。

核心判断:

if(vis【nex.x】【nex.y】>vis【now.x】【now.y】+tmp)【tmp在两个点字母不同的时候为1 ,相同的时候为0】s.push(nex)&&更新vis【nex.x】【nex.y】=vis【now.x】【now.y】+tmp、


AC代码:


#include<stdio.h>#include<queue>#include<string.h>using namespace std;struct zuobiao{    int x,y;    char pre;    int output;}now,nex;int ans;char a[1005][1005];int vis[1005][1005];int fx[4]={0,0,1,-1};int fy[4]={1,-1,0,0};int n,m;void Bfs(int x,int y){    queue<zuobiao >s;    memset(vis,-1,sizeof(vis));    vis[x][y]=1;    now.x=x;    now.y=y;    now.pre=a[x][y];    s.push(now);    while(!s.empty())    {        now=s.front();        if(now.x==n-1)        {            ans=min(vis[now.x][now.y],ans);        }        s.pop();        for(int i=0;i<4;i++)        {            nex.x=now.x+fx[i];            nex.y=now.y+fy[i];            nex.pre=now.pre;            if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m)            {                int tmp=0;                if(a[nex.x][nex.y]!=now.pre)                {                    tmp=1;                    nex.pre=a[nex.x][nex.y];                }                if(vis[nex.x][nex.y]==-1)                {                    vis[nex.x][nex.y]=vis[now.x][now.y]+tmp;                    s.push(nex);                }                else                {                    if(vis[nex.x][nex.y]>vis[now.x][now.y]+tmp)                    {                        vis[nex.x][nex.y]=vis[now.x][now.y]+tmp;                        s.push(nex);                    }                }            }        }    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        for(int i=0;i<n;i++)        {            scanf("%s",a[i]);        }        ans=0x3f3f3f3f;        for(int i=0;i<m;i++)        {            Bfs(0,i);        }        printf("%d\n",ans);    }}







0 0