https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14BgD6AEECFAYh
모든 경우의 수를 확인해야 한다
일단 아래로 이동하고
이동한 곳의 좌우를 확인해서 좌우로 갈 수 있다면, 그 방향으로 전환
이전 스탭에서 방향 전환을 했다면,
일단 그 방향으로 이동하고
방향 또 다시 결정 (아래 or 그 방향 유지)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = 10;
int[][] box = new int[100][102];// 0패딩, 1 - 100, 101패딩
for (int t = 1; t <= T; t++) {
br.readLine(); // 입력값
int distX = 0, distY = 0;
for(int i = 0; i < 100; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for(int j = 1; j <= 100; j++) {
box[i][j] = Integer.parseInt(st.nextToken());
}
}
int minDist = Integer.MAX_VALUE;
int ansY = 1;
for(int y = 1; y <= 100; y ++) {
int[][] temp = copy(box);
if (temp[0][y] == 0) continue;
distY = y;
int cnt = 0;
int dir = 0;
distX = 0;
while(distX < 99) {
temp[distX][distY] = 0;
cnt += 1;
if (dir == 0) {
distX += 1;
if(temp[distX][distY-1] == 1) {
dir = -1;
}
else if(temp[distX][distY+1] == 1) {
dir = 1;
}
} else {
distY += dir;
if(temp[distX][distY+dir] == 0) {
dir = 0;
}
}
}
if(minDist >= cnt) {
ansY = y-1;
minDist = cnt;
}
}
System.out.println("#"+t+" "+(ansY));
}
}
static int[][] copy(int[][] zz) {
int[][] temp = new int[zz.length][zz[0].length];
for(int i = 0; i < zz.length; i++) {
for(int j = 0; j < zz[0].length; j++) {
temp[i][j] = zz[i][j];
}
}
return temp;
}
}
[BJ] 1182 : 부분수열의 합 (0) | 2024.10.05 |
---|---|
[SWEA] 4408. 자기 방으로 돌아가기 (0) | 2024.10.01 |
[SWEA] 1210.[S/W 문제해결 기본] 2일차 - Ladder1 (0) | 2024.10.01 |
[SWEA 4831] 1일차-전기버스 (0) | 2024.09.25 |
[BJ ] 1904 : 01타일 (0) | 2024.09.19 |