잃어버린 강아지 코드 질문
해결됨
자바 코딩테스트 - it 대기업 유제
Thing이라는 클래스를 생성하여 멤버 변수로 x, y, d를 가지게 했습니다. 처음 for문을 돌면서, dog과 person이라는 객체를 생성해서 각자의 x,y,d값을 가지도록 생성자를 구현했습니다. 그 외의 로직은 강의와 거의 유사한데, 첫번째 테스트 케이스는 51이 나오지만 두번째 테스트케이스가 0으로 나오고 있습니다. 코드 첨부하겠습니다. class Thing { int x; //x 좌표 int y; //y 좌표 int d; //바라보는 위치 public Thing(int x, int y, int d) { this.x = x; this.y = y; this.d = d; } } public class Code04 { public int solution(int[][] board){ int[] dx = {-1, 0, 1, 0}; int[] dy = {0, 1, 0, -1}; Thing person = null; Thing dog = null; int time = 0; //사람과 강아지의 위치 좌표를 찾는다. for(int i=0; i<board.length; i++) { for(int j=0; j<board[i].length; j++) { if(board[i][j] == 2) { person = new Thing(i, j, 0); } if(board[i][j] == 3) { dog = new Thing(i, j, 0); } } } if(person == null || dog == null) { // 2 혹은 3이 존재하지 않는 경우 return 0; } while(time < 10000) { time++; int px = person.x + dx[person.d]; int py = person.y + dy[person.d]; int dogX = dog.x + dx[dog.d]; int dogY = dog.y + dy[dog.d]; boolean flagP = true; boolean flagD = true; if(px == dogX && py == dogY) { return time; } if(!isValidXY(px, py, board)) { person.d = (person.d + 1) % 4; flagP = false; } if(!isValidXY(dogX, dogY, board)) { dog.d = (dog.d + 1) % 4; flagD = false; } if(flagP) { person.x = px; person.y = py; } if(flagD) { dog.x = dogX; dog.y = dogY; } } return 0; } private boolean isValidXY(int x, int y, int[][] board) { return x >= 0 && y >= 0 && x < board.length && y < board.length && board[x][y] != 1; } public static void main(String[] args){ Code04 T = new Code04(); int[][] arr1 = { {0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 2, 0, 0}, {1, 0, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 3, 0, 0, 0, 1}, {0, 0, 0, 1, 0, 1, 0, 0, 0, 0}, {0, 1, 0, 1, 0, 0, 0, 0, 0, 0} }; System.out.println(T.solution(arr1)); int[][] arr2 = { {1, 0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 1, 1, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 1, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0, 1, 0, 1, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 0, 2, 1}, {0, 0, 0, 1, 0, 1, 0, 0, 0, 1}, {0, 1, 0, 1, 0, 0, 0, 0, 0, 3} }; System.out.println(T.solution(arr2)); } }
- java
- 코딩-테스트





