|
| 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 | + static char[][] arr; |
| 20 | + static boolean[][] vis; |
| 21 | + static int[] dx = {1,0,-1,0}; |
| 22 | + static int[] dy = {0,1,0,-1}; |
| 23 | + |
| 24 | + static int N, M; |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + public static void main(String[] args) throws Exception { |
| 29 | + |
| 30 | + ready(); |
| 31 | + solve(); |
| 32 | + |
| 33 | + bwEnd(); |
| 34 | + } |
| 35 | + |
| 36 | + static void ready() throws Exception{ |
| 37 | + |
| 38 | + nextLine(); |
| 39 | + N = nextInt(); |
| 40 | + M = nextInt(); |
| 41 | + arr = new char[N][M]; |
| 42 | + vis = new boolean[N][M]; |
| 43 | + for(int i=0;i<N;i++) arr[i] = br.readLine().toCharArray(); |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + static void solve() throws Exception{ |
| 48 | + |
| 49 | + Queue<int[]> Q = new LinkedList<>(); |
| 50 | + for(int i=0;i<N;i++) for(int j=0;j<M;j++) if(arr[i][j] == 'W') { |
| 51 | + Q.add(new int[] {i,j}); |
| 52 | + vis[i][j] = true; |
| 53 | + } |
| 54 | + |
| 55 | + while(!Q.isEmpty()) { |
| 56 | + int[] temp = Q.poll(); |
| 57 | + int x = temp[0], y = temp[1]; |
| 58 | + |
| 59 | + for(int i=0;i<4;i++) { |
| 60 | + int xx = x+dx[i], yy = y+dy[i]; |
| 61 | + while(arr[xx][yy] == '+') { |
| 62 | + xx += dx[i]; |
| 63 | + yy += dy[i]; |
| 64 | + } |
| 65 | + |
| 66 | + if(arr[xx][yy] == '#') { |
| 67 | + xx -= dx[i]; |
| 68 | + yy -= dy[i]; |
| 69 | + } |
| 70 | + |
| 71 | + if(vis[xx][yy]) continue; |
| 72 | + Q.offer(new int[] {xx,yy}); |
| 73 | + vis[xx][yy] = true; |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + for(int i=0;i<N;i++) { |
| 80 | + for(int j=0;j<M;j++) { |
| 81 | + if(arr[i][j] == '.' && !vis[i][j]) bw.write('P'); |
| 82 | + else bw.write(arr[i][j]); |
| 83 | + } |
| 84 | + bw.write("\n"); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | +} |
| 91 | + |
| 92 | +``` |
0 commit comments