LoginSignup
0
1

More than 3 years have passed since last update.

競技プログラミングメモ(Python)

Last updated at Posted at 2021-04-10

グリッドに関する処理

ABC75 B Minesweeper

H, W = map(int, input().split())
S = []
for i in range(H):
  S.append(list(input()))
#上、下、右、左、右上、右下、左上、左下
dy = [-1, 1, 0, 0, -1, 1, -1, 1]
dx = [0, 0, 1, -1, 1, 1, -1, -1]
for y in range(H):
  for x in range(W):
    if S[y][x] == '#':
      continue
    cnt = 0
    for i in range(8):
      ny = y + dy[i]
      nx = x + dx[i]
      if ny < 0 or ny >= H:
        continue
      if nx < 0 or nx >= W:
        continue
      if S[ny][nx] == '#':
        cnt += 1
    S[y][x] = str(cnt)
for y in range(H):
    print("".join(S[y]))

ABC96 C Grid Repainting 2

import sys

H, W = map(int, input().split())
S = []
for i in range(H):
  S.append(list(input()))
#上、下、右、左
dy = [-1, 1, 0, 0]
dx = [0, 0, 1, -1]
for y in range(H):
  for x in range(W):
    if S[y][x] == '.':
      continue
    if S[y][x] == '#':
      for i in range(4):
        ny = y + dy[i]
        nx = x + dx[i]
        if ny < 0 or ny >= H:
          continue
        if nx < 0 or nx >= W:
          continue
        if S[ny][nx] == '#':
          break
        if i == 3:
          print('No')
          sys.exit()
print('Yes')
0
1
1

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
1