質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

2回答

1276閲覧

AtCoder Grand Contest 033 A - Darker and Darker のPython3での計算量について

reud

総合スコア21

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

1クリップ

投稿2019/05/07 13:31

以下のようなコードを書きました。計算量がO(N)になっていると考えていて、PyPyなら十分通ると考えているのですが、ほとんどの結果がTLEになってしまいます。 計算時間を短縮し、ACする方法はあるのでしょうか。

python3

1def dig(tag,end): 2 h=tag[0] 3 w=tag[1] 4 delta=[(1,0),(-1,0),(0,1),(0,-1)] 5 next=set() 6 for d in delta: 7 if not h+d[0]<0 and not h+d[0]>=H and not w+d[1]<0 and not w+d[1]>=W and tag not in end: 8 next.add((h+d[0],w+d[1])) 9 return next 10 11H, W = map(int, input().split()) 12black_mass=set() 13 14for h in range(H): 15 line=input() 16 for w in range(W): 17 if line[w] == '#': 18 black_mass.add((h,w)) 19 20count=-1 21end=set() 22targets=black_mass 23next_que=set() 24while True: 25 count+=1 26 next_que=set() 27 for tag in targets: 28 next_que= next_que | dig(tag,end) 29 end.add(tag) 30 # print('next que',next_que) 31 # print('end:',end) 32 targets=next_que 33 if len(end) == H*W: 34 print(count) 35 exit(0) 36

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

PHPで通したコードに発想が似ているので、Python3で組み直してみたんですけど、TLEになりますね。
(Pythonは初心者です)

Python3

1# coding: utf-8 2 3import copy 4 5H, W = map(int, input().split()) 6rest = H * W 7lst = [[0 for i in range(W)] for j in range(H)] 8answer = 0 9 10black_mass = set() 11new_black_mass = set() 12 13for h in range(H): 14 line=input() 15 for w in range(W): 16 if line[w] == '#': 17 rest -= 1 18 lst[h][w] = 1 19 black_mass.add((h,w)) 20 21while(rest > 0): 22 for xy in black_mass: 23 if xy[0] > 0: 24 if lst[xy[0]-1][xy[1]] == 0: 25 lst[xy[0]-1][xy[1]] = 1 26 new_black_mass.add((xy[0]-1, xy[1])) 27 rest -= 1 28 if xy[0] < H - 1: 29 if lst[xy[0]+1][xy[1]] == 0: 30 lst[xy[0]+1][xy[1]] = 1 31 new_black_mass.add((xy[0]+1, xy[1])) 32 rest -= 1 33 if xy[1] > 0: 34 if lst[xy[0]][xy[1]-1] == 0: 35 lst[xy[0]][xy[1]-1] = 1 36 new_black_mass.add((xy[0], xy[1]-1)) 37 rest -= 1 38 if xy[1] < W - 1: 39 if lst[xy[0]][xy[1]+1] == 0: 40 lst[xy[0]][xy[1]+1] = 1 41 new_black_mass.add((xy[0], xy[1]+1)) 42 rest -= 1 43 black_mass.clear() 44 black_mass = copy.copy(new_black_mass) 45 answer += 1 46 47print(answer)

手元で実行したところ、in01.txtで40秒近くかかってました。
計算量自体はPHPのときと同じのはずなので、Pythonの処理系の中で遅いデータ処理の仕方になっているんじゃないかなー、と思います。

ACした人のPythonコードを見てみたんですが、NumPyとかSciPy使ってる人が多いですね。
それ以外の人のコードを、もう少し探してみたいと思います。

投稿2019/05/25 04:28

takepan1973

総合スコア821

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

Atcoderでは、「詳細」から他の回答者の回答(コード)を見ることができます。

以下のリンクにACの回答者リストがあるので、その中から何人かの回答を見てみてはどうでしょうか。「詳細」は回答者リストの右側に載っています。

https://atcoder.jp/contests/agc033/submissions?f.Task=agc033_a&f.Language=3510&f.Status=AC&f.User=

ご参考になれば幸いです。

投稿2019/05/08 01:16

amahara_waya

総合スコア1029

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問