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?

More than 1 year has passed since last update.

ABC325(A~C問題)をPythonで解く

0
Last updated at Posted at 2023-10-25

初めに

この記事はABC325のA,B,C問題をPythonを使って解いてみた記録となります。

コンテストへのリンク

Problem A Takahashi san

Problem A

考察

苗字と名前がもらえるので苗字にsanをくっつけて出しました。

コード

S, T = map(str, input().split())
print(S, "san")

B問題 World Meeting

Problem B

考察

collectionsのdefaultdictに0時から23時(標準時)までのキーを持ち、参加可能な人数を足していくイメージでコードを書きました。

コード

営業開始時間から9時間後までの全てのキーに参加可能人数を足します。日付を跨いでしまうとカウントできないのでmodで余りを取りました。

from collections import defaultdict

N = int(input())
w = list()
for _ in range(n):
    w1, w2 = map(int, input().split())
    w.append([w1, w2])

d = defaultdict(int)

for i in range(N):
    for j in range(9):
        d[(w[i][1] + j) % 24] += w[i][0]

print(max(d.values()))


C問題 Sensors

Problem C

解法

問題文を読んだときには(木の個数を数えればいいのか…?)と思いましたが、方法が思いつかずかなり苦戦しました。入力時から木を作れればと思ったのですが、ピンと来なかったので最終的には幅優先探索で解きました。

コード

手順としては、

  1. 全てのマス目に対して順番に
  2. まだチェックしていない(木に加えられていない)#の時は
  3. 木の数を1つ増やし、キューを作ってそのマスを追加
  4. そのマスの周囲8マスを探索
  5. まだチェックしていない(木に加えられていない)#の時は
  6. キューに追加して続行 
    で書いています。
from collections import deque 

H, W = map(int, input().split())
S = []
for _ in range(H):
    s = input()
    S.append(s)

cnt = 0

Seen = [[False] * W for _ in range(H)]

for row in range(H):
    for col in range(W):
        if S[row][col] == "#" and not Seen[row][col]:
            cnt += 1
            d = deque()
            d.append((row, col))
            while d:
                i, j = d.popleft()
                for dr in [-1, 0, 1]:
                    for dc in [-1, 0, 1]:
                        if dr == dc == 0:
                            continue 
                        else:
                            if (0 <= i+ dr < H and
                                0 <= j + dc < W and
                                S[i+dr][j+dc] == "#" and
                                not Seen[i+dr][j+dc]):
                                d.append((i+dr, j+dc))
                                Seen[i+dr][j+dc] = True

print(cnt)
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?