0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paizaラーニング問題集「STEP2移動が可能かの判定・方角」を解いてみた

Posted at

▼考え方

この問題を解くために私が考えた内容1、2を以下に示します。

  1. 現在向いている方角dと1マス移動する方向mを関数caldistanceに渡して、y,x座標の移動距離を計算します。

  2. 1の戻り値と現在の座標を関数nextpositioncheckに渡して、移動が可能かどうかを判定し出力します。

▼コード

H,W,sy,sx,d,m = input().split()

S = [list(input()) for _ in range(int(H))]

# caldistance: y,x座標の移動距離を計算する関数
def caldistance(d,m):

    # dy: y座標の移動距離
    dy = 0

    # dx: x座標の移動距離
    dx = 0
    
    if d == "N":
        if m == "L":
            dx -= 1
        else:
            dx += 1
    elif d == "S":
        if m == "L":
            dx += 1
        else:
            dx -= 1
    elif d == "W":
        if m == "L":
            dy += 1
        else:
            dy -= 1
    else:
        if m == "L":
            dy -= 1
        else:
            dy += 1

    return [dy,dx]

# nextpositioncheck: 移動が可能かどうかを判定し出力する関数
def nextpositioncheck(ny,nx,dyx):
    
    if (ny+dyx[0] <= -1 or ny+dyx[0] >= int(H)) or (nx+dyx[1] <= -1  or nx+dyx[1] >= int(W)):
        print("No")
    elif S[ny][nx] == "#":
        print("No")
    else:
        print("Yes")

# 考え方1,2.
nextpositioncheck(int(sy),int(sx),caldistance(d,m))
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?