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

[ABC422] ABC 422(Atcoder Beginner Contest)のA~C(A,B,C)問題をPythonで解説(復習)

Posted at

[ABC422] ABC 422(Atcoder Beginner Contest)のA~C(A,B,C)問題をPythonで解説(復習)

合計回答時間:80分

A問題

自分の回答

かかった時間:5分

s = input()

i_world = int(s[0])
j_world = int(s[2])

# ワールドが1から7まではステージが増えるだけ
if 1 <= j_world <= 7:
    print(f"{i_world}-{j_world+1}")
# 8の場合は繰り上がり、ワールドにプラス1,ステージは1になる
else:
    print(f"{i_world+1}-{1}")

終了後考えた最適な回答

s = input()
# 名前は問題文のまま、world,stageの方が良さそう
world = int(s[0]) 
stage = int(s[2]) 

if stage == 8:
# strと+を使えば空白が入らない
    print(str(world+1)+'-1')
else:
    print(str(world)+'-'+str(stage+1))

B問題

自分の回答

かかった時間:45分
・時間がかかった原因はindexに対する知識不足
・番兵というものを知らず、存在確認の処理の記述に時間がかかった
・問題文通りコードを書くということを意識できていなかった

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

flag = True
for i in range(H):
    for j in range(W):
# マスが黒く塗られているかの判定
        if S [i][j] == "#":
            count = 0
# もし塗られていたら上下左右のマスを調べる
            if (j < W-1) and (S[i][j+1] == "#"):
                count += 1
            if (j-1 >= 0) and (S[i][j-1] == "#"):
                count += 1
            if (i < H-1) and (S[i+1][j] == "#"):
                count += 1
            if (i-1 >= 0) and (S[i-1][j] == "#"):
                count += 1
# 黒く塗られているマスが2,4ならyesを出力
            if (count != 2) and (count != 4):
                flag = False
                break
    if not flag:
        break
# 全部のマスがこれを満たすならyesを出力
if flag: 
    print('Yes')
else:
    print('No')

終了後考えた最適な回答

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

# 番兵を用意する、今回であれば白マスを周囲に置くことでマスの存在チェックをする必要がなくなる
field = ['.' * (W + 2)] + ['.' + input() + '.' for i in range(H)] + ['.' * (W + 2)]

flag = True
for i in range(1,H+1):
    for j in range(1,W+1):
        count = 0
        if field[i - 1][j] == '#':
            count += 1
        if field[i][j - 1] == '#':
            count += 1
        if field[i + 1][j] == '#':
            count += 1
        if field[i][j + 1] == '#':
            count += 1

        if field[i][j] == '#' and count != 2 and count != 4:
            flag = False
            break

if flag: 
    print('Yes')
else:
    print('No')

C問題

自分の回答

かかった時間:30分
・A,Cの最小の値以上の答えは出なさそうというところまでは自分でたどり着けた

T = int(input())

result_list = []
for i in range(T):
    A,B,C = map(int,input().split())
    total = A + B + C
# コンテストを開くにはA、Cのどちらか小さい方の数以下でないとならない
# 最後のtotal // 3は解説を読んだがいまだにあまり理解できていない
    result_list.append(min(A,C,total // 3))
    
for j in range(T):
    print(result_list[j])

終了後考えた最適な回答

この問題は考え方が重要。解説に
AとCは絶対必要、と書かれていたが、なぜBが絶対必要でないのか疑問だった。だが、Bが足りなくても「AAC」、「ACC」の2つのパターンが成り立つ。だから必須ではないという考え。
また(A+B+C)//3)の部分は1回のコンテスト開催に必要な文字が3つであることから全体の文字数÷3が回数の上限となる。

T = int(input())

for i in range(T):
    A, B, C = map(int, input().split())
# わざわざリストに入れなくてもそのまま出力すればいい
    print(min(A, C, (A+B+C)//3))

次に向けてやること

・gready法を使ったC問題を解く
・配列のindexに対する理解を深める
・問題文通りに回答を記載するということを習慣づける
・コメントを書いて問題を整理しながら解く

感想

B問題の回答に時間がかかってしまった。水分不足で脳が回らなくて変なミスをした気がする、もったいねー。C問題はgoogleで方法を検索したらそのあとはコードに落とし込めたのはよかった。

補足

関係するリンク(参考文献など)

今回のコンテスト

回答の正当性は保証できません。ベストエフォート方式です。

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