|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + static StringTokenizer st; |
| 9 | + static int N,M; |
| 10 | + static int[][] matrix; |
| 11 | + static List<int[]> shiftList; |
| 12 | + static List<int[]> cloudList; |
| 13 | + static int[] di = {0,0,-1,-1,-1,0,1,1,1}; |
| 14 | + static int[] dj = {0,-1,-1,0,1,1,1,0,-1}; |
| 15 | + |
| 16 | + public static void main(String[] args) throws Exception { |
| 17 | + st = new StringTokenizer(br.readLine()); |
| 18 | + N = Integer.parseInt(st.nextToken()); |
| 19 | + M = Integer.parseInt(st.nextToken()); |
| 20 | + |
| 21 | + matrix = new int[N+1][N+1]; |
| 22 | + for (int i = 1; i <= N; i++) { |
| 23 | + st = new StringTokenizer(br.readLine()); |
| 24 | + for (int j = 1; j <= N; j++) { |
| 25 | + matrix[i][j] = Integer.parseInt(st.nextToken()); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + shiftList = new ArrayList<>(M); |
| 30 | + for (int i = 0; i < M; i++) { |
| 31 | + st = new StringTokenizer(br.readLine()); |
| 32 | + shiftList.add(new int[]{Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken())}); |
| 33 | + } |
| 34 | + |
| 35 | + cloudList = new ArrayList<>(); |
| 36 | + cloudList.add(new int[]{N,1}); |
| 37 | + cloudList.add(new int[]{N,2}); |
| 38 | + cloudList.add(new int[]{N-1,1}); |
| 39 | + cloudList.add(new int[]{N-1,2}); |
| 40 | + |
| 41 | + for (int[] curShift : shiftList) { |
| 42 | + int cloudSize = cloudList.size(); |
| 43 | + for (int i = 0; i < cloudSize; i++) { |
| 44 | + int[] cur = cloudList.get(i); |
| 45 | + shift(cur, curShift[0],curShift[1]); |
| 46 | + matrix[cur[0]][cur[1]]++; |
| 47 | + } |
| 48 | + |
| 49 | + for (int i = 0; i < cloudSize; i++) { |
| 50 | + int[] cur = cloudList.get(i); |
| 51 | + for (int j = 2; j <= 8; j+=2) { |
| 52 | + int ni = cur[0] + di[j]; |
| 53 | + int nj = cur[1] + dj[j]; |
| 54 | + if(ni>=1 && ni<=N && nj>=1 && nj<=N && matrix[ni][nj]>0){ |
| 55 | + matrix[cur[0]][cur[1]]++; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + boolean[][] visited = new boolean[N+1][N+1]; |
| 61 | + for (int[] cur : cloudList) { |
| 62 | + visited[cur[0]][cur[1]] = true; |
| 63 | + } |
| 64 | + cloudList.clear(); |
| 65 | + |
| 66 | + for (int i = 1; i <= N; i++) { |
| 67 | + for (int j = 1; j <= N; j++) { |
| 68 | + if(!visited[i][j] && matrix[i][j]>=2){ |
| 69 | + cloudList.add(new int[]{i,j}); |
| 70 | + matrix[i][j] -= 2; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + int ans = 0; |
| 77 | + for (int i = 1; i <= N; i++) { |
| 78 | + for (int j = 1; j <= N; j++) { |
| 79 | + ans += matrix[i][j]; |
| 80 | + } |
| 81 | + } |
| 82 | + bw.write(ans+""); |
| 83 | + bw.close(); |
| 84 | + } |
| 85 | + static void shift(int[] cur, int direction, int count){ |
| 86 | + cur[0] += di[direction]*count; |
| 87 | + cur[1] += dj[direction]*count; |
| 88 | + |
| 89 | + cur[0] = (((cur[0] - 1) % N) + N) % N + 1; |
| 90 | + cur[1] = (((cur[1] - 1) % N) + N) % N + 1; |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
0 commit comments