Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 5076 | Accepted: 1103 |
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 N, M (2 ≤ N, M ≤ 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,M - 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
题意:有一个矩阵,每个元素是数字或*或#,#代表障碍物,不能经过,若经过*,则能传送到指定的地方,而走过数字时则获得数字的值,只能向右走或下走,问最多能获得的值的多少。
思路:将每一格与在它右边或下边的格连一条有向边,有*的格子和指定的格子连一条有向边,然后进行缩点,缩点后用树dp求最长路。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <vector> #include <cmath> #include <cstdlib> #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) #define ll long long using namespace std; const int maxn=2005; struct node { int v,next; }edge[maxn*maxn]; int G[maxn],NG[maxn],val[maxn],sum[maxn]; int low[maxn],dfn[maxn],scc[maxn],stack[maxn]; char map[50][50]; bool ins[maxn],vis[maxn]; int n,m,num,top,snum,cnt; void init() { memset(G,-1,sizeof(G)); memset(NG,-1,sizeof(NG)); num=0; } void add(int *head,int u,int v) { edge[num].v=v; edge[num].next=head[u]; head[u]=num++; } bool ok(int x,int y) { if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='#') return true; return false; } void input() { int x,y; scanf("%d%d",&n,&m); memset(val,0,sizeof(val)); for(int i=0;i<n;i++) { scanf("%s",map[i]); for(int j=0;j<m;j++) if(map[i][j]>='0'&&map[i][j]<='9') val[i*m+j]=map[i][j]-'0'; } for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(map[i][j]=='#') continue; if(ok(i,j+1)) add(G,i*m+j,i*m+j+1); if(ok(i+1,j)) add(G,i*m+j,(i+1)*m+j); if(map[i][j]=='*') { scanf("%d%d",&x,&y); if(map[x][y]!='#') add(G,i*m+j,x*m+y); } } } void dfs(int u) { int x; low[u]=dfn[u]=++cnt; stack[top++]=u; ins[u]=true; for(int i=G[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(!dfn[v]) { dfs(v); low[u]=min(low[u],low[v]); } else if(ins[v]) low[u]=min(low[u],dfn[v]); } if(low[u]==dfn[u]) { do{ x=stack[--top]; ins[x]=false; sum[snum]+=val[x]; scc[x]=snum; }while(x!=u); snum++; } } void tarjan() { memset(dfn,0,sizeof(dfn)); memset(ins,false,sizeof(ins)); memset(sum,0,sizeof(sum)); top=cnt=snum=0; for(int i=0;i<n*m;i++) if(!dfn[i]) dfs(i); } int dfs1(int u) { int ret=sum[u]; int maxv=0; for(int i=NG[u];i!=-1;i=edge[i].next) { int v=edge[i].v; int t=dfs1(v); if(t>maxv) maxv=t; } return ret+maxv; } void solve() { for(int u=0;u<n*m;u++) for(int i=G[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(scc[u]!=scc[v]) add(NG,scc[u],scc[v]); } printf("%d\n",dfs1(scc[0])); } int main() { int t; scanf("%d",&t); while(t--) { init(); input(); tarjan(); solve(); } return 0; }