λ¬Έμ
https://www.acmicpc.net/problem/2615
νμ΄
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 |