Ural 1437. Gasoline Station
2014-01-16 19:23
239 查看
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { static short visited[][][] = new short[260][260][260]; static int mark[] = new int[1000]; static void add_result(int a, int b, int c){ mark[a]=1; mark[b]=1; mark[c]=1; mark[a+b] = 1; mark[b+c] = 1; mark[a+c] = 1; mark[a+b+c] = 1; } public static void DFS(int a, int b, int c, int l, int m, int n){ if(a==l&&b==m&&c==n) { add_result(a,b,c); return; } if(visited[a][b][c]==1) return; visited[a][b][c] = 1; add_result(a,b,c); if(a<l){ DFS(l, b, c, l, m, n); } if(b<m){ DFS(a,m,c,l,m,n); } if(c<n){ DFS(a,b,n,l,m,n); } int temp = 0; if(a!=0){ if(b<m){ temp = Math.min(a, m-b); DFS(a-temp, b+temp, c, l, m, n); } if(c<n){ temp = Math.min(a, n-c); DFS(a-temp, b, c+temp, l, m, n); } } if(b!=0){ if(a<l){ temp = Math.min(b, l-a); DFS(a+temp, b-temp, c, l, m, n); } if(c<n){ temp = Math.min(b, n-c); DFS(a, b-temp, c+temp, l, m, n); } } if(c!=0){ if(a<l){ temp = Math.min(c, l-a); DFS(a+temp, b, c-temp, l, m, n); } if(b<m){ temp = Math.min(c, m-b); DFS(a, b+temp, c-temp, l, m, n); } } } public static void main (String [] args) throws Exception { Scanner scan = new Scanner(System.in); int a = scan.nextInt(); int b = scan.nextInt(); int c = scan.nextInt(); DFS(0,0,0,a,b,c); int result = 0; for(int i=1;i<1000;i++){ if(mark[i]==1) result++; } System.out.println(result); //Dumper.print_1_arr(mark, 20); } }
题目略坑爹,实话说我是推了半天公式,想了半天状态转化。。。尼玛做DP都做出思维定势了。
搞不出来,于是又google了一下,原来就是DFS加判重。记忆化搜索。。。
好吧,强写。 胡写了一通DFS居然过了。。。
哎。。不会做,蛋碎
相关文章推荐
- PL/SQL Developer如何连接64位的Oracle图解
- shell 返回返回值调用
- CodeSmith 使用教程(10): CodeTemplateInfo 对象
- Python语言学习笔记(一)
- javaSE-常用类-String
- CodeSmith 使用教程(9): Progress对象
- 模块化设计进化
- CodeSmith 使用教程(8): CodeTemplate对象
- Android JNI使用方法
- CodeSmith 使用教程(7): 基本语法-转义Asp.Net标记
- 浮点数在内存中的存储方式
- CodeSmith 使用教程(6): 基本语法-声明和使用属性
- CodeSmith 使用教程(5): 基本语法-使用注释
- hdu 1242 Rescue(BFS,优先队列,基础)
- CodeSmith 使用教程(4): 基本语法-CodeTemplate 指令
- uva 796(求割边)
- CodeSmith 使用教程(3): 自动生成Yii Framework ActiveRecord
- HDU - 1231 - 最大连续子序列
- const引用测试小程序
- 我叫MT九游17区邀请ID,444331909