회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
새로 추가된 문제
새로 추가된 영어 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
랭킹
게시판
그룹
블로그
강의
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
글쓰기
질문 도움말
자주묻는 질문
어느부분이 틀렸는지 잘 모르겠습니다..
17837번 - 새로운 게임 2
sdfgcsdfgc
10달 전
0
어느부분이 틀렸는지 잘 모르겠습니다.. 알려주시면 감사하겠습니다.
package 삼성기출; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.StringTokenizer; public class Solution_새로운게임2 { static int N; static int map[][]; static LinkedList<Integer>[][] waitingList; static Horse[] horses; static int dx[] = {0,1,-1,0,0}; //동 서 북 남 static int dy[] = {0,0,0,-1,1}; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; st = new StringTokenizer(br.readLine()); N = Integer.parseInt(st.nextToken())+1; int horseCount = Integer.parseInt(st.nextToken()); map = new int[N][N]; for(int r=1; r<N; r++) { st = new StringTokenizer(br.readLine()); for (int c = 1; c < N; c++) { map[r][c] = Integer.parseInt(st.nextToken()); } } waitingList = new LinkedList[N][N]; //말 번호 저장 for(int r=1; r<N; r++) { for (int c = 1; c < N; c++) { waitingList[r][c] = new LinkedList<Integer>(); } } horses = new Horse[horseCount+1]; for(int i=1; i<=horseCount; i++) { st = new StringTokenizer(br.readLine()); int row = Integer.parseInt(st.nextToken()); int col = Integer.parseInt(st.nextToken()); int d = Integer.parseInt(st.nextToken()); horses[i] = new Horse(row,col, d); waitingList[row][col].add(i); } //===================Start======================== int answer = gameStart(); System.out.println(answer); } private static int gameStart() { int turnCount = 0; while(turnCount < 1000) { turnCount++; // 턴 증가 for (int horseNumber = 1; horseNumber < horses.length; horseNumber++) { Horse horse = horses[horseNumber]; //이동 하려는 말번호 -> i int horseRow = horse.row; int horseCol = horse.col; int horseD = horse.currentD; int nextRow = horseRow + dy[horseD]; int nextCol = horseCol + dx[horseD]; LinkedList<Integer> temp = new LinkedList<Integer>(); LinkedList<Integer> list = waitingList[horseRow][horseCol]; boolean removeOk = false; for (int i = 0; i < list.size(); i++) { int num = list.get(i); if(num == horseNumber || removeOk) { temp.add(num); list.remove(i); i--; removeOk = true; } } move(horseNumber,horseRow, horseCol, nextRow, nextCol, horseD, temp); if(checkListSize4()) { return turnCount; } } } return -1; } private static boolean checkListSize4() { for (int horseNumber = 1; horseNumber < horses.length; horseNumber++) { Horse h = horses[horseNumber]; int row = h.row; int col = h.col; if(waitingList[row][col].size() >= 4) { return true; } } return false; } private static void move(int horseNumber, int horseRow, int horseCol,int nextRow, int nextCol, int horseD, LinkedList<Integer> temp) { if(nextRow >=1 && nextRow < N && nextCol >= 1 && nextCol < N) { //이동 가능 if(map[nextRow][nextCol] == 0) { //다음 발판이 하얀색 for (int i = 0; i < temp.size(); i++) { int number = temp.get(i); waitingList[nextRow][nextCol].add(number); horses[number].row = nextRow; horses[number].col = nextCol; } } else if(map[nextRow][nextCol] == 1) { //빨간색 for (int i = temp.size()-1; i >= 0; i--) { int num = temp.get(i); waitingList[nextRow][nextCol].add(num); horses[num].row = nextRow; horses[num].col = nextCol; } } else if(map[nextRow][nextCol] == 2) { //파란색 반대방향 : 1-> 2 , 2->1 , 3->4 4->3 if(horseD == 1 ) { horseD = 2; } else if( horseD == 2) { horseD = 1; } else if( horseD == 3) { horseD = 4; } else { horseD = 3; } horses[horseNumber].currentD = horseD; nextRow = horseRow + dy[horseD]; nextCol = horseCol + dx[horseD]; if( nextRow >=1 && nextRow < N && nextCol >= 1 && nextCol < N && map[nextRow][nextCol] != 2 ) { move(horseNumber,horseRow, horseCol, nextRow, nextCol,horseD, temp); } } }else { //이동 불가 -> 파란색과 같은 역활 if(horseD == 1 ) { horseD = 2; } else if( horseD == 2) { horseD = 1; } else if( horseD == 3) { horseD = 4; } else { horseD = 3; } horses[horseNumber].currentD = horseD; nextRow = horseRow + dy[horseD]; nextCol = horseCol + dx[horseD]; if(nextRow >=1 && nextRow < N && nextCol >= 1 && nextCol < N && map[nextRow][nextCol] != 2 ) { move(horseNumber,horseRow, horseCol, nextRow, nextCol,horseD, temp); } } } public static class Horse{ int row; int col; int currentD; public Horse(int r, int c, int cD) { this.row =r; this.col = c; this.currentD =cD; } } }
댓글을 작성하려면
로그인
해야 합니다.
sdfgcsdfgc 10달 전
어느부분이 틀렸는지 잘 모르겠습니다.. 알려주시면 감사하겠습니다.