hdu 3468 Treasure Hunting【Bfs最短路+Dfs+二分匹配】

Treasure Hunting

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1858    Accepted Submission(s): 497

Problem Description

Do you like treasure hunting? Today, with one of his friend, iSea is on a venture trip again. As most movie said, they find so many gold hiding in their trip.
Now iSea’s clever friend has already got the map of the place they are going to hunt, simplify the map, there are three ground types:

● '.' means blank ground, they can get through it
● '#' means block, they can’t get through it
● '*' means gold hiding under ground, also they can just get through it (but you won’t, right?)

What makes iSea very delighted is the friend with him is extraordinary justice, he would not take away things which doesn’t belong to him, so all the treasure belong to iSea oneself! 
But his friend has a request, he will set up a number of rally points on the map, namely 'A', 'B' ... 'Z', 'a', 'b' ... 'z' (in that order, but may be less than 52), they start in 'A', each time friend reaches to the next rally point in the shortest way, they have to meet here (i.e. iSea reaches there earlier than or same as his friend), then start together, but you can choose different paths. Initially, iSea’s speed is the same with his friend, but to grab treasures, he save one time unit among each part of road, he use the only one unit to get a treasure, after being picked, the treasure’s point change into blank ground.
Under the premise of his friend’s rule, how much treasure iSea can get at most?

Input

There are several test cases in the input.

Each test case begin with two integers R, C (2 ≤ R, C ≤ 100), indicating the row number and the column number.
Then R strings follow, each string has C characters (must be ‘A’ – ‘Z’ or ‘a’ – ‘z’ or ‘.’ or ‘#’ or ‘*’), indicating the type in the coordinate.

The input terminates by end of file marker.

Output

For each test case, output one integer, indicating maximum gold number iSea can get, if they can’t meet at one or more rally points, just output -1.

Sample Input

2 4

A.B.

***C

2 4

A#B.

***C

Sample Output

1

2

Author

iSea @ WHU

Source

2010 ACM-ICPC Multi-University Training Contest3——Host by WHU

 

题目大意:有这么个人,起点是A,按照A-B-C-D-E.....-Z-a-b-c-d-e-............z的顺序走,每一次走的路径都是最短路径,而且对于每次走的路径上的宝藏只能拿一个,或者不拿。问最多能够拿多少个宝藏。


思路:


1、首先对于每条最短路的解决方案,当然是Bfs。记录一个数组step【x】【y】表示到点(x,y)的时间. 起点记做ss,终点记做tt,维护:step【xx】【yy】=step【x】【y】+1(xx,yy是xy走到的下一步的坐标)如果从起点Bfs搜索到了终点,跳出,然后进行下一次路径的Bfs。


2、那么我们如何记录这个最短路径呢?我们在维护step【x】【y】的过程,也就是供给我们从起点搜索到了终点之后回溯递归寻找路径所用,当我们Bfs找到了一条最短路之后,我们再从终点Dfs找起点,对应step【xx】【yy】==step【x】【y】-1,那么xx,yy这个点就一定是某条最短路径上的某个点(最短路径当然不唯一)。


3、然后我们将点进行标号,地图左上角定为1,地图右下角定为n*m,然后对应每一次路径作为匹配点,将每一个Dfs回溯路径上遇到的宝藏点作为被匹配点。然后我们跑一遍匈牙利二分匹配算法即可,输出答案。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct zuobiao
{
    int x,y;
}now,nex;
struct node
{
    int from;
    int to;
    int w;
    int next;
}e[151515];
int head[200*200];
int match[200*200];
int r,c,sx,sy;
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
char a[150][150];
int vis[150][150];
int step[150][150];
int vis2[200*200];
int contz,cont;
void add(int from,int to)
{
    e[cont].from=from;
    e[cont].to=to;
    e[cont].next=head[from];
    head[from]=cont++;
}
int Bfs(int x,int y)
{
    memset(step,0,sizeof(step));
    step[x][y]=2;
    queue<zuobiao >s;
    now.x=x;
    now.y=y;
    s.push(now);
    while(!s.empty())
    {
        now=s.front();
        if(a[now.x][now.y]==a[x][y]+1||a[x][y]=='Z'&&a[now.x][now.y]=='a')
        {
            sx=now.x;
            sy=now.y;
            return 1;
        }
        s.pop();
        for(int i=0;i<4;i++)
        {
            nex.x=now.x+fx[i];
            nex.y=now.y+fy[i];
            if(nex.x>=0&&nex.x<r&&nex.y>=0&&nex.y<c&&step[nex.x][nex.y]==0&&a[nex.x][nex.y]!='#')
            {
                step[nex.x][nex.y]=step[now.x][now.y]+1;
                s.push(nex);
            }
        }
    }
    return 0;
}
void Dfs_build(int x,int y)
{
    if(a[x][y]=='*')
    {
        add(contz,x*c+y+1);
    }
    vis[x][y]=1;
    for(int i=0;i<4;i++)
    {
        int xx=x+fx[i];
        int yy=y+fy[i];
        if(xx>=0&&xx<r&&yy>=0&&yy<c&&vis[xx][yy]==0&&step[xx][yy]==step[x][y]-1&&a[xx][yy]!='#')
        {
            Dfs_build(xx,yy);
        }
    }
}
int find(int u)
{
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(vis2[v]==0)
        {
            vis2[v]=1;
            if(match[v]==-1||find(match[v]))
            {
                match[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
void Slove()
{
    memset(match,-1,sizeof(match));
    int output=0;
    for(int i=0;i<contz;i++)
    {
        memset(vis2,0,sizeof(vis2));
        if(find(i))output++;
    }
    printf("%d\n",output);
}
int main()
{
    while(~scanf("%d%d",&r,&c))
    {
        contz=0;
        int cont=0;
        int sum=0;
        for(int i=0;i<r;i++)
        {
            scanf("%s",a[i]);
            for(int j=0;j<c;j++)
            {
                if(a[i][j]=='A')
                {
                    sx=i;sy=j;
                }
                if(a[i][j]>='A'&&a[i][j]<='Z'||a[i][j]>='a'&&a[i][j]<='z')sum++;
            }
        }
        cont=0;
        memset(head,-1,sizeof(head));
        sum--;
        while(1)
        {
            int tt=Bfs(sx,sy);
            if(tt==1)
            {
                //printf("---------------\n");
                memset(vis,0,sizeof(vis));
                Dfs_build(sx,sy);
                sum--;
                contz++;
            }
            else break;
        }
        //printf("%d\n",sum);
        if(sum==0)
        {
            Slove();
        }
        else printf("-1\n");
    }
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值