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. 前問「いびつなリバーシ対戦(2人)」はプレイヤーが2人でしたが、本問はプレイヤーがn人になります。プレイヤーの番号および石を置く盤面の(y,x)を受けたら、あとは基本的に前問の処理を繰り返せばよいです。

  2. 自分以外のプレイヤーの石を挟む必要があるため、そのことを (S[tmp_y][tmp_x] != "#") and (S[tmp_y][tmp_x] != p) で表現しています。 !=p だけでは、石を置くことができない穴の空いているマス(#)も含んでしまうため、このような処理にしています。

▼コード

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

# 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

    # 考え方2.
    while (0 <= tmp_y <= H-1) and (0 <= tmp_x <= W-1) and ((S[tmp_y][tmp_x] != "#") and (S[tmp_y][tmp_x] != p)):
        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] == p) else []

# set_stones: 石を置き盤面を更新する関数
def set_stones():
    S[Y][X] = p
    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] = p

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

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

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

# 考え方1.
for _ in range(n):
    
    p,Y,X = input().split()
    
    Y,X = int(Y),int(X)
    
    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?