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

Posted at

▼問題

▼考え方

この問題を解くために私が考えた内容1.~3.を以下に示します。

  1. 題意よりプレイヤーは盤面の'!'のマスに石を置きます。そのマスの(y,x)を取得するための処理です。

2.~4.は以前私が解いたものと同じですので、以下にまとめてあります。

▼コード

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

# 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

    # 考え方3.
    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

    # 考え方4.
    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] = "*"
    
    # 考え方2.
    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 = map(int, input().split())
S = []

# 考え方1.
for j in range(H):
    
    tmp = list(input())
    if "!" in tmp:
        Y,X = j,tmp.index("!")

    S.append(tmp)

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?