회원가입
로그인
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
문제집
대회
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
틀렸습니다가 나오는데 왜인지 모르겠어요(java)
14503번 - 로봇 청소기
wh414
5년 전
0
2번째 테스트케이스를 돌려봤더니 답은 56이 나오고...
이전에는 54가 나와서 방향 전환하는 부분을 바꿨더니 56이 나오는데...
뭐가 문제인지 모르겠어요 ㅠㅠ
import java.util.*; import java.io.*; public class Main { private static int[][] MAP; private static int count; private static int N; private static int M; private static boolean checkMap(int currX, int currY) { if (currX < 0 || currX >= N || currY < 0 || currY >= M) return false; if (MAP[currX][currY] == 0) return true; else return false; } private static int nextX(int currX, int dir) { switch (dir) { case 0: return currX+1; case 2: return currX-1; default: return currX; } } private static int nextY(int currY, int dir) { switch (dir) { case 1: return currY+1; case 3: return currY-1; default: return currY; } } private static int goRearX(int currX, int dir) { switch (dir) { case 0: return currX-1; case 2: return currX+1; default: return currX; } } private static int goRearY(int currY, int dir) { switch (dir) { case 1: return currY-1; case 3: return currY+1; default: return currY; } } private static int solve(int currX, int currY, int dir) { // 1. 청소 진행 if (checkMap(currX, currY)) { MAP[currX][currY] = -1; count++; int tmpdir = (4 + dir - 1) % 4; // 1-2. 다음 장소가 청소할 수 있는 곳이면 청소하러 Go if (checkMap(nextX(currX, tmpdir), nextY(currY, tmpdir))) { return solve(nextX(currX, tmpdir), nextY(currY, tmpdir), tmpdir); } } int tmpDir = dir; boolean flag = true; // flag = false 이면 네방향이 청소를 이미 했거나, 벽이라는 의미 // 2. 네 방향 탐색 for (int i = 0; i < 4; i++) { tmpDir = (4 + dir - i) % 4; if (!checkMap(nextX(currX, tmpDir), nextY(currY, tmpDir))) flag = false; else { flag = true; break; } } // 3-1. 후진을 할 수가 없을 경우 탐색 종료 if (flag == false && MAP[goRearX(currX, dir)][goRearY(currY, dir)] == 1) return count; // 3-2. 네 방향 중에 청소할 수 있는 곳이 존재하면 청소하러 Go else if(flag == true && checkMap(nextX(currX, tmpDir), nextY(currY, tmpDir))) return solve(nextX(currX, tmpDir), nextY(currY, tmpDir), tmpDir); // 3-3. 후진을 할 수 있다면 후진을 하고 다시 탐색 진행 else return solve(goRearX(currX, dir), goRearY(currY, dir), dir); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); N = sc.nextInt(); M = sc.nextInt(); MAP = new int[N][M]; int r = sc.nextInt(); // present x int c = sc.nextInt(); // present y int d = sc.nextInt(); // present direction for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { MAP[i][j] = sc.nextInt(); } } count = 0; System.out.println(solve(r, c, d)); } }
댓글을 작성하려면
로그인
해야 합니다.
wh414 5년 전