uva 10599 Robots(II)
来源:互联网 发布:mac ssh在哪 编辑:程序博客网 时间:2024/04/29 23:37
import java.io.*;import java.math.BigInteger;import java.util.*;class problem{ int s,m,n; void solver() throws IOException{ Scanner scan = new Scanner(System.in); int cases=1; while((m=scan.nextInt())!=-1){ n=scan.nextInt(); int arr[][] = new int[m+1][n+1]; int seq[] = new int[m*n+1]; int t1,t2; while(true){ t1=scan.nextInt(); t2=scan.nextInt(); if(t1==0&&t2==0) break; arr[t1][t2] = 1; } int size=0; for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ if(arr[i][j]==1) seq[size++]=(i-1)*n+j-1; } } if(seq[size-1]!=m*n-1) seq[size++]=m*n-1; BigInteger dp[] = new BigInteger[size]; int num[] = new int[size]; int pre[] = new int[size]; for(int i=0;i<size;i++){ num[i]=1;dp[i]=BigInteger.ONE;pre[i]=-1; for(int j=0;j<i;j++){ if(seq[i]%n>=seq[j]%n){ if(dp[i].compareTo(dp[j].add(BigInteger.ONE))<0){ dp[i]=dp[j].add(BigInteger.ONE); num[i]=num[j]; pre[i]=j; }else if(dp[i].compareTo(dp[j].add(BigInteger.ONE))==0){ num[i]+=num[j]; } } } } if(arr[m][n]!=1) dp[size-1]=dp[size-1].subtract(BigInteger.ONE); System.out.print("CASE#"+(cases++)+": "+dp[size-1]+" "+num[size-1]); backtrack(pre, size-1, seq, arr, size); System.out.println(); } } void backtrack(int pre[], int point, int seq[], int arr[][], int size){ if(pre[point]!=-1){ backtrack(pre,pre[point], seq, arr, size); } if(point!=size-1||arr[m][n]==1){ System.out.print(" "+(seq[point]+1)); } }}public class Main { public static void main (String [] args) throws Exception { problem p = new problem(); p.solver(); }}
这题又尼玛悲剧了,一看题很简单啊,2B递推就是了,dp[i][j]= max(dp[i-1][j], dp[i][j-1]) + arr[i][j] 就完事了
其实是完事了,只不过题目要求求很多东西。。。比如总路径数,以及打印一条路径
总路径数用2维数组很难求出,我写了150多行还是WA,连自己的数据都过不去。。。。
于是开始google, 然后是这个巧妙的转化,很多细节还是很巧妙的转化的, 长姿势了...
0 0
- uva 10599 Robots(II)
- UVA 10599 Robots(II)
- UVA - 10599 Robots(II)
- uva 10599 Robots(II)
- uva 10599 - Robots(II)
- UVA - 10599 Robots(II)
- uva 10599 Robots(II)
- uva 10599 - Robots(II)(LIS)
- uva 10599 Robots(II) (DP)
- Robots(II) - UVa 10599 dp
- UVA - 10599Robots(II)(LIS)
- uva 10599- Robots(II) LIS记录路径
- UVA 10599 Robots(II)(dp lis)
- Uva 10599 - Robots(II) (dp + 记录路径)
- uva 10599 - Robots(II) -动态规划
- uva 10599 - Robots(II) (dp | 记忆化搜索)
- UVA - 10599 Robots(II)(最长上升自序列)
- 10599 - Robots(II)
- c语言内存分配方式
- 这款小配件让你可以淋浴时使用手机
- 护肤品小常识科普
- 10M独享带宽,能承受多少人下载文件?
- 大文件完美上传方案(nginx upload module 集群部署方案)
- uva 10599 Robots(II)
- EASY_ZJU_PAT ADVANCED LEVEL_1031 仔细分析,删繁就简
- c语言结构体的偏移、c++
- echo,print,print_r的区别
- Git入门指南五:查看已暂存和未暂存的更新/提交更新
- DTV
- 近期复习计划
- Please ensure that adb is correctly located at 最新详细方案
- TCP/IP笔记本