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ラーニング問題集「いびつなひとりリバーシ」を解いてみた

Posted at

▼問題

▼考え方

  1. 前問「いびつなひとりリバーシ(1ターン)」は石を1回だけ置く問題でしたが、本問はN回石を置く問題です。前問の処理をN回繰り返せばよいです。

▼コード

############### 関数処理 ###############

# reversi_positions: 新しく石を置くことができる(y,x)を求める関数
def reversi_positions(dy, dx):

    # newpositions: 新しく石を置くことができる(y,x)を格納するリスト
    newpositions = []

    # tmp_y,tmp_x: 単位ベクトルを、Yから1つずつ拡大させるための変数
    tmp_y,tmp_x = Y+dy,X+dx
    
    while (0 <= tmp_y <= H-1) and (0 <= tmp_x <= W-1) and (S[tmp_y][tmp_x] == "."):
        newpositions.append((tmp_y,tmp_x))
        tmp_y,tmp_x = tmp_y+dy,tmp_x+dx
    
    return newpositions if (0 <= tmp_y <= H-1) and (0 <= tmp_x <= W-1) and (S[tmp_y][tmp_x] == "*") else []

# set_stones: 石を置き盤面を更新する関数
def set_stones():
    
    S[Y][X] = "*"
    
    for dy, dx in (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1):
        for y, x in reversi_positions(dy, dx):
            S[y][x] = "*"

############### メイン関数処理 ###############

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

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

# 考え方1.
for i in range(N):
    
    Y,X = map(int, input().split())
    set_stones()

for s in S:
    print(*s, 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?