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ラーニング問題集「【マップの扱い 3】マップの判定・縦横斜め」を解いてみた

Posted at

▼考え方

ポイントになる考え方1.~6.を以下に示します。

1.マップの書き換えの処理は関数にします。同じコードを何度も記述することを防ぐためです。

2.マップの1番上の行から1行ずつ処理をします。

3.与えられた座標のマス(y,x)と同じ行(縦:y)の横のマスはすべて、マップの書き換えの処理を行います。

4.すべての行において、与えられた座標のマス(y,x)と同じ列(横:x)のマスは、マップの書き換えの処理を行います。

5.すべての行において、与えられた座標のマス(y,x)の左斜め上(y-a,x-a)と右斜め下(y+a,x+a)に該当するマスは、マップの書き換えの処理を行います。条件として、マップの横の範囲(0~W-1)を超えないマスに対して処理を行います。

6.すべての行において、与えられた座標のマス(y,x)の右斜め上(y-a,x+a)と左斜め下(y+a,x-a)に該当するマスは、マップの書き換えの処理を行います。条件としては、考え方5.と同様です。

▼コード


# 横方向 x,W,i
# 縦方向 y,H,j

########## 処理0(準備) インプット,リスト,関数定義など ###########

# 考え方1.
def kakikae(y,x):
    
    if S[y][x] == ".":
        S[y][x] = "#"
    else:
        S[y][x] = "."

H,W = map(int,input().split())

S = [[0 for i in range(W)] for j in range(H)]

for j in range(H):
    S[j] = list(input())

y,x = map(int,input().split())

########## 処理1 マップの書き換え、マップの出力 ##########

# 考え方2.
for j in range(H):

    # 考え方3.
    if j == y:
        for i in range(W):
            kakikae(j,i)

    else:
        # 考え方4.
        kakikae(j,x)
        
        # 考え方5.
        if x-(y-j) >= 0 and x-(y-j) <= W-1:
            kakikae(j,x-(y-j))

        # 考え方6.
        if x+(y-j) >= 0 and x+(y-j) <= W-1:
            kakikae(j,x+(y-j))

for j in range(H):
    print(*S[j],sep='')
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?