0
0

More than 3 years have passed since last update.

座標系での規則的な移動 (paizaランク B 相当) をPython3で解く

Last updated at Posted at 2021-04-14

問題の条件や定義は下記から確認できる。

まず理解できる部分を書いて、何を付け足せばいいか考えてみる

x,y,n = map(int,input().split())
direction = "E"
for i in range(n):
    if direction == "E":
        x += 1
        direction = "S"

    elif direction == "S":
        y += 1
        direction = "W"

    elif direction == "W":
        x -= 1
        direction = "N"

    elif direction == "W":
        y -= 1
        direction = "E"

print(y, x)

上記は向いている方角に1マス進み、進んだ後は一つ右を向くというコード。
これだけだと4箇所のマスをただぐるぐると回り続けるだけなので、渦巻くように外側に広がるにはどうすればいいのか考えてみる。

コードを継ぎ足す

2回方角が変わるごとに、直進する回数が+1されている。
と考え、S or N で曲がった時にカウントを増やせばいいと思った。

以下が初回クリア時のコード。

x,y,n = map(int,input().split())
direction = "E"
frag = 1
tmpo_frag = None

for i in range(n):
    if direction == "E":
        x += 1

        if frag == 1:
            direction = "S"
            tmpo_frag= None

        elif not frag == 1:

            if tmpo_frag == None:
                tmpo_frag= frag

            if tmpo_frag == 1:
                direction = "S"
                tmpo_frag = None

            if not tmpo_frag == None:
                tmpo_frag -= 1

    elif direction == "S":
        y += 1

        if frag == 1:
            direction = "W"
            tmpo_frag = None
            frag += 1

        elif not frag == 1:

            if tmpo_frag== None:
                tmpo_frag = frag

            if tmpo_frag == 1:
                direction = "W"
                tmpo_frag = None
                frag += 1

            if not tmpo_frag == None:
                tmpo_frag -= 1

    elif direction == "W":
        x -= 1

        if frag == 1:
            direction = "N"
            tmpo_frag = None

        elif not frag == 1:

            if tmpo_frag == None:
                tmpo_frag = frag

            if tmpo_frag == 1:
                direction = "N"
                tmpo_frag= None

            if not tmpo_frag == None:
                tmpo_frag -= 1

    elif direction == "N":
        y -= 1

        if frag == 1:
            direction = "E"
            tmpo_frag = None
            frag += 1

        elif not frag == 1:

            if tmpo_frag == None:
                tmpo_frag= frag

            if tmpo_frag == 1:
                direction = "E"
                tmpo_frag = None
                frag += 1

            if not tmpo_frag == None:
                tmpo_frag -= 1

print(x,y)

とにかくただ継ぎ接ぎしてできた解答なのであまりにも長い。
frag += 1でカウントを増やして回る外周が広がっているのだが、その1文を通すための増築があまりにも多い、書き直したい。

追記コード:2021/4/14/22:00

x,y,n = map(int,input().split())
direction = "E"
next_direction = {"E":"S","S":"W","W":"N","N":"E"}
frag = 1
tmpo_frag = None

for _ in range(n):
    if direction == "E":
        x += 1
    elif direction == "S":
        y += 1
    elif direction == "W":
        x -= 1
    elif direction == "N":
        y -= 1

    if frag == 1 or tmpo_frag == 1:
        tmpo_frag = None
        direction = next_direction[direction]

        if direction == "W" or direction == "E":
            frag +=1

    elif not frag == 1:

        if tmpo_frag== None:
            tmpo_frag = frag

        if not tmpo_frag == None:
            tmpo_frag -= 1

print(x,y)

初回のコードは文字数制限でDiscordに貼れないくらいになっていて、どんだけnoobなんだと思い短くする努力をしてみた。
とにかく同じ事を何回も書かない、たとえば
x +=1frag += 1など明らかにループ文の中に1つあればいいと思う部分から詰めていった。

方向転換はnext_directionでkeyとvaluを無限ループさせて回るようにさせた。

xとyの座標を移動させてる部分も

hoge = {"E":x+=1}

みたいに定義すれば短くなるのかもしれないけどできないし実現する書き方がわからない。これ以上やっても可読性が怪しくなりそうな気もするし一旦このあたりで。

0
0
1

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