g | x | w | all
Bytes Lang Time Link
084Charcoal250615T082048ZNeil
171JavaScript ES6250608T143723ZArnauld

Charcoal, 84 bytes

WS⊞υι⊞υ⭆θX≔⟦⪫υX⟧υFυ«≔⌈⁻ιXη≔⁺⌕ιη§⟦¹±¹⊕Lθ±⊕Lθ⟧⌕RLDηζ≡§ιζ#J№ι ⁰ FRLDU⊞υ⭆ι⎇⁼ληX⎇⁼μζκλ»Iⅈ

Try it online! Link is to verbose version of code. Takes input as a rectangular array of newline-terminated strings. (The quotes aren't actually necessary, I was just too lazy to remove them.) Explanation: Calculates all queues and outputs the least number of empty squares of those queues that reach a path, so too slow for many of the test cases.

WS⊞υι

Input the grid.

⊞υ⭆θX

Append a row of obstacles, to deal with cyclic indexing.

≔⟦⪫υX⟧υFυ«

Join the rows together with more obstacles and start a breadth first search for queues.

≔⌈⁻ιXη

Find the current direction of this queue. (A different obstacle character, such as *, would save me two bytes.)

≔⁺⌕ιη§⟦¹±¹⊕Lθ±⊕Lθ⟧⌕RLDηζ

Find the next step of this queue.

≡§ιζ#J№ι ⁰

If this queue has reached a path, record the number of spaces left.

 FRLDU⊞υ⭆ι⎇⁼ληX⎇⁼μζκλ

If this queue has not reached an obstacle, try all possible next steps.

»Iⅈ

Output the minimum number of spaces left. (Because this is a breadth-first search, this is always the last count recorded.)

JavaScript (ES6), 171 bytes

Expects a matrix of characters.

f=(m,X,Y=o=f,e=1,n=0)=>m.map((r,y)=>r.map((c,x)=>!~(j="RULD #".search(c))|(n+=j==4,X+1?j>4|(h=x-X)*h+(v=y-Y)*v^1:j<5)?0:j<4?e=j^(v&2)-~h:r[f(m,r[x]=x,y),x]=c))|e|n>o?o:o=n

Try it online! (only the fastest test cases)