본문 바로가기
휴지통/알고리즘 & 자료구조

백준 10709

by 신재권 2022. 2. 22.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main10709 {
	
	public static int[][] ans;
	public static int H,W;
	public static Queue<Pair> q;
	
	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		H = Integer.parseInt(st.nextToken()); // 세로 H 
		W = Integer.parseInt(st.nextToken()); // 가로 W
		
		ans = new int[H][W];
		q=  new LinkedList<>();
		
		for(int i=0;i<H; i++) {
			String s = br.readLine();
			for(int j=0; j<W; j++) {
				 char c = s.charAt(j);
				 if(c == 'c') {
					 ans[i][j] = 0;
					 q.add(new Pair(i,j));
				 }else {
					 ans[i][j] = -1;
				 }
			}
		}

		
		while(!q.isEmpty()) {
			Pair p = q.poll();
			int y = p.y;
			int x = p.x;
			
			int cnt = 1;
			for(int i=x+1; i<W; i++) {
				if(ans[y][i] == 0) continue;
				ans[y][i] = cnt++; 
				
			}
		
		}
		
		
		for(int i=0; i<H; i++) {
			for(int j=0; j<W; j++) {
				System.out.print(ans[i][j] + " ");
			}
			System.out.println();
		}
	}
	
	public static class Pair{
		int x,y;
		
		public Pair(int y, int x) {
			this.y = y;
			this.x = x;
		}
	}
	
	
	
	
	
	
	
}

'휴지통 > 알고리즘 & 자료구조' 카테고리의 다른 글

백준 2852  (0) 2022.02.24
백준 3474  (0) 2022.02.23
백준 2870  (0) 2022.02.21
백준 4659  (0) 2022.02.20
백준 2910  (0) 2022.02.19