Algorithm/SWEA
[SWEA] S/W 문제해결 기본 (2) - Ladder 1
goakgoak
2020. 4. 16. 16:51
[1210] Ladder 1
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14ABYKADACFAYh
- 100 X 100 크기의 2차원 배열로 주어진 사다에 대해서, 지정된 도착점에 대응되는 출발점 X를 반환하는 평면상에 사다리는 연속된 '1'로 표현된다. 도착 지점은 '2'로 표현된다.
- 어느 사다리를 고르면 X 표시에 도착하게 되는지 구해보자.
Solution
- 목적지점에서 올라가면서 왼쪽 오른쪽 체크 & 방향 전환
소스코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Solution { static int T, n, m; static String[][] ladder; static int[] dx = { 0, -1, 0 }; static int[] dy = { -1, 0, 1 }; static StringTokenizer st; static String line; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (true) { T = stoi(br.readLine()); init(); int x = 99, y = 0; for (int i = 0; i < 100; i++) { ladder[i] = br.readLine().split(" "); } for(int i = 0; i<100; i++) { if(ladder[99][i].equals("2")) { y = i; } } System.out.println("#" + T + " " + solve(x, y)); if (T == 10) break; } } static int solve(int x, int y) { int cx = x; int cy = y; int dir = 1; while (cx > 0) { // up if (dir == 1) { cx += dx[dir]; cy += dy[dir]; // left if(cy > 0 && ladder[cx][cy-1].equals("1")) { dir = 0; continue; } // right else if(cy < 99 && ladder[cx][cy+1].equals("1")) { dir = 2; continue; } } else { cx += dx[dir]; cy += dy[dir]; if(cx > 0 && ladder[cx-1][cy].equals("1")) { dir = 1; continue; } } } return cy; } static void init() { ladder = new String[100][100]; } static int stoi(String s) { return Integer.parseInt(s); } } | cs |