結果
| 問題 |
No.307 最近色塗る問題多くない?
|
| コンテスト | |
| ユーザー |
hama_du
|
| 提出日時 | 2015-11-27 23:17:30 |
| 言語 | Go (1.23.4) |
| 結果 |
AC
|
| 実行時間 | 226 ms / 4,000 ms |
| コード長 | 1,896 bytes |
| コンパイル時間 | 11,123 ms |
| コンパイル使用メモリ | 213,940 KB |
| 実行使用メモリ | 5,632 KB |
| 最終ジャッジ日時 | 2024-11-21 21:50:13 |
| 合計ジャッジ時間 | 14,837 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 36 |
ソースコード
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)
}
hama_du