|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +class Main { |
| 7 | + |
| 8 | + // IO field |
| 9 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + static StringTokenizer st; |
| 12 | + |
| 13 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 14 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 15 | + static long nextLong() {return Long.parseLong(st.nextToken());} |
| 16 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 17 | + |
| 18 | + // Additional field |
| 19 | + |
| 20 | + static int H, W; |
| 21 | + static int[][] A; |
| 22 | + static boolean[][] vis; |
| 23 | + static int[] dx = {1,1,1,0,0,-1,-1,-1}; |
| 24 | + static int[] dy = {1,0,-1,1,-1,1,0,-1}; |
| 25 | + |
| 26 | + public static void main(String[] args) throws Exception { |
| 27 | + |
| 28 | + ready(); |
| 29 | + solve(); |
| 30 | + |
| 31 | + bwEnd(); |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + static void ready() throws Exception{ |
| 36 | + |
| 37 | + nextLine(); |
| 38 | + H = nextInt(); |
| 39 | + W = nextInt(); |
| 40 | + A = new int[H][W]; |
| 41 | + vis = new boolean[H][W]; |
| 42 | + for(int i=0;i<H;i++) { |
| 43 | + char[] temp = br.readLine().toCharArray(); |
| 44 | + for(int j=0;j<W;j++) { |
| 45 | + if(temp[j] == '.') continue; |
| 46 | + A[i][j] = temp[j]-'0'; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + static void solve() throws Exception{ |
| 53 | + |
| 54 | + int ans = 0; |
| 55 | + |
| 56 | + Queue<int[]> Q = new LinkedList<>(); |
| 57 | + for(int i=0;i<H;i++) for(int j=0;j<W;j++) if(A[i][j] == 0) { |
| 58 | + vis[i][j] = true; |
| 59 | + Q.add(new int[] {i,j,0}); |
| 60 | + } |
| 61 | + |
| 62 | + while(!Q.isEmpty()) { |
| 63 | + int[] now = Q.poll(); |
| 64 | + int x = now[0], y = now[1], time = now[2]; |
| 65 | + ans = time; |
| 66 | + for(int k=0;k<8;k++) { |
| 67 | + int xx = x+dx[k], yy = y+dy[k]; |
| 68 | + if(xx<0 || xx>=H || yy<0 || yy>=W || vis[xx][yy]) continue; |
| 69 | + if(--A[xx][yy] == 0) { |
| 70 | + vis[xx][yy] = true; |
| 71 | + Q.add(new int[] {xx,yy,time+1}); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + bw.write(ans + "\n"); |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +``` |
0 commit comments