|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +public class wkb129 { |
| 9 | + //遍历 |
| 10 | + public boolean canMakeSquare(char[][] grid) { |
| 11 | + for (int i = 0; i < grid.length - 1; i++) { |
| 12 | + for (int j = 0; j < grid[i].length - 1; j++) { |
| 13 | + int a = 0, b = 0; |
| 14 | + if (grid[i][j] == 'W') { |
| 15 | + a++; |
| 16 | + } else { |
| 17 | + b++; |
| 18 | + } |
| 19 | + |
| 20 | + if (grid[i + 1][j] == 'W') { |
| 21 | + a++; |
| 22 | + } else { |
| 23 | + b++; |
| 24 | + } |
| 25 | + if (grid[i][j + 1] == 'W') { |
| 26 | + a++; |
| 27 | + } else { |
| 28 | + b++; |
| 29 | + } |
| 30 | + if (grid[i + 1][j + 1] == 'W') { |
| 31 | + a++; |
| 32 | + } else { |
| 33 | + b++; |
| 34 | + } |
| 35 | + if (a >= 3 || b >= 3) return true; |
| 36 | + } |
| 37 | + } |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + //枚举 |
| 42 | + public long numberOfRightTriangles(int[][] grid) { |
| 43 | + int[][] row = new int[grid.length + 1][grid[0].length + 1]; |
| 44 | + int[][] col = new int[grid.length + 1][grid[0].length + 1]; |
| 45 | + for (int i = 0; i < grid.length; i++) { |
| 46 | + for (int j = 0; j < grid[i].length; j++) { |
| 47 | + row[i][j + 1] = row[i][j] + grid[i][j]; |
| 48 | + col[i + 1][j] = col[i][j] + grid[i][j]; |
| 49 | + } |
| 50 | + } |
| 51 | + long ans = 0; |
| 52 | + for (int i = 0; i < grid.length; i++) { |
| 53 | + for (int j = 0; j < grid[i].length; j++) { |
| 54 | + if (grid[i][j] == 0) continue; |
| 55 | + int left = row[i][j]; |
| 56 | + int right = row[i][grid[0].length] - left - 1; |
| 57 | + int up = col[i][j]; |
| 58 | + int down = col[grid.length][j] - up - 1; |
| 59 | + ans += ((long) right + left) * (up + down); |
| 60 | + } |
| 61 | + } |
| 62 | + return ans; |
| 63 | + } |
| 64 | + |
| 65 | + // 记忆化搜索 |
| 66 | + public int numberOfStableArrays(int zero, int one, int limit) { |
| 67 | + |
| 68 | + memo=new int[zero+1][one+1][2]; |
| 69 | + for (int[][] ints : memo) { |
| 70 | + for (int[] anInt : ints) { |
| 71 | + Arrays.fill(anInt,-1); |
| 72 | + } |
| 73 | + } |
| 74 | + return (int)((dfs(zero, one, 0, limit) +dfs(zero,one,1,limit))%mod); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + int mod = (int) 1e9 + 7; |
| 79 | + int [][][]memo; |
| 80 | + |
| 81 | + long dfs(int zero, int one, int cur, int limit) { |
| 82 | + if (zero == 0) { |
| 83 | + return cur==1&&one <= limit ? 1 : 0; |
| 84 | + } |
| 85 | + if (one == 0) { |
| 86 | + return cur==0&&zero <= limit ? 1 : 0; |
| 87 | + } |
| 88 | + Long key = ((long) zero << 10) + ((long) one << (10 * 2)) + ((long) cur << (10 * 3)); |
| 89 | + if (memo[zero][one][cur]!=-1) return memo[zero][one][cur]; |
| 90 | + |
| 91 | + long ans = 0; |
| 92 | + if (cur == 0) { |
| 93 | + ans += dfs(zero - 1, one, 0, limit) + dfs(zero-1,one, 1, limit) |
| 94 | + - (zero > limit ? dfs(zero - limit - 1, one, 1, limit) : 0)+mod; |
| 95 | + } else if (cur == 1) { |
| 96 | + ans += dfs(zero , one-1, 0, limit) + dfs(zero, one - 1, 1, limit) |
| 97 | + - (one > limit ? dfs(zero, one - limit - 1, 0, limit) : 0)+mod; |
| 98 | + } |
| 99 | + ans%=mod; |
| 100 | + memo[zero][one][cur]=(int)ans; |
| 101 | + return (int)ans; |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + //TLE |
| 106 | + /* int dfs(int zero, int one,int preZero,int preOne, int len, int limit) { |
| 107 | + int cur = len - zero - one; |
| 108 | + if (cur >= len) { |
| 109 | + return 1; |
| 110 | + } |
| 111 | +
|
| 112 | +
|
| 113 | + Long key = ((long) zero << 10) + ((long) one << (10 * 2)) + ((long) pre << (10 * 3)); |
| 114 | +
|
| 115 | + if (memo.containsKey(key)) return memo.get(key); |
| 116 | + int ans = 0; |
| 117 | + //当前位置放0 |
| 118 | + if (cur - preOne <= limit && zero > 0) { |
| 119 | + ans += dfs(zero - 1, one, cur, preOne, len, limit); |
| 120 | + ans %= mod; |
| 121 | + } |
| 122 | + //当前位置放1 |
| 123 | + if (cur - preZero <= limit && one > 0) { |
| 124 | + ans += dfs(zero, one - 1, preZero, cur, len, limit); |
| 125 | + ans %= mod; |
| 126 | + } |
| 127 | + memo.put(key, ans); |
| 128 | + return ans; |
| 129 | + }*/ |
| 130 | + |
| 131 | + public static void main(String[] args) { |
| 132 | + wkb129 w = new wkb129(); |
| 133 | + w.numberOfStableArrays(1, 2, 1); |
| 134 | + } |
| 135 | +} |
0 commit comments