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ラーニング問題集「いびつなリバーシ対戦(2人)」を解いてみた

Posted at

▼問題

▼考え方

  1. 前問「いびつなひとりリバーシ」はプレイヤーが1人でしたが、本問はプレイヤーが2人になります。基本は、プレイヤー1人ずつ、前問の処理を繰り返します。n = 1 - nでプレイヤーを交代します。

  2. 相手プレイヤーの石を挟む必要があるため、そのことを S[tmp_y][tmp_x] == "BA"[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

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

# set_stones: 石を置き盤面を更新する関数
def set_stones():

    S[Y][X] = "AB"[n]

    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] = "AB"[n]

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

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

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

# n: n=0がプレイヤーA、n=1がプレイヤーBを表す変数
n = 0

# 考え方1.
for _ in range(N*2):
    
    Y,X = map(int, input().split())
    set_stones()
    n = 1 - n

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?