結果

問題 No.307 最近色塗る問題多くない?
ユーザー koyumeishi
提出日時 2015-11-27 05:20:08
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 1,451 ms / 4,000 ms
コード長 1,273 bytes
コンパイル時間 1,534 ms
コンパイル使用メモリ 76,320 KB
実行使用メモリ 109,464 KB
最終ジャッジ日時 2024-11-21 21:44:11
合計ジャッジ時間 12,329 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

import time
start = time.clock()

h,w = map(int, raw_input().split())

a = []
for i in xrange(h):
	a.append( map(int, raw_input().split()) )

visit = [None] * (h*w)
q_ = [None] * (h*w)
ptr = 0

def bfs (start, color, turn) :
	global a
	global q_
	global ptr
	global visit

	if a[start // w][start % w] == color :
		return 0

	dx = [0,0,1,-1]
	dy = [1,-1,0,0]

	ret = 0
	q_[ptr] = start
	ptr += 1

	visit[start] = turn

	while ptr != 0 :
		pos = q_[ptr-1]
		ptr -= 1

		x = pos % w
		y = pos // w
		
		ret += 1;
		a[y][x] = color

		for k in xrange(4) :
			xx = x + dx[k]
			yy = y + dy[k]
			next_pos = yy * w + xx
			if xx<0 or xx >= w or yy<0 or yy>=h :
				continue
			if (visit[next_pos] == turn) or (a[yy][xx] == color) :
				continue

			visit[next_pos] = turn
			q_[ptr] = next_pos
			ptr += 1

	return ret;


filled = False;
last = -1;
q = input()

for i in xrange(q):
	r,c,x = map(int, raw_input().split())
	r = r-1;
	c = c-1;
	last = x;
	if (a[r][c] == x or filled == True) :
		continue

	cnt = bfs(r * w + c, x, i)
	if (w*h == cnt) : 
		filled = True

import sys
print >> sys.stderr, filled

if (filled == True) :
	for i in xrange(h):
		a[i] = str(last) * w

for i in xrange(h):
	print ' '.join(map(str, a[i]))



print >> sys.stderr, time.clock() - start , "ms"
0