Instantaneous Transference--POJ3592Tarjan缩点+搜索
Instantaneous Transference
Time Limit: 5000MS | Memory Limit: 65536K |
---|
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
Source
South Central China 2008 hosted by NUDT
题意:在一个矿区,你驾驶着一辆采矿的卡车,你的任务是采集最大数量的矿石,矿区是一个长方形的区域,包含n*m个的小方块,有些矿区有矿石,有的没有,矿石采完后不能再生,采矿车的起始位置在西北角(0,0),它只能向东面或者南面相邻的方格,其中的一些方块有魔法,能将矿车瞬间移动到指定的位置,作为驾驶员,你可以决定是否使用这个魔法,魔法不会消失。
思路:首先是建图,建完图发现会形成强连通分量,所以我们先进行缩点,缩完点后会形成一个DAG图,然后进行DFS搜索一边就可以,不过在建图的时候要注意传送到外面的点也要建图
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <algorithm>
using namespace std;
const int Max = 2000;
typedef struct node
{
int v,next;
}Line;
Line Li[Max*1000];
int Head1[Max],Head2[Max],top;
int dfn[Max],low[Max],vis[Max],dep,pre[Max];
int Dp[Max],num;
char str[50][50];
int va[Max],a[Max];
int T,n,m;
int dir[][2]={{0,1},{1,0}};
stack<int>S;
void Init()
{
memset(Head1,-1,sizeof(Head1));
memset(Head2,-1,sizeof(Head2));
memset(vis,0,sizeof(vis));
memset(va,0,sizeof(va));
memset(a,0,sizeof(a));
memset(pre,-1,sizeof(pre));
dep = 0 ;num = 0;
}
void AddEdge1(int u,int v)
{
Li[top].v = v; Li[top].next =Head1[u];
Head1[u] = top++;
}
void AddEdge2(int u,int v)
{
Li[top].v = v; Li[top].next = Head2[u];
Head2[u] = top++;
}
bool Judge(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m&&str[x][y]!='#')
{
return true;
}
return false;
}
void DFS(int x,int y)
{
for(int i = 0;i<2;i++)
{
int Fx = x+dir[i][0];
int Fy = y+dir[i][1];
if(Judge(Fx,Fy))
{
AddEdge1(x*m+y,Fx*m+Fy);
}
}
}
void Tarjan(int u) //Tarjan强连通缩点
{
dfn[u] = low[u] =dep++;
vis[u]=1;
S.push(u);
for(int i=Head1[u];i!=-1;i=Li[i].next)
{
if(vis[Li[i].v]==1)
{
low[u] = min(low[u],dfn[Li[i].v]);
}
else if(vis[Li[i].v]==0)
{
Tarjan(Li[i].v);
low[u] = min(low[u],low[Li[i].v]);
}
}
if(dfn[u]==low[u])
{
while(!S.empty())
{
int v=S.top();
S.pop();
pre[v] = num;
vis[v] = 2;
a[num]+=va[v];
if(u==v)
{
break;
}
}
num++;
}
}
int dfs(int u)//搜索最大值
{
if(!vis[u])
{
vis[u]=1;
int ans=0;
for(int i=Head2[u];i!=-1;i=Li[i].next)
{
ans = max(ans,dfs(Li[i].v));
}
a[u]+=ans;
}
return a[u];
}
int main()
{
scanf("%d",&T);
int x,y;
while(T--)
{
scanf("%d %d",&n,&m);
Init();
for(int i=0;i<n;i++)
{
scanf("%s",str[i]);
}
for(int i=0;i<n;i++)//建图
{
for(int j=0;j<m;j++)
{
if(str[i][j] == '#')
{
continue;
}
DFS(i,j);
if(str[i][j]=='*')
{
scanf("%d %d",&x,&y);
AddEdge1(i*m+j,x*m+y);
}
else
{
va[i*m+j]=str[i][j]-'0';
}
}
}
for(int i=0;i<n*m;i++)
{
if(!vis[i])
{
Tarjan(i);
}
}
for(int i=0;i<n*m;i++) //重新建图
{
for(int j=Head1[i];j!=-1;j = Li[j].next)
{
if(pre[i]!=pre[Li[j].v])
{
AddEdge2(pre[i],pre[Li[j].v]);
}
}
}
memset(vis,0,sizeof(vis));
printf("%d\n",dfs(pre[0]));
}
return 0;
}
/*
1
10 10
1167811678
1*77811678
1*70001678
1*77811678
1#77800078
1#77837###
1*00037###
1*34000###
1*3451*778
37###1#345
5 5
5 5
5 6
5 7
5 8
5 9
5 10
ans=130
*/
Instantaneous Transference--POJ3592Tarjan缩点+搜索的更多相关文章
- poj3592 Instantaneous Transference tarjan缩点+建图
//给一个n*m的地图.坦克从(0 , 0)開始走 //#表示墙不能走,*表示传送门能够传送到指定地方,能够选择也能够选择不传送 //数字表示该格的矿石数, //坦克从(0,0)開始走.仅仅能往右和往 ...
- poj 3592 Instantaneous Transference 【SCC +缩点 + SPFA】
Instantaneous Transference Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6204 Accep ...
- POJ 3592 Instantaneous Transference(强连通+DP)
POJ 3592 Instantaneous Transference 题目链接 题意:一个图.能往右和下走,然后有*能够传送到一个位置.'#'不能走.走过一个点能够获得该点上面的数字值,问最大能获得 ...
- poj 3592 Instantaneous Transference 缩点+最长路
题目链接 给一个n*m的图, 从0, 0这个点开始走,只能向右和向下. 图中有的格子有值, 求能获得的最大值. 其中有些格子可以传送到另外的格子, 有些格子不可以走. 将图中的每一个格子都看成一个点, ...
- Instantaneous Transference(强连通分量及其缩点)
http://poj.org/problem?id=3592 题意:给出一个n*m的矩阵,左上角代表起始点,每个格子都有一定价值的金矿,其中‘#’代表岩石不可达,‘*’代表时空门可以到达指定格子,求出 ...
- POJ 3592 Instantaneous Transference(强联通分量 Tarjan)
http://poj.org/problem?id=3592 题意 :给你一个n*m的矩阵,每个位置上都有一个字符,如果是数字代表这个地方有该数量的金矿,如果是*代表这个地方有传送带并且没有金矿,可以 ...
- POJ3592 Instantaneous Transference tarjan +spfa
链接:http://poj.org/problem?id=3592 题意:题目大意:给定一个矩阵,西南角为出发点,每个单位都有一订价值的金矿(#默示岩石,不成达,*默示时佛门,可以达到指定单位),队# ...
- POJ3592 Instantaneous Transference题解
题意: 给一个矩形,矩形中某些点有一定数量的矿石,有些点为传送点,有些点为障碍.你驾驶采矿车(ore-miner truck,我也不知道是什么),从左上角出发,采尽量多的矿石,矿石不可再生.不能往左边 ...
- POJ3592 Instantaneous Transference 强连通+最长路
题目链接: id=3592">poj3592 题意: 给出一幅n X m的二维地图,每一个格子可能是矿区,障碍,或者传送点 用不同的字符表示: 有一辆矿车从地图的左上角(0,0)出发, ...
随机推荐
- iscoll制作顶部可以左右滑动的tab
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- ASP.Net的两种开发模式
一.ASP.Net的两种开发模式 1.1 ASP.Net WebForm的开发模式 (1)处理流程 在传统的WebForm模式下,我们请求一个例如http://www.aspnetmvc.com/bl ...
- Spark java.lang.outofmemoryerror gc overhead limit exceeded 与 spark OOM:java heap space 解决方法
引用自:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c42246 ...
- Bypass WAF Cookbook
PS.之前一直想把零零碎碎的知识整理下来,作为知识沉淀下来,正好借着wooyun峰会的机会将之前的流程又梳理了一遍,于是就有了下文.也希望整理的内容能给甲方工作者或则白帽子带来一些收获. 0x00 概 ...
- SQL学习整理_1
数据库是保存表和其他相关SQL结构的容器. 列是存储在表中的一块同类型数据. 行是一组能够描述某个事物的列的集合. SQL不区分大小写,但建议命令采用大写,表名采用小写,便于读写. 建立数据库 CRE ...
- linux查看进程启动时间
1. ps axu 2. 精确查看 for pid in $(pgrep httpd); do echo -n "${pid} " ; ps -p ${pid} -o lstart ...
- DS-5建工程
DS-5教程-使用ARM DS-5 和DSTREAM仿真器调试裸机程序 http://bbs.elecfans.com/jishu_453909_1_1.html i2c( 楼主 )2014-10-1 ...
- Hadoop基础知识
摘要:Hadoop的安装目录了解.etc的核心配置项.hadoop的启动.HDFS文件的block块级副本的存放策略.checkpoint触发设置. 1.hadoop目录了解 bin:可执行文件,命令 ...
- jee websocket搭建总结
1.使用框架spring+springmvc+mybatis+jdk7+tomcat7+maven 2.基本原理: a. WebSocket协议是一种双向通信协议,它建立在TCP之上,同http一样通 ...
- Android 多媒体视频播放一( 多媒体理解与经验分享)
前言 说到android的多媒体,一把辛酸一把泪,当初听说会多媒体的比较牛掰,公司也有需求,于是乎我也积极的加入研究android多媒体的行列,记得以前刚接触的时候,最开始还是比较头大的,主要是但是很 ...