|
| 1 | +package tool; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +public class contest { |
| 9 | + |
| 10 | + int[] help(int[] temp) { |
| 11 | + int[] ans = new int[temp.length]; |
| 12 | + for (int i = 1; i < temp.length; i++) { |
| 13 | + if (temp[i] > temp[i - 1]) { |
| 14 | + ans[i] = 1; |
| 15 | + } else if (temp[i] < temp[i - 1]) { |
| 16 | + ans[i] = -1; |
| 17 | + } else { |
| 18 | + ans[i] = 0; |
| 19 | + } |
| 20 | + } |
| 21 | + return ans; |
| 22 | + } |
| 23 | + |
| 24 | + public int temperatureTrend(int[] temperatureA, int[] temperatureB) { |
| 25 | + int[] a = help(temperatureA); |
| 26 | + int[] b = help(temperatureB); |
| 27 | + int ans = 0; |
| 28 | + int count = 0; |
| 29 | + for (int i = 1; i < temperatureA.length; i++) { |
| 30 | + System.out.println(temperatureA[i] + " " + temperatureB[i]); |
| 31 | + if (a[i] == b[i]) { |
| 32 | + count++; |
| 33 | + } else { |
| 34 | + count = 0; |
| 35 | + } |
| 36 | + ans = Math.max(ans, count); |
| 37 | + } |
| 38 | + return ans; |
| 39 | + } |
| 40 | + |
| 41 | + public int transportationHub(int[][] path) { |
| 42 | + int[][] dp = new int[1000 + 1][2]; |
| 43 | + Set<Integer> set = new HashSet<>(); |
| 44 | + |
| 45 | + for (int[] ints : path) { |
| 46 | + dp[ints[0]][0]++; |
| 47 | + dp[ints[1]][1]++; |
| 48 | + set.add(ints[0]); |
| 49 | + set.add(ints[1]); |
| 50 | + } |
| 51 | + |
| 52 | + for (int i = 0; i < dp.length; i++) { |
| 53 | + if (dp[i][1] == set.size() - 1 && dp[i][0] == 0) { |
| 54 | + return i; |
| 55 | + } |
| 56 | + } |
| 57 | + return -1; |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + public int[][] ballGame(int num, String[] plate) { |
| 62 | + List<int[]> list = new ArrayList<>(); |
| 63 | + for (int i = 0; i < plate.length; i++) { |
| 64 | + for (int j = 0; j < plate[i].length(); j++) { |
| 65 | + if (plate[i].charAt(j) == 'O') { |
| 66 | + list.add(new int[]{i, j}); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + int[][] moves = new int[][]{ |
| 71 | + {0, 1}, {0, -1}, {1, 0}, {-1, 0} |
| 72 | + }; |
| 73 | + int[][][][] v = new int[plate.length][plate[0].length()][3][3]; |
| 74 | + |
| 75 | + List<int[]> temp = new ArrayList<>(); |
| 76 | + for (int i = 0; i < list.size(); i++) { |
| 77 | + int[] begin = list.get(i); |
| 78 | + for (int[] move : moves) { |
| 79 | + |
| 80 | + int[] a = check(true, begin[0], begin[1], move[0], move[1], plate, num, v); |
| 81 | + if (a != null) { |
| 82 | + if ((a[0] == 0 && a[1] == 0) || (a[0] == 0 && a[1] == plate[0].length() - 1) || (a[0] == plate.length - 1 && a[1] == 0) || (a[0] == plate.length - 1 && a[1] == plate[0].length() - 1)) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + temp.add(a); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + int[][] res = new int[temp.size()][2]; |
| 90 | + for (int i = 0; i < temp.size(); i++) { |
| 91 | + res[i] = temp.get(i); |
| 92 | + } |
| 93 | + return res; |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + int[] check(boolean init, int x, int y, int up, int left, String[] plate, int num, int[][][][] visited) { |
| 98 | + |
| 99 | + if (num < 0) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + if (visited[x][y][up + 1][left + 1]>=num) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + visited[x][y][up + 1][left + 1] = num; |
| 106 | + |
| 107 | + |
| 108 | + //逆时针 |
| 109 | + if (plate[x].charAt(y) == 'E') { |
| 110 | + if (up != 0) { |
| 111 | + if (up > 0) { |
| 112 | + left = 1; |
| 113 | + } else { |
| 114 | + left = -1; |
| 115 | + } |
| 116 | + up = 0; |
| 117 | + } else { |
| 118 | + if (left > 0) { |
| 119 | + up = -1; |
| 120 | + } else { |
| 121 | + up = 1; |
| 122 | + } |
| 123 | + left = 0; |
| 124 | + } |
| 125 | + //顺时针 |
| 126 | + } else if (plate[x].charAt(y) == 'W') { |
| 127 | + if (up != 0) { |
| 128 | + if (up > 0) { |
| 129 | + left = -1; |
| 130 | + } else { |
| 131 | + left = 1; |
| 132 | + } |
| 133 | + up = 0; |
| 134 | + } else { |
| 135 | + if (left > 0) { |
| 136 | + up = 1; |
| 137 | + } else { |
| 138 | + up = -1; |
| 139 | + } |
| 140 | + left = 0; |
| 141 | + } |
| 142 | + } else if (!init && plate[x].charAt(y) == 'O') { |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | + int nx = x + up; |
| 147 | + int ny = y + left; |
| 148 | + if (nx < 0 || nx >= plate.length || ny < 0 || ny >= plate[0].length()) { |
| 149 | + if (plate[x].charAt(y) == '.') { |
| 150 | + return new int[]{x, y}; |
| 151 | + } else { |
| 152 | + return null; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return check(false, nx, ny, up, left, plate, num - 1, visited); |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments