您的位置:首页 > 其它

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居然过了。。。

哎。。不会做,蛋碎
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: