상세 컨텐츠

본문 제목

[SWEA] 1211. ladder2

💯ProblemSolving/문제 풀이-Java

by :부셔져버린개발자 2024. 10. 1. 13:21

본문

       문제 요약        

- 문제

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;
  }

}
728x90

관련글 더보기