uva 10118 - Free Candies
来源:互联网 发布:js图片旋转样式 编辑:程序博客网 时间:2024/05/19 19:31
import java.io.*;import java.math.BigInteger;import java.util.*;public class Main { public static int dfs(int a,int b,int c,int d, int dp[][][][], int marked[][][][],int size, int[] basket, int[][] table){ if(marked[a][b][c][d]==1){ return dp[a][b][c][d]; } if(count_candy(basket)>=5) { marked[a][b][c][d] = 1; return 0; } if(a<size){ int candy = 0; basket[table[a][0]]^=1; if(basket[table[a][0]] ==0) candy = 1; dp[a][b][c][d] = Math.max(dp[a][b][c][d],dfs(a+1,b,c,d,dp,marked,size,basket,table)+candy); basket[table[a][0]]^=1; } if(b<size){ int candy = 0; basket[table[b][1]]^=1; if(basket[table[b][1]] ==0) candy = 1; dp[a][b][c][d] = Math.max(dp[a][b][c][d],dfs(a,b+1,c,d,dp,marked,size,basket,table)+candy); basket[table[b][1]]^=1; } if(c<size){ int candy = 0; basket[table[c][2]]^=1; if(basket[table[c][2]] ==0) candy = 1; dp[a][b][c][d] = Math.max(dp[a][b][c][d],dfs(a,b,c+1,d,dp,marked,size,basket,table)+candy); basket[table[c][2]]^=1; } if(d<size){ int candy = 0; basket[table[d][3]]^=1; if(basket[table[d][3]] ==0) candy = 1; dp[a][b][c][d] = Math.max(dp[a][b][c][d],dfs(a,b,c,d+1,dp,marked,size,basket,table)+candy); basket[table[d][3]]^=1; } marked[a][b][c][d] = 1; return dp[a][b][c][d]; } public static int count_candy(int[] basket){ int count = 0; for(int i=0;i<25;i++){ count+=basket[i]; } return count; } public static void main (String [] args) throws Exception { Scanner scan = new Scanner(System.in); while(true){ int n = scan.nextInt(); if(n==0) return; int table[][] = new int[n+1][5]; for(int i=0;i<n;i++){ for(int j=0;j<4;j++){ table[i][j] = scan.nextInt(); } } int dp[][][][] = new int[n+1][n+1][n+1][n+1]; int marked[][][][] = new int[n+1][n+1][n+1][n+1]; int basket[] = new int[25]; int result = dfs(0,0,0,0,dp,marked,n,basket,table); System.out.println(result); } }}
其实不算难题啦。。
和以前写的二维dfs走迷宫其实差不多的,无非是判重,深搜
没写过4维的,反映不过来,一些细节比如如何存candy和pair没想清楚。。。
写出来再看,其实还是很清晰的思路。四路都往下搜,中间判重就好。
- uva 10118 Free Candies
- uva 10118 - Free Candies
- UVA - 10118 Free Candies
- uva 10118 - Free Candies
- UVA 10118 - Free Candies
- UVA - 10118 Free Candies
- UVA - 10118 Free Candies
- UVa 10118 Free Candies
- uva 10118 free candies
- UVA 10118 Free Candies
- UVA 10118 Free Candies
- uva 10118 Free Candies
- UVA 10118 Free Candies
- uva 10118Free Candies
- uva 10118 Free Candies
- uva 10118 Free Candies
- Free Candies - UVa 10118 dp
- uva 10118 (Free Candies)(DP)
- html5 offlline 缓存
- NSRange各种初始化方法
- hdu_2550 百步穿杨
- 美国科技博客网:改变世界15种技术
- C++Primer笔记 第六章 语句
- uva 10118 - Free Candies
- Cocos2d-x 用C++ API File类读取文件
- Log4j使用总结
- 企业创新:先发射子弹 再发射炮弹
- JButton增加图标
- Windwos Phone MVVM Light的 EventToCommand 命令绑定实现方式
- HDU 2546 饭卡
- android多国语言---简写对应国家
- 百年中国的思想脑震荡