https://www.acmicpc.net/problem/10709
์ฝ๋
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 ์ถ๋ ฅ์ผ๋ก ๊ณ์ฐ.
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 1188๋ฒ: ์์ํ๋ก ๊ฐ(๊ตฌํ, ์ต๋๊ณต์ฝ์) (0) | 2020.04.08 |
---|---|
[Codeforces] 1316A: Grade Allocation (0) | 2020.04.07 |
[๋ฐฑ์ค] 1021๋ฒ: ํ์ ํ๋ ํ (0) | 2020.04.07 |
[๋ฐฑ์ค] 10026๋ฒ: ์ ๋ก์์ฝ(DFS) (0) | 2020.04.06 |
[Codeforces] 1136A: Nastya is Reading a Book (0) | 2020.04.06 |
๋๊ธ