Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
为什么我的程序总是RE,求大神解救!public class Main { private static final int MODULO = 100000000; // 第二维表示行数 private int dp[][]; private int isOk[]; // 假设所有的土地都是肥沃的情况下,那么最多有多少种情况(就是只有一个约束条件,那就是格子不相邻) private int goodForFarm[]; private int goodCount; private int sumState; public Main(int M, int N) { sumState = 1 << N; dp = new int[sumState][M]; isOk = new int[M]; goodForFarm = new int[sumState]; for (int i = 0; i < M; i++) { Arrays.fill(dp[i], 0); } Arrays.fill(isOk, 0); } public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin = new Scanner(new BufferedInputStream(System.in)); int M, N; M = cin.nextInt(); N = cin.nextInt(); Main ma = new Main(M, N); int bit; for (int i = 0; i < M; i++) { bit = 0; for (int j = 0; j < N; j++) { bit = (bit << 1); if(cin.hasNext()){ bit = bit | cin.nextInt(); } } ma.isOk[i] = bit; } System.out.println(ma.getAllPossible()); } // 假设所有的土地都是肥沃的情况下,那么最多有多少种情况 private void prepareIt() { int count = 0; // TODO Auto-generated method stub for (int i = 0; i < sumState; i++) { if ((i & (i << 1)) == 0) { goodForFarm[count] = i; count++; } } goodCount = count; } private int getAllPossible() { prepareIt(); // TODO Auto-generated method stub // 先求出第0层的每一种可能,因为不需要依赖上一层,因此独立开; for (int i = 0; i < goodCount; i++) { // 假如i为7就代表...0000111(总共N位),就是说后三个格子放牛的情况 // 接下来就是判断i这种情况是否满足要求,如果满足,那么将dp[i][0]置之1; if ((goodForFarm[i] & isOk[0]) == goodForFarm[i]) { // 该状态满足肥沃条件 dp[goodForFarm[i]][0] = 1; } } // 第一个循环,遍历每一层层开始 for (int i = 1; i < isOk.length; i++) { // 第二个循环,遍历上一层的每种状态 for (int j = 0; j < sumState; j++) { if (dp[j][i - 1] == 0) { // 这种状态根本不存在; continue; } // 遍历所有的可能的情况; for (int k = 0; k < goodCount; k++) { if ((goodForFarm[k] & isOk[i]) == goodForFarm[k]) { // 该状态满足肥沃条件 // 只要错开一位,就能判断是否相邻 // 该状态满足不相邻条件 // 看是否会与上一层相邻, if ((j & goodForFarm[k]) == 0) { dp[goodForFarm[k]][i] = (dp[goodForFarm[k]][i] + dp[j][i - 1]) % MODULO; } } } } } int countsum = 0; for (int i = 0; i < sumState; i++) { countsum = (countsum + dp[i][isOk.length - 1]) % MODULO; } return countsum % MODULO; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator