結果

問題 No.307 最近色塗る問題多くない?
ユーザー hama_duhama_du
提出日時 2015-11-27 23:17:30
言語 Go
(1.21.3)
結果
AC  
実行時間 236 ms / 4,000 ms
コード長 1,896 bytes
コンパイル時間 11,442 ms
コンパイル使用メモリ 215,888 KB
実行使用メモリ 7,892 KB
最終ジャッジ日時 2023-08-14 03:46:06
合計ジャッジ時間 15,451 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 6 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 19 ms
4,376 KB
testcase_08 AC 112 ms
5,560 KB
testcase_09 AC 59 ms
4,384 KB
testcase_10 AC 20 ms
4,380 KB
testcase_11 AC 48 ms
5,508 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 11 ms
7,860 KB
testcase_14 AC 10 ms
4,380 KB
testcase_15 AC 14 ms
4,380 KB
testcase_16 AC 10 ms
4,380 KB
testcase_17 AC 54 ms
4,376 KB
testcase_18 AC 100 ms
4,380 KB
testcase_19 AC 118 ms
5,512 KB
testcase_20 AC 9 ms
4,380 KB
testcase_21 AC 122 ms
5,512 KB
testcase_22 AC 123 ms
5,512 KB
testcase_23 AC 210 ms
5,508 KB
testcase_24 AC 143 ms
5,512 KB
testcase_25 AC 164 ms
7,884 KB
testcase_26 AC 229 ms
7,892 KB
testcase_27 AC 122 ms
7,876 KB
testcase_28 AC 129 ms
7,884 KB
testcase_29 AC 6 ms
5,544 KB
testcase_30 AC 236 ms
7,884 KB
testcase_31 AC 129 ms
7,884 KB
testcase_32 AC 128 ms
7,884 KB
testcase_33 AC 116 ms
5,512 KB
testcase_34 AC 129 ms
7,888 KB
testcase_35 AC 126 ms
7,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main
import (
	"fmt"
	"bufio"
	"os"
	"strconv"
	"io"
)

var dy = []int{0, -1, 0, 1}
var dx = []int{1, 0, -1, 0}

func main()  {
	h := nextInt()
	w := nextInt()

	a := make([][]int, h)
	for i := 0 ; i < h ; i++ {
		a[i] = make([]int, w)
		for j := 0 ; j < w ; j++ {
			a[i][j] = nextInt()
		}
	}

	que := make([]int, 114514)

	all := false
	q := nextInt()
	final := -1
	for i := 0 ; i < q ; i++ {
		r := nextInt()-1
		c := nextInt()-1
		x := nextInt()
		if all {
			final = x
			continue
		}
		if a[r][c] == x {
			continue
		}

		cnt := 1
		a[r][c] = x
		qh := 0
		qt := 0
		que[qh] = r
		qh++
		que[qh] = c
		qh++
		for qt < qh {
			ny := que[qt]
			qt++
			nx := que[qt]
			qt++
			for d := 0 ; d < 4 ; d++ {
				ty := ny + dy[d]
				tx := nx + dx[d]
				if ty < 0 || ty >= h || tx < 0 || tx >= w {
					continue
				}
				if a[ty][tx] == x {
					continue
				}
				a[ty][tx] = x
				que[qh] = ty
				qh++
				que[qh] = tx
				qh++
				cnt++
			}
		}
		if cnt >= h*w {
			all = true
			final = x
		}
		cnt++
	}

	for i := 0; i < h; i++ {
		for j := 0; j < w; j++ {
			if j >= 1 {
				fmt.Printf(" ")
			}
			out := a[i][j]
			if final != -1 {
				out = final
			}
			fmt.Printf("%d", out)
		}
		fmt.Println()
	}

}


// ====

var rdr = bufio.NewReader(os.Stdin)

func nextInt() int {
	i, e := strconv.Atoi(readToken(20))
	if e != nil {
		panic(e)
	}
	return i
}

func nextString(limit int) string {
	return readToken(limit)
}

func readToken(limit int) string {
	buf := make([]byte, 0, limit)

	for {
		byte, err := rdr.ReadByte()
		if err != nil {
			if err == io.EOF {
				break
			}
		}
		if byte != 10 && byte != 13 && byte != 32 {
			buf = append(buf, byte)
			break
		}
	}
	for {
		byte, err := rdr.ReadByte()
		if err != nil {
			if err == io.EOF {
				break
			}
		}
		if byte == 10 || byte == 13 || byte == 32 {
			break
		}
		buf = append(buf, byte)
	}
	return string(buf)
}
0