[BOJ]14503-ロボット掃除機
10057 ワード
質問リンク
機械掃除機
問題の説明
N*Mサイズの部屋はロボット掃除機で掃除します.部屋の各部屋は空き部屋(0)と壁(1)に分かれています.掃除機は北0洞1南2西3の一方向に位置し、北からr格子、西からc格子の(r,c)座標に位置する.を選択します.
N*Mサイズの部屋はロボット掃除機で掃除します.部屋の各部屋は空き部屋(0)と壁(1)に分かれています.掃除機は北0洞1南2西3の一方向に位置し、北からr格子、西からc格子の(r,c)座標に位置する.を選択します.
2.1左の格子が掃除されていない格子であれば->回転して1マス進む->1
2.2壁や掃除済みの部屋の場合->回転->2
2.3 4つの方向が壁になっている場合、または掃除された部屋->方向を保って、1つの部屋に後ろに倒れている場合->2
2.4壁または掃除済みの4方向の部屋の場合、後ろの方向も壁である->終了
問題を解く
与えられた通りに実現すればいい!import sys
input = sys.stdin.readline
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
N, M = map(int, input().split())
x, y, dir = map(int, input().split())
board = []
for _ in range(N):
board.append(list(map(int, input().split())))
def in_range(x, y):
if x in range(N) and y in range(M):
return True
return False
answer = 0
while 1:
#1. clean
if board[x][y] == 0:
board[x][y] = 2
answer += 1
clean_flag = 0
#2. check from left
for i in range(4):
next_dir = (dir+3)%4
nx, ny = x+dx[next_dir], y+dy[next_dir]
if in_range(nx, ny) and board[nx][ny] == 0:
#2-1. in range and has not been cleaned
dir = next_dir
x, y = nx, ny
clean_flag = 1
break
else:
#2-2. already cleaned
dir = next_dir
if clean_flag == 1:
continue
else:
behind = (dir+2)%4
bx, by = x+dx[behind], y+dy[behind]
if board[bx][by] == 1:
#2-4. if all 4 spaces are cleaned and behind is a wall
break
else:
#2-3. if all 4 spaces are cleaned and behind is not a wall
x, y = bx, by
print(answer)
^_^
Reference
この問題について([BOJ]14503-ロボット掃除機), 我々は、より多くの情報をここで見つけました
https://velog.io/@woo0_hooo/BOJ-14503-로봇청소기
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
import sys
input = sys.stdin.readline
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
N, M = map(int, input().split())
x, y, dir = map(int, input().split())
board = []
for _ in range(N):
board.append(list(map(int, input().split())))
def in_range(x, y):
if x in range(N) and y in range(M):
return True
return False
answer = 0
while 1:
#1. clean
if board[x][y] == 0:
board[x][y] = 2
answer += 1
clean_flag = 0
#2. check from left
for i in range(4):
next_dir = (dir+3)%4
nx, ny = x+dx[next_dir], y+dy[next_dir]
if in_range(nx, ny) and board[nx][ny] == 0:
#2-1. in range and has not been cleaned
dir = next_dir
x, y = nx, ny
clean_flag = 1
break
else:
#2-2. already cleaned
dir = next_dir
if clean_flag == 1:
continue
else:
behind = (dir+2)%4
bx, by = x+dx[behind], y+dy[behind]
if board[bx][by] == 1:
#2-4. if all 4 spaces are cleaned and behind is a wall
break
else:
#2-3. if all 4 spaces are cleaned and behind is not a wall
x, y = bx, by
print(answer)
Reference
この問題について([BOJ]14503-ロボット掃除機), 我々は、より多くの情報をここで見つけました https://velog.io/@woo0_hooo/BOJ-14503-로봇청소기テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol