๋ฌธ์
https://www.acmicpc.net/problem/2615
2615๋ฒ: ์ค๋ชฉ
์ค๋ชฉ์ ๋ฐ๋ํ์ ๊ฒ์ ๋ฐ๋์๊ณผ ํฐ ๋ฐ๋์์ ๊ต๋๋ก ๋์์ ๊ฒจ๋ฃจ๋ ๊ฒ์์ด๋ค. ๋ฐ๋ํ์๋ 19๊ฐ์ ๊ฐ๋ก์ค๊ณผ 19๊ฐ์ ์ธ๋ก์ค์ด ๊ทธ๋ ค์ ธ ์๋๋ฐ ๊ฐ๋ก์ค์ ์์์๋ถํฐ ์๋๋ก 1๋ฒ, 2๋ฒ, ... ,19๋ฒ์ ๋ฒํธ
www.acmicpc.net
ํ์ด
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
// * ๋ฐฉํฅ์ ์์๋๋ก โ, โ, โ, โ
private static final int[] rowDir = {1, 0, 1, -1};
private static final int[] colDir = {0, 1, 1, 1};
private static int[][] board;
// * ํด๋น ๋ฐฉํฅ์ผ๋ก ๋ช ๊ฐ์ ๊ฐ์ ๋์ด ์กด์ฌํ๋์ง ํ์ธํ๋ ํจ์
public static int[] countLine(int dir, int r, int c) {
int prev = -1;
int[] result = new int[3];
int nr = r;
int nc = c;
int count = 0;
while(true) {
// 01. count๊ฐ 5์ผ ๋, ๋์ด ๋ฐ๋์๊ฑฐ๋ ๋๋ฌ๋ค๋ฉด ์ฐพ์๋ค! (์กฐ๊ฑด์ ๋ถ๋ฆฌํด IndexError๋ฅผ ๋ฐฉ์ง)
if(count == 5 && (nr < 0 || nr >= 19 || nc < 0 || nc >= 19)) {
return result;
}
if(count == 5 && board[nr][nc] != prev) {
return result;
}
// 02. ํด๋น ๊ฒฝ์ฐ๊ฐ ์๋๋ฐ ๋ฒ์ ๋ฐ์ผ๋ก ๋๊ฐ๋ค๋ฉด ํด๋น์ฌํญ ์๋ค.
if(nr < 0 || nr >= 19 || nc < 0 || nc >= 19) {
result[0] = -1;
result[1] = -1;
result[2] = -1;
return result;
}
// 03-1. ๋น ์นธ์ ๊ทธ๋ฅ ๊ฑด๋๋ฐ๊ณ , ๋ค์ ์ด๊ธฐํ ํด์ฃผ์.
if(board[nr][nc] == 0) {
prev = board[nr][nc];
count = 0;
}else {
// 03-2. ๊ธฐ์กด์ ๋ณด๋ ๋๊ณผ ์ข
๋ฅ๊ฐ ๋ค๋ฅด๋ค๋ฉด ์๋กญ๊ฒ count ์์ํ๋ค.
if(prev != board[nr][nc]) {
result[0] = nr+1;
result[1] = nc+1;
result[2] = board[nr][nc];
prev = board[nr][nc];
count = 1;
}
// 03-3. ๊ธฐ์กด์ ๋ณด๋ ๋๊ณผ ์ข
๋ฅ๊ฐ ๊ฐ๋ค๋ฉด count๋ฅผ ๊ณ์ํ๋ค.
else {
count ++;
}
}
// 04. ํด๋น ๋ฐฉํฅ ๋ค์ ๋์ ๋ฐ๋ผ๋ณธ๋ค.
nr += rowDir[dir];
nc += colDir[dir];
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 00. ๋ฐ๋ํ์ ์
๋ ฅ ๋ฐ๋๋ค.
board = new int[19][19];
for(int i = 0; i < 19; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for(int j = 0; j < 19; j++) {
board[i][j] = Integer.parseInt(st.nextToken());
}
}
// 01. ๊ฐ ๋ฐฉํฅ์ ๊ฒ์ฌํ๋ค.
int[] result;
// โ ๋ฐฉํฅ ์ผ๊ด๊ฒ์ฌ
for(int j = 0; j < 19; j++) {
result = countLine(0, 0, j);
if(result[0] == -1) {
continue;
} else {
System.out.println(result[2]);
System.out.println(result[0] + " " + result[1]);
return;
}
}
// โ ๋ฐฉํฅ ๊ฒ์ฌ
for(int i = 0; i < 19; i++) {
result = countLine(1, i, 0);
if(result[0] == -1) {
continue;
} else {
System.out.println(result[2]);
System.out.println(result[0] + " " + result[1]);
return;
}
}
// โ ๋ฐฉํฅ ๊ฒ์ฌ
for(int j = 0; j < 19; j++) {
result = countLine(2, 0, j);
if(result[0] == -1) {
continue;
} else {
System.out.println(result[2]);
System.out.println(result[0] + " " + result[1]);
return;
}
}
for(int i = 1; i < 19; i++) {
result = countLine(2, i, 0);
if(result[0] == -1) {
continue;
} else {
System.out.println(result[2]);
System.out.println(result[0] + " " + result[1]);
return;
}
}
// โ ๋ฐฉํฅ ๊ฒ์ฌ
for(int i = 0; i < 19; i++) {
result = countLine(3, i, 0);
if(result[0] == -1) {
continue;
} else {
System.out.println(result[2]);
System.out.println(result[0] + " " + result[1]);
return;
}
}
for(int j = 1; j < 19; j++) {
result = countLine(3, 18, j);
if(result[0] == -1) {
continue;
} else {
System.out.println(result[2]);
System.out.println(result[0] + " " + result[1]);
return;
}
}
// ํด๋น์๋ค๋ฉด 0 ์ถ๋ ฅ!
System.out.println("0");
}
}
๋ฌธ์ ๋ฅผ ์ ํ๊ณ ๋ ๊ฐ์ง ํ์ด ์ค ์ด๋ค ๊ฒ์ ์ ํํด์ผ ํ ์ง ๊ณ ๋ฏผํด๋ดค๋ค.
- ๋ชจ๋ ๊ฐ๊ฐ์ ์๋ฆฌ๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ค ๋ฐฉํฅ ๊ฒ์ฌํ๋ ๋ฐฉ๋ฒ
- ๋ฐ๋ํ ์ ์ฒด๋ฅผ ๋๊ณ , ํ, ์ด, ๋๊ฐ์ ์ ์ฐ์๋ 5๊ฐ๊ฐ ์๋์ง ๊ฒ์ฌํ๋ ๋ฐฉ๋ฒ
๊ตฌํ์ ๋น์ทํ๊ฒ ํ์ด๋ณธ ๊ฒฝํ์ด ์๋ ๋ค ๋ฐฉํฅ ํ์์ 1๋ฒ์ด ์ฌ์ธ ๊ฒ ๊ฐ์์ผ๋, 2๋ฒ ๋ฐฉ๋ฒ์ด ์ฑ๋ฅ์ ์์ด์ ํจ์ฌ ํจ์จ์ ์ด๊ณ , ๋์ ๊ฐ์์ ๊ด๊ณ์์ด ์์ ์ ์ด๋ผ๊ณ ์๊ฐํ๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ์ ๋ฐฉํฅ์ ์์ด์ ๊ฐ์ ์์๊ฐ ์ฌ๋ฌ๋ฒ ๊ฒ์ฌ ๋์์ ์ฌ๋ผ๊ฐ์ง ์๊ธฐ ๋๋ฌธ์ด๋ค. ๊ทธ๋ผ์๋ ๋ง์ฝ 1๋ฒ ๋ฐฉ๋ฒ์ผ๋ก ํ์ด๋ฅผ ํ๋ค๋ฉด, ๊ฒ์ฌํ๋ ๋ถ๋ถ์ ๊ณ์ํด์ ๋ค์ ๊ฒ์ฌํด์ผ ํ๊ธฐ ๋๋ฌธ์ ๋์ ๊ณํ๋ฒ์ ์ฌ์ฉํด์ ํ๋ฉด ์ฑ๋ฅ์ ๊ฐ์ ํ ์ ์์ ๊ฒ ๊ฐ๋ค.

'๐ > ์ฝ๋ฉํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๊ณ ๋ฆฌ์ฆ] ํ๋ฆฐํฐ ํ (๋ฐฑ์ค, 1966) (0) | 2022.01.27 |
---|---|
[์๊ณ ๋ฆฌ์ฆ] ํฌ๋ก์ํฐ์ ์ํ๋ฒณ (๋ฐฑ์ค, 2941) (0) | 2021.03.03 |
[์๊ณ ๋ฆฌ์ฆ] ๋ฑ (๋ฐฑ์ค, 3190) (0) | 2020.11.20 |
[์๊ณ ๋ฆฌ์ฆ] โพ (๋ฐฑ์ค, 17281) (0) | 2020.11.20 |
[์๊ณ ๋ฆฌ์ฆ] 2048(Easy) (๋ฐฑ์ค, 12100) (0) | 2020.11.20 |