Official
B - Santa Claus 1 Editorial by en_translator
First, let us consider how to simulate the moves of Santa Claus.
This problem can be solved by maintaining the current coordinates of Santa Claus, and updating them according to the \(i\)-th character of \(T\) and whether the square you are moving into is passable.
Pseudocode
X,Y=Initial Santa Claus' coordinates
for i in 1..N:
if T[i]=='U' and S[X-1][Y]!='#': X-=1
if T[i]=='D' and S[X+1][Y]!='#': X+=1
if T[i]=='L' and S[X][Y-1]!='#': Y-=1
if T[i]=='R' and S[X][Y+1]!='#': Y+=1
print(X,Y)
Let us consider how many houses are passed. The simplest way to avoid counting the same house multiple times would be, once you entered a house, increment the house counter and destroy the house.
Sample code (Python)
H,W,X,Y = map(int,input().split())
S = [list(input()) for _ in range(H)]
T = input()
X-=1
Y-=1
ans=0
for c in T:
if c=='U' and S[X-1][Y]!='#': X-=1
if c=='D' and S[X+1][Y]!='#': X+=1
if c=='L' and S[X][Y-1]!='#': Y-=1
if c=='R' and S[X][Y+1]!='#': Y+=1
if S[X][Y]=='@':
ans+=1
S[X][Y]='.'
print(X+1,Y+1,ans)
posted:
last update: