classTest { publicstaticvoidmain(String[] args) { Scannersc=newScanner(System.in); intM= sc.nextInt(); intN= sc.nextInt(); intK= sc.nextInt(); intL= sc.nextInt(); int[][] pic = newint[M][N]; int[][] core = newint[K][L]; int[][] ans = newint[M][N]; for (inti=0; i < M; i++) { for (intj=0; j < N; j++) { pic[i][j] = sc.nextInt(); } } for (inti=0; i < K; i++) { for (intj=0; j < L; j++) { core[i][j] = sc.nextInt(); } } for (inti=0; i < M; i++) { for (intj=0; j < N; j++) { ans[i][j] = helper(pic, core, i, j, M, N, K, L); if(ans[i][j] < 0)ans[i][j] = 0; if(ans[i][j] > 255)ans[i][j] = 255; } } for (inti=0; i < M; i++) { for (intj=0; j < N; j++) { System.out.print(ans[i][j] + " "); } System.out.println(); } }
publicstaticinthelper(int[][] pic, int[][] core, int x, int y, int M, int N, int K, int L){ intres=0; for(inti= - K / 2; i <= K / 2; i++){ for(intj= - L / 2; j <= L / 2; j++){ if(x + i < 0 || x + i > M - 1)continue; if(y + j < 0 || y + j > N - 1)continue; res += pic[x + i][y + j] * core[i + K / 2][j + L / 2]; } } return res; } }