[Java] SWEA 1210 Ladder1

Lee GaEun·2024년 12월 9일

[Java] 알고리즘

목록 보기
32/93

1210 Ladder1 문제 링크

문제분석

  • 사다리타기하여 당첨자를 찾는 문제
  • 당첨은 2를 뽑는 사람

  • ‘0’ : 평면
  • ‘1’ : 사다리
  • '2' : 도착지점

제약 사항

  • 한 막대에서 출발한 가로선이 다른 막대를 가로질러서 연속하여 이어지는 경우는 없음

입력 조건

  • 첫째 줄 : 테스트 케이스의 번호
  • 둘째 줄 : 사다리모양

출력 조건

  • #부호 + 테스트 케이스 번호 + " " + 당첨자 출력

#1

import java.io.*;
import java.util.Scanner;

class Solution
{
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);

        for(int test_case = 1; test_case <= 10; test_case++)
        {
            int[][] arr = new int[100][100];
            int N = sc.nextInt();
            int indexX = 0;
            int indexY = 0;

            for(int i=0; i<100; i++) {
                for(int j=0; j<100; j++) {
                    arr[i][j] = sc.nextInt();
                    if(arr[i][j]==2) {
                        indexX = i;
                        indexY = j;
                    }
                }
            }
            while (indexX!=0) {
                if(indexY!=0 && arr[indexX][indexY-1] == 1) {
                    while (indexY!=0 && arr[indexX][indexY-1]==1) {
                        indexY--;
                    }
                } else if (indexY!=99 && arr[indexX][indexY+1] == 1) {
                    while (indexY!=99 && arr[indexX][indexY+1]==1) {
                        indexY++;
                    }
                }
                indexX--;
            }
            System.out.println("#" + N + " " + indexY);
        }
    }
}

  • 성공!

#2


import java.util.Scanner;
import java.io.FileInputStream;

class Solution
{
    static int[][] arr;
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        int T;
        T=10;

        for(int test_case = 1; test_case <= T; test_case++)
        {
            int N = sc.nextInt();
            arr = new int[100][100];
            int index = 0;
            for(int i=0; i<100; i++) {
                for(int j=0; j<100; j++) {
                    arr[i][j] = sc.nextInt();
                    if(arr[i][j] == 2) index = j;
                }
            }

            for(int j=99; j>=0; j--) {
                if(index-1>=0 && arr[j][index-1]==1) {
                    index = left(j, index);
                } else if (index+1<=99 && arr[j][index+1]==1) {
                    index = right(j, index);
                }
            }

            System.out.println("#"+test_case+" "+index);
        }
    }

    static int left(int j, int index) {
        while (index-1>=0) {
            if(arr[j][index-1] == 0) return index;
            index--;
        }
        return 0;
    }
    static int right(int j, int index) {
        while (index+1<=99) {
            if(arr[j][index+1] == 0) return index;
            index++;
        }
        return 99;
    }
}
  • 성공
profile
I will give it my all (๑•̀o•́๑)ง

0개의 댓글