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?

AtCoder ABC377 振り返り(Python A~C)

Posted at

初の投稿です。記録のために書きます。本番ではのA~Cの3完でした。

A問題

問題概要

長さ3の文字列を入れ替えてABCを作ることができるか?

解答

文字列中にA,B,Cが1個ずつあるかを確認して、問題を解いた。

s = input()

if s.count("A") == 1 and s.count("B") == 1 and s.count("C") == 1:
    print("Yes")
else:
    print("No")

普通に文字列をソートして、ABCになっているか判定でよかったと思う。

B問題

問題概要

8×8のマスにコマをいくつか置く、コマは飛車のように縦横に動ける。
コマにとられないようなマスはいくつあるか?

解答

  1. コマの位置を取得し、移動可能マスを塗りつぶす
  2. 塗りつぶされていないマスを数えてた
# 駒があるマス目の座標を取得し、リストに格納
# その後移動可能マスを塗りつぶし、塗りつぶされていないマスを取得
grid = [list(input()) for _ in range(8)]

# print(grid)
# 駒の位置取得
koma = []
for y in range(8):
    for x in range(8):
        if grid[y][x] == "#":
            koma.append((y, x))

# 移動可能マスを塗りつぶす
for y, x in koma:
    for i in range(8):
        grid[y][i] = "#"
        grid[i][x] = "#"

ans = 0
for y in range(8):
    for x in range(8):
        if grid[y][x] == ".":
            ans += 1

print(ans)

詳しくはC問題で解説するが、コマの存在する可能性のあるマスをsetで持ち、64から引いてもよかった。

C問題

問題概要

N×Nのマス目にコマがM個ある。コマは一定の動き方をする。
コマにとられないマスはいくつあるか?

解答

  1. 移動先をリストでもっておく
  2. それぞれのコマの初期位置を記録
    1. それぞれのコマが取りうる位置をsetで記録
  3. N×N - (コマの取りうる位置) で答えを求める
n, m = map(int, input().split())

# x, yの移動方向
moves =[(2, 1), (1, 2), (-1, 2), (-2, 1),
        (-2, -1), (-1, -2), (1, -2), (2, -1)]

catch = set()
koma = []
for _ in range(m):
    a, b = map(int, input().split())
    koma.append((a, b))
    catch.add((a, b))

# 8方向に移動できるかどうか
for x, y in koma:
    for dx, dy in moves:
        if 1<= x+dx <= n and 1<= y+dy <= n:
            catch.add((x+dx, y+dy))


ans = n**2 - len(catch)
print(ans)

感想

今回は約1か月ぶりのABCだったが、Cまで19分弱で解けた。最近C問題の精進をしていたので、成果が出てよかったと思う。また、久しぶりの緑パフォーマンスを出せることができた。おかげでレートが44も上がっておいしかった。

一方、時間がたくさん余ったにもかかわらずD問題は全く分からなかった。一瞬、尺取り法を思いついたが、考察力と計算量の概算ができなくて3完に終わってしまったのは悔しい。今後は、C問題の早解きと、D問題の正解が壁になりそうなので続けて精進していきたい。

0
0
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
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?