회원가입
로그인
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
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
68% 에서 틀렸다고 나옵니다. 반례 하나 주세요.
5827번 - What's Up With Gravity
hjj5612
7달 전
0
68%에서 틀렸다고 나오네요. 반례 하나 주세요.
package org.example.p5827 import java.util.Deque fun main() { val (n, m) = readln().split(" ").map { it.toInt() } val grid : Array<CharArray> = Array(n.toInt()) { readln().toCharArray() } println("${Solution(n, m, grid).getAnswer()}") } class Solution(private val rowSize : Int, private val colSize : Int, private val grid : Array<CharArray>) { /** * 위치 * * @property * row : 행의 위치 * col : 열의 위치 */ class Location(val row : Short, val col : Short) { fun up() = Location((row - 1).toShort(), col) fun down() = Location((row + 1).toShort(), col) override fun equals(other: Any?): Boolean { if(other == null) return false if(other !is Location) return false if (row != other.row) return false if (col != other.col) return false return true } override fun hashCode(): Int { var result : Int = row.toInt() result = 31 * result + col return result } } /** * bfs(너비 우선 탐색)엣서 사용 되는 기억의 한 조각 * * @property * row : 행의 위치 * col : 열의 위치 * direction : 방향, 0 이면 아래쪽으로 중력이 작용 함, 1 이면 위쪽으로 중력이 작용함 * count : 중력을 몇 번 바꾸었는지 기록 하는 변수 */ inner class Piece(val row : Short, val col : Short, val direction : Byte, val count : Int) { fun left() : Piece = Piece(row, (col - 1).toShort(), direction, count) fun right() : Piece = Piece(row, (col + 1).toShort(), direction, count) fun reverse() : Piece = Piece(row, col, (1 - direction).toByte(), count + 1) fun isValid() : Boolean = (row in 0 until rowSize) && (col in 0 until colSize) fun location() : Location = Location(row, col) } companion object { const val DOCTOR = 'D' const val CAPTAIN = 'C' const val EMPTY = '.' const val BLOCK = '#' const val DOWN : Byte = 0 const val UP: Byte = 1 } enum class Result { NOT_MOVE, /* 중력으로 움직이 없었음 */ LOCATION_CHANGED, /* 중력으로 위치가 변경 되었음 */ FIND_DOCTOR, /* 박사를 찾았음 */ FLY_OUT /* 격자 밖으로 나깟음 */ } class ReturnValue(val result : Result, val piece : Piece) private lateinit var locationOfCaptain : Location private lateinit var locationOfDoctor : Location private val answer : Int init { for(i in 0 until rowSize) { for(j in 0 until colSize) { if(grid[i][j] == DOCTOR) locationOfDoctor = Location(row = i.toShort(), col = j.toShort()) if(grid[i][j] == CAPTAIN) locationOfCaptain = Location(row = i.toShort(), col = j.toShort()) } } answer = bfs() } private fun isValid(row : Short, col : Short) : Boolean = (row in 0 until rowSize) && (col in 0 until colSize) private fun isValid(location : Location) : Boolean = isValid(location.row, location.col) /** * 중력이 작용함 * * */ private fun gravityActs(piece : Piece) : ReturnValue { return when(piece.direction) { DOWN -> { gravityActsSubFunction(piece, Location::down, Location::up) } UP -> { gravityActsSubFunction(piece, Location::up, Location::down) } else -> throw IllegalArgumentException("Invalid direction") } } private fun gravityActsSubFunction(piece: Piece, function1 : (Location)-> Location, function2 : (Location) -> Location): ReturnValue { val location = function1(piece.location()) if (isValid(location)) { if (grid[location.row.toInt()][location.col.toInt()] == BLOCK) return ReturnValue(Result.NOT_MOVE, piece) else { var curr = location while (isValid(curr)) { if (grid[curr.row.toInt()][curr.col.toInt()] == DOCTOR) return ReturnValue( Result.FIND_DOCTOR, piece ) else if (grid[curr.row.toInt()][curr.col.toInt()] == BLOCK) { var l = Location(curr.row, curr.col) l = function2(l) return ReturnValue( Result.LOCATION_CHANGED, Piece(l.row, l.col, piece.direction, piece.count) ) } curr = function1(curr) } return ReturnValue(Result.FLY_OUT, piece) } } else return ReturnValue(Result.FLY_OUT, piece) } private fun bfs() : Int { val visited = Array(rowSize) { Array(colSize) { BooleanArray(2) { false } } } val dq = java.util.ArrayDeque<Piece>() val piece = Piece(locationOfCaptain.row, locationOfCaptain.col, DOWN, 0) val result = gravityActs(piece) when(result.result) { Result.LOCATION_CHANGED -> { dq.addFirst(result.piece) visited[result.piece.row.toInt()][result.piece.col.toInt()][result.piece.direction.toInt()] = true } Result.FIND_DOCTOR -> { return 0 } Result.NOT_MOVE -> { dq.addFirst(result.piece) visited[result.piece.row.toInt()][result.piece.col.toInt()][result.piece.direction.toInt()] = true } Result.FLY_OUT -> { val p = Piece(locationOfCaptain.row, locationOfCaptain.col, UP, 1) val r = gravityActs(p) when(r.result) { Result.LOCATION_CHANGED -> { dq.addLast(r.piece) visited[r.piece.row.toInt()][r.piece.col.toInt()][r.piece.direction.toInt()] = true } Result.FIND_DOCTOR -> { return 1 } Result.NOT_MOVE -> { dq.addLast(r.piece) visited[r.piece.row.toInt()][r.piece.col.toInt()][r.piece.direction.toInt()] = true } Result.FLY_OUT -> { } } } } while(!dq.isEmpty()) { val p = dq.pollFirst() val leftPiece = p.left() var t = bfsSubRoutine1(leftPiece, dq, visited) if(t >= 0) return t val rightPiece = p.right() t = bfsSubRoutine1(rightPiece, dq, visited) if(t >= 0) return t val reversePiece = p.reverse() t = bfsSubRoutine2(reversePiece, dq, visited) if(t >= 0) return t } return -1 } private fun bfsSubRoutine1(piece : Piece, dq : Deque<Piece>, visited : Array<Array<BooleanArray>>) : Int { if(piece.isValid() && grid[piece.row.toInt()][piece.col.toInt()] != BLOCK && !visited[piece.row.toInt()][piece.col.toInt()][piece.direction.toInt()]) { if(piece.location() == locationOfDoctor) return piece.count val result1 = gravityActs(piece) when(result1.result) { Result.LOCATION_CHANGED -> { if(!visited[result1.piece.row.toInt()][result1.piece.col.toInt()][result1.piece.direction.toInt()]) { dq.addFirst(result1.piece) visited[result1.piece.row.toInt()][result1.piece.col.toInt()][result1.piece.direction.toInt()] = true } } Result.FIND_DOCTOR -> { return result1.piece.count } Result.NOT_MOVE -> { if(!visited[result1.piece.row.toInt()][result1.piece.col.toInt()][result1.piece.direction.toInt()]) { dq.addFirst(result1.piece) visited[result1.piece.row.toInt()][result1.piece.col.toInt()][result1.piece.direction.toInt()] = true } } Result.FLY_OUT -> { val reversePiece = piece.reverse() val t = bfsSubRoutine2(reversePiece, dq, visited) if(t >= 0) return t } } } return -1 } private fun bfsSubRoutine2(piece : Piece, dq : Deque<Piece>, visited : Array<Array<BooleanArray>>) : Int { val result = gravityActs(piece) when(result.result) { Result.LOCATION_CHANGED -> { if(!visited[result.piece.row.toInt()][result.piece.col.toInt()][result.piece.direction.toInt()]) { dq.addLast(result.piece) visited[result.piece.row.toInt()][result.piece.col.toInt()][result.piece.direction.toInt()] = true } } Result.FIND_DOCTOR -> { return result.piece.count } Result.NOT_MOVE -> { if(!visited[result.piece.row.toInt()][result.piece.col.toInt()][result.piece.direction.toInt()]) { dq.addLast(result.piece) visited[result.piece.row.toInt()][result.piece.col.toInt()][result.piece.direction.toInt()] = true } } Result.FLY_OUT -> { ; // no operation } } return -1 } fun getAnswer() : Int = answer }
댓글을 작성하려면
로그인
해야 합니다.
hjj5612 7달 전
68%에서 틀렸다고 나오네요. 반례 하나 주세요.