poj 3592 Instantaneous Transference

Instantaneous Transference
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 6523 Accepted: 1474

Description

It was long ago when we played the game Red Alert. There is a magic function for the game objects which is called instantaneous transfer. When an object uses this magic function, it will be transferred to the specified point immediately, regardless of how far it is.

Now there is a mining area, and you are driving an ore-miner truck. Your mission is to take the maximum ores in the field.

The ore area is a rectangle region which is composed by n × m small squares, some of the squares have numbers of ores, while some do not. The ores can't be regenerated after taken.

The starting position of the ore-miner truck is the northwest corner of the field. It must move to the eastern or southern adjacent square, while it can not move to the northern or western adjacent square. And some squares have magic power that can instantaneously transfer the truck to a certain square specified. However, as the captain of the ore-miner truck, you can decide whether to use this magic power or to stay still. One magic power square will never lose its magic power; you can use the magic power whenever you get there.

Input

The first line of the input is an integer T which indicates the number of test cases.

For each of the test case, the first will be two integers NM (2 ≤ NM ≤ 40).

The next N lines will describe the map of the mine field. Each of the N lines will be a string that contains M characters. Each character will be an integer X (0 ≤ X ≤ 9) or a '*' or a '#'. The integer X indicates that square has X units of ores, which your truck could get them all. The '*' indicates this square has a magic power which can transfer truck within an instant. The '#' indicates this square is full of rock and the truck can't move on this square. You can assume that the starting position of the truck will never be a '#' square.

As the map indicates, there are K '*' on the map. Then there follows K lines after the map. The next K lines describe the specified target coordinates for the squares with '*', in the order from north to south then west to east. (the original point is the northwest corner, the coordinate is formatted as north-south, west-east, all from 0 to N - 1,- 1).

Output

For each test case output the maximum units of ores you can take.  

Sample Input

1
2 2
11
1*
0 0

Sample Output

3

Source

提示

题意:

很久很久以前我们还在玩红色警戒。。。现在你有一辆游戏中的卡车,你需要在地图上收集尽可能多的矿石,地图为n*m(2<=n<=40,2<=m<=40)的矩阵,地图中有数字0~9,'#','*'组成,其中数字代表的是这一位置的矿石数,'#'代表的是墙壁,顾名思义墙是不可以走的,'*'表示传送门(该位置无矿石),可以将矿车传送到地图中的某一点(包括墙),车只能向下或向右走,以左上角为起点请求出矿石最大搜集量。

思路:

给地图点编号,如:

0,1,2,3

4,5,6,7

...

在地图上建立地图每个点的边,之后利用这些缩点,再建一个新图,在新图上跑一下最长路即可。

示例程序

Source Code

Problem: 3592		Code Length: 3673B
Memory: 776K		Time: 0MS
Language: GCC		Result: Accepted
#include <stdio.h>
#include <string.h>
struct
{
    int v,next;
}w[2][4000];
int h[2][3200],numw[2],val[3200],sumval[3200],dfn[3200],low[3200],vis[3200],stack[80000],id[3200],count,deep,top;
void insert(int u,int v,int flag)		//flag表示当前建立的是缩点前还是缩点后的图([0][]缩点前[1][]缩点后)
{
    w[flag][numw[flag]].v=v;
    w[flag][numw[flag]].next=h[flag][u];
    h[flag][u]=numw[flag];
    numw[flag]++;
}
void tarjan(int t)
{
    int pos,i;
    dfn[t]=deep;
    low[t]=deep;
    deep++;
    vis[t]=1;
    stack[top]=t;
    top++;
    for(i=h[0][t];i!=-1;i=w[0][i].next)
    {
        pos=w[0][i].v;
        if(dfn[pos]==-1)
        {
            tarjan(pos);
            if(low[t]>low[pos])
            {
                low[t]=low[pos];
            }
        }
        else if(vis[pos]==1&&low[t]>dfn[pos])
        {
            low[t]=dfn[pos];
        }
    }
    if(dfn[t]==low[t])
    {
        sumval[count]=0;
        do
        {
            top--;
            pos=stack[top];
            sumval[count]=sumval[count]+val[pos];			//分量中每个点矿石数加和
            id[pos]=count;						//记录每个点所属于哪个分量
            vis[pos]=0;
        }while(t!=pos);
        count++;
    }
}
void bulid(int n)
{
    int i,i1;
    for(i=0;n>i;i++)						//枚举地图中每个点
    {
        for(i1=h[0][i];i1!=-1;i1=w[0][i1].next)
        {
            if(id[i]!=id[w[0][i1].v])				//起点和终点不属于同一个分量就建边
            {
                insert(id[i],id[w[0][i1].v],1);
            }
        }
    }
}
int spfa()
{
    int v[3200],d[3200],q[80000],i,pos,f=0,top=0,max;
    for(i=0;count>i;i++)
    {
        v[i]=0;
        d[i]=0;
    }
    pos=id[0];					//从左上角开始
    v[pos]=1;
    d[pos]=sumval[pos];
    q[top]=pos;
    top++;
    while(f<top)
    {
        v[q[f]]=0;
        for(i=h[1][q[f]];i!=-1;i=w[1][i].next)
        {
            pos=w[1][i].v;
            if(d[pos]<d[q[f]]+sumval[pos])
            {
                d[pos]=d[q[f]]+sumval[pos];
                if(v[pos]==0)
                {
                    q[top]=pos;
                    top++;
                    v[pos]=1;
                }
            }
        }
        f++;
    }
    max=d[0];
    for(i=1;count>i;i++)
    {
        if(max<d[i])
        {
            max=d[i];
        }
    }
    return max;
}
int main()
{
    int t,i,i1,i2,n,m,x,y;
    char map[40][41];
    scanf("%d",&t);
    for(i=1;t>=i;i++)
    {
        scanf("%d %d",&n,&m);
        memset(h,-1,sizeof(h));
        memset(dfn,-1,sizeof(dfn));
        memset(low,-1,sizeof(low));
        memset(vis,0,sizeof(vis));
        numw[0]=0;
        numw[1]=0;
        count=0;
        deep=0;
        top=0;
        for(i1=0;n>i1;i1++)
        {
            scanf("%s",map[i1]);
        }
        for(i1=0;n>i1;i1++)
        {
            for(i2=0;m>i2;i2++)
            {
                if(map[i1][i2]!='#')
                {
                    if(i1+1<n&&map[i1+1][i2]!='#')
                    {
                        insert(i1*m+i2,(i1+1)*m+i2,0);			//与下一行同列的点建立边
                    }
                    if(i2+1<m&&map[i1][i2+1]!='#')
                    {
                        insert(i1*m+i2,i1*m+i2+1,0);			//与同行下一列的点建立边
                    }
                    if(map[i1][i2]!='*')				//该点有矿石
                    {
                        val[i1*m+i2]=map[i1][i2]-'0';
                    }
                    else						//传送门,同样要建立边
                    {
                        val[i1*m+i2]=0;
                        scanf("%d %d",&x,&y);
                        if(map[x][y]!='#')
                        {
                             insert(i1*m+i2,x*m+y,0);
                        }
                    }
                }
            }
        }
        for(i1=0;n*m>i1;i1++)
        {
            if(dfn[i1]==-1)				//Tarjan缩点
            {
                tarjan(i1);
            }
        }
        bulid(n*m);					//建立新图
        printf("%d\n",spfa());
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值