๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[๋ฐฑ์ค€] 10709๋ฒˆ: ๊ธฐ์ƒ์บ์Šคํ„ฐ(๊ตฌํ˜„)

by ์ฃผ๋ฐœ2 2020. 4. 7.
๋ฐ˜์‘ํ˜•

https://www.acmicpc.net/problem/10709

 

10709๋ฒˆ: ๊ธฐ์ƒ์บ์Šคํ„ฐ

๋ฌธ์ œ JOI์‹œ๋Š” ๋‚จ๋ถ๋ฐฉํ–ฅ์ด H ํ‚ฌ๋กœ๋ฏธํ„ฐ, ๋™์„œ๋ฐฉํ–ฅ์ด W ํ‚ฌ๋กœ๋ฏธํ„ฐ์ธ ์ง์‚ฌ๊ฐํ˜• ๋ชจ์–‘์ด๋‹ค. JOI์‹œ๋Š” ๊ฐ€๋กœ์™€ ์„ธ๋กœ์˜ ๊ธธ์ด๊ฐ€ 1ํ‚ฌ๋กœ๋ฏธํ„ฐ์ธ H × W ๊ฐœ์˜ ์ž‘์€ ๊ตฌ์—ญ๋“ค๋กœ ๋‚˜๋‰˜์–ด ์žˆ๋‹ค. ๋ถ์ชฝ์œผ๋กœ๋ถ€ํ„ฐ i ๋ฒˆ์งธ, ์„œ์ชฝ์œผ๋กœ๋ถ€ํ„ฐ j ๋ฒˆ์งธ์— ์žˆ๋Š” ๊ตฌ์—ญ์„ (i, j) ๋กœ ํ‘œ์‹œํ•œ๋‹ค. ๊ฐ ๊ตฌ์—ญ์˜ ํ•˜๋Š˜์—๋Š” ๊ตฌ๋ฆ„์ด ์žˆ์„ ์ˆ˜๋„, ์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค. ๋ชจ๋“  ๊ตฌ๋ฆ„์€ 1๋ถ„์ด ์ง€๋‚  ๋•Œ๋งˆ๋‹ค 1ํ‚ฌ๋กœ๋ฏธํ„ฐ์”ฉ ๋™์ชฝ์œผ๋กœ ์ด๋™ํ•œ๋‹ค. ์˜ค๋Š˜์€ ๋‚ ์”จ๊ฐ€ ์ •๋ง ์ข‹๊ธฐ ๋•Œ๋ฌธ์— JOI์‹œ์˜ ์™ธ๋ถ€์—์„œ ๊ตฌ๋ฆ„์ด ์ด๋™ํ•ด ์˜ค๋Š” ๊ฒฝ์šฐ

www.acmicpc.net

์ฝ”๋“œ

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());
		StringBuilder sb = new StringBuilder();
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		int H = Integer.parseInt(st.nextToken());	// ์„ธ๋กœ
		int W = Integer.parseInt(st.nextToken());	// ๊ฐ€๋กœ
		char[][] cArr = new char[H][W];
		for(int i=0; i<H; i++) {
			String str = bf.readLine();
			for(int j=0; j<W; j++) {
				cArr[i][j] = str.charAt(j);
			}
		}
		
		for(int i=0; i<H; i++) {
			int time = 0;
			for(int j=0; j<W; j++) {
				
				/* ํ˜„์žฌ 'c'์ผ๊ฒฝ์šฐ -> time ์ดˆ๊ธฐํ™”, 0 ์ถœ๋ ฅ */
				if(cArr[i][j] == 'c') {
					time = 1;
					sb.append("0 ");
				}
				
				/* ํ˜„์žฌ '.'์ผ๊ฒฝ์šฐ -> ์ „์— 'c'๊ฐ€ ์—†์„๊ฒฝ์šฐ -> -1์ถœ๋ ฅ, 'c'๊ฐ€ ์žˆ์—ˆ์„๊ฒฝ์šฐ -> time ์ถœ๋ ฅ */
				else if(cArr[i][j] == '.') {
					if(time == 0) {
						sb.append("-1 ");
					}
					else {
						sb.append(time + " ");
						time ++;
					}
				}
			}
			sb.append("\n");
		}
		bw.write(sb.toString());
		bw.flush();
		bf.close();
		bw.close();
	}

}

ํ’€์ด

๋ฌธ์ œ๋Š” ๊ฐ„๋‹จํ•˜๋‹ค. ๊ตฌ๋ฆ„('c')๋Š” ๋™์ชฝ์œผ๋กœ 1๋ถ„์— 1km์”ฉ ์ด๋™ํ•˜๋ฉฐ, (i,j) ์œ„์น˜์— ๊ตฌ๋ฆ„์ด ์–ธ์ œ ์˜ค๋Š”์ง€ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

๊ตฌ๋ฆ„์˜ ์ด๋™์‹œ๊ฐ„์„ ๋‚˜ํƒ€๋‚ผ time์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

('c'๊ฐ€ ์˜ค์ง€์•Š๊ณ , '.'๊ฐ€ ์˜ฌ๊ฒฝ์šฐ -> time์€ 0์ด๋ฏ€๋กœ -1์„ ์ถœ๋ ฅํ•œ๋‹ค.)

( i , j )์— 'c'๊ฐ€ ์˜ฌ๊ฒฝ์šฐ -> ํ˜„์žฌ ๊ตฌ๋ฆ„์ด ๋– ์žˆ๋Š” ๊ฒฝ์šฐ์ด๋ฏ€๋กœ 0์„ ์ €์žฅํ•˜๊ณ , time์„ 1๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

( i , j )์— '.'๊ฐ€ ์˜ฌ๊ฒฝ์šฐ -> ์ด์ „์— 'c'๊ฐ€ ์—†์„๊ฒฝ์šฐ(time=0) -1 ์ €์žฅํ•˜๊ณ , ์ด์ „์— 'c'๊ฐ€ ์™”์„๊ฒฝ์šฐ(time!=0) time์„ ์ €์žฅํ•œ๋‹ค.

 

 

๋ฌธ์ œ์—์„œ ์‹œ๊ฐ„์ด ์ข€ ์˜ค๋ž˜๊ฑธ๋ ค์„œ Scanner -> BufferedReader , System.out.println -> StringBuilder ๋กœ ๋ฐ”๊ฟ”์„œ ๊ณ„์‚ฐํ•ด๋ดค๋‹ค.

240ms -> Scanner ์ž…๋ ฅ๊ณผ for๋ฌธ์—์„œ ๋ฐ”๋กœ System.out.println()์œผ๋กœ ์ถœ๋ ฅ

168ms -> BufferedReader ์ž…๋ ฅ๊ณผ for๋ฌธ์—์„œ ๋ฐ”๋กœ System.out.println()์œผ๋กœ ์ถœ๋ ฅ

84ms -> BufferedReader ์ž…๋ ฅ๊ณผ StringBuilder() ์œผ๋กœ ์ €์žฅํ•œ ํ›„ ํ•œ๋ฒˆ์— ์ถœ๋ ฅ

92, 88ms -> BufferedReader ์ž…๋ ฅ๊ณผ BufferedWriter ์ถœ๋ ฅ์œผ๋กœ ๊ณ„์‚ฐ.

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€