Life Engineering
[BOJ 2615] 오목 (JAVA) 본문
https://www.acmicpc.net/problem/2615
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
static int[] dr= {-1,1,0,0,-1,1,-1,1};
static int[] dc= {0,0,-1,1,1,-1,-1,1};
static int winner=0;
static int ans_r=-1;
static int ans_c=-1;
public static void main(String[] args) throws FileNotFoundException {
//System.setIn(new FileInputStream("Test5.txt"));
Scanner sc=new Scanner(System.in);
boolean isAnswer=false;
int[][] board=new int[21][21]; //padding
for (int i=1; i<=19; i++) {
for (int j=1; j<=19; j++) {
board[i][j]=sc.nextInt();
}
}
for (int r=1; r<=19; r++) {
for (int c=1; c<=19; c++) {
if (board[r][c]!=0) {
for (int k=0; k<8; k++) {
if (board[r-dr[k]][c-dc[k]]==board[r][c])
continue;
int cnt=1;
int nr=r+dr[k];
int nc=c+dc[k];
while (true) {
if (board[nr][nc]!=board[r][c]) {
break;
}
cnt++;
nr+=dr[k];
nc+=dc[k];
}
if (cnt==5) {
winner=board[r][c];
saveAnswer(r,c,nr,nc,k);
isAnswer=true;
break;
}
}
}
if (isAnswer) {
break;
}
}
if (isAnswer)
break;
}
System.out.println(winner);
if (winner!=0) {
System.out.printf("%d %d\n",ans_r, ans_c);
}
}
public static void saveAnswer(int r, int c, int nr, int nc, int k) {
nr-=dr[k];
nc-=dc[k];
switch(k) {
case 0: case 2: case 5: case 6:
ans_r=nr;
ans_c=nc;
break;
case 1: case 3: case 4: case 7:
ans_r=r;
ans_c=c;
break;
}
}
}
padding을 이용해 경계체크를 할 필요가 없게 만든다.
여기서 중요한 건 탐색할 때 자기 앞에 있는 것(r-dr[i], c-dc[i])이 자기의 값과 같지 않은 경우만 탐색해야 된다는 것이다.
'Problem Solving' 카테고리의 다른 글
[SW Expert 1249] 보급로 (Java) (0) | 2022.01.26 |
---|---|
[프로그래머스] 보석 쇼핑 (Java) (0) | 2022.01.25 |
[프로그래머스] 다단계 칫솔 판매 (JAVA) (0) | 2022.01.23 |
[프로그래머스] 영어 끝말잇기 (Java) (0) | 2022.01.21 |
[SW Expert 1859] 백만 장자 프로젝트 (JAVA) (0) | 2022.01.21 |