SWEA_D4_1210
๐ [D4_1210]] Ladder1
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Solution {
static int N = 100;
static int des_x, des_y;
// 3๋ฐฉํฅ
static int[] dx = { 1, -1, 0 };
static int[] dy = { 0, 0, -1 };
public static void main(String[] args) throws IOException, NumberFormatException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();
for (int t = 1; t <= 10; t++) {
sb.append("#").append(t).append(" ");
// ์
๋ ฅ์ 1,2,3.. ๋ค์ด๊ฐ์๋ค
int num = Integer.parseInt(br.readLine());
int[][] arr = new int[N][N];
boolean[][] isChecked = new boolean[N][N];
// ๋ฐฐ์ด ์
๋ ฅ
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine(), " ");
for (int j = 0; j < N; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
// ๋์ฐฉ์ง์ x,y ์ขํ ์ ์ฅ
if (arr[i][j] == 2) {
des_x = j;
des_y = i;
}
}
}
int nx = N;
int ny = N;
while(ny != 0) {
for(int i=0;i<3;i++) {
nx = des_x + dx[i];
ny = des_y + dy[i];
if(!(nx<N && nx >=0 && ny<N && ny>=0)) {
continue;
}
// ๋ค์ ์ขํ์ ์ซ์๊ฐ 1์ด๊ณ boolean ๋ฐฐ์ด์ด false์ด๋ฉด ( ์์ง ์๊ฐ๋ค๋ ๋ง )
if(arr[ny][nx] == 1 && !isChecked[ny][nx]) {
des_x = nx;
des_y = ny;
isChecked[des_y][des_x] = true;
// ์ด๋ฏธ ๊ฐ๊ธฐ ๋๋ฌธ์ ์ด for๋ฌธ์ break
break;
}
}
}
sb.append(nx).append("\n");
}
System.out.println(sb);
}
}
๐ค ๋์ ์๊ฐ
์ฌ๋ค๋ฆฌ ๋ฌธ์ ๋ฅผ ๋ณด์๋ง์ 3๋ฐฉํฅ ํ์๊ณผ ๋ฐ๋๋ก ์ฌ๋ผ๊ฐ์ผ๊ฒ ๋ค๋ ์๊ฐ์ ํ์๋ค.
๋ฐฐ์ด์ 1์ธ ๋ถ๋ถ๋ง ๋ฐ๋ผ๊ฐ๋ฉด ๋๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ์กฐ๊ฑด์ ๋ค์ ์์๊ฐ 1์ ๊ฐ๋ฆฌํค๊ณ isChecked๊ฐ false์ด๋ฉด ๊ฐ ์์๋ ์กฐ๊ฑด์ด ๋๊ฒ ํ์๋ค.