๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐฑ์ค€/Platinum

[BOJ] 23289 : ์˜จํ’๊ธฐ ์•ˆ๋…•!

by Jaeguk 2022. 6. 9.
๋ฌธ์ œ ๋งํฌ

23289๋ฒˆ: ์˜จํ’๊ธฐ ์•ˆ๋…•! (acmicpc.net)

 

23289๋ฒˆ: ์˜จํ’๊ธฐ ์•ˆ๋…•!

์œ ๋‚œํžˆ ์ถ”์šด ๋‚ ์”จ๊ฐ€ ์˜ˆ์ƒ๋˜๋Š” ์ด๋ฒˆ ๊ฒจ์šธ์„ ๋Œ€๋น„ํ•˜๊ธฐ ์œ„ํ•ด ๊ตฌ์‚ฌ๊ณผ๋Š” ์˜จํ’๊ธฐ๋ฅผ ์„ค์น˜ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์˜จํ’๊ธฐ์˜ ์„ฑ๋Šฅ์„ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด ๊ตฌ์‚ฌ๊ณผ๋Š” ์ง‘์„ ํฌ๊ธฐ๊ฐ€ R×C์ธ ๊ฒฉ์žํŒ์œผ๋กœ ๋‚˜ํƒ€๋ƒˆ๊ณ , 1×1 ํฌ๊ธฐ

www.acmicpc.net

์˜จํ’๊ธฐ๋ฅผ ์ด์šฉํ•ด์„œ ์กฐ์‚ฌํ•˜๋Š” ์นธ์˜ ์˜จ๋„๊ฐ€ ๋ชจ๋‘ ํŠน์ • ์˜จ๋„ ์ด์ƒ์ด ๋˜๋„๋ก ํ•˜๋Š”๋ฐ ํ…Œ์ŠคํŠธ ๊ณผ์ •์„ ๋ช‡ ๋ฒˆ ๊ฑฐ์ณ์•ผํ•˜๋Š”์ง€ ๊ณ„์‚ฐํ•˜๋Š” ๊ตฌํ˜„ ๋ฌธ์ œ.

์—ฌ๊ธฐ์„œ๋Š” ํ…Œ์ŠคํŠธ ๊ณผ์ • ํ•œ ๋ฒˆ์„ ์ดˆ์ฝœ๋ฆฟ์„ ํ•˜๋‚˜ ๋จน๋Š” ๊ฒƒ์œผ๋กœ ํ‘œํ˜„ํ–ˆ๋‹ค.

 

ํ’€์ด

R x C ํฌ๊ธฐ์˜ ๊ฒฉ์ž์— ์˜จํ’๊ธฐ๊ฐ€ ๋†“์—ฌ์žˆ๋‹ค. ์ด ์˜จํ’๊ธฐ์—์„œ ๋ฐ”๋žŒ์ด ํ•œ ๋ฒˆ ๋‚˜์˜ค๋ฉด ๋ฌธ์ œ์—์„œ ์ œ์‹œ๋œ ๊ทธ๋ฆผ๋Œ€๋กœ ๊ฒฉ์ž ์นธ์˜ ์˜จ๋„๊ฐ€ ์ƒ์Šนํ•˜๊ฒŒ ๋œ๋‹ค.

์„ฑ๋Šฅ ํ…Œ์ŠคํŠธ์˜ ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1. ์ง‘์— ์žˆ๋Š” ๋ชจ๋“  ์˜จํ’๊ธฐ์—์„œ ๋ฐ”๋žŒ์ด ํ•œ ๋ฒˆ ๋‚˜์˜ด
2. ์˜จ๋„๊ฐ€ ์กฐ์ ˆ๋จ
3. ์˜จ๋„๊ฐ€ 1 ์ด์ƒ์ธ ๊ฐ€์žฅ ๋ฐ”๊นฅ์ชฝ ์นธ์˜ ์˜จ๋„๊ฐ€ 1์”ฉ ๊ฐ์†Œ
4. ์ดˆ์ฝœ๋ฆฟ์„ ํ•˜๋‚˜ ๋จน๋Š”๋‹ค.
5. ์กฐ์‚ฌํ•˜๋Š” ๋ชจ๋“  ์นธ์˜ ์˜จ๋„๊ฐ€ K ์ด์ƒ์ด ๋˜์—ˆ๋Š”์ง€ ๊ฒ€์‚ฌ.
๋ชจ๋“  ์นธ์˜ ์˜จ๋„๊ฐ€ K์ด์ƒ์ด๋ฉด ํ…Œ์ŠคํŠธ๋ฅผ ์ค‘๋‹จํ•˜๊ณ , ์•„๋‹ˆ๋ฉด 1๋ถ€ํ„ฐ ๋‹ค์‹œ ์‹œ์ž‘ํ•œ๋‹ค.

 ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„ฑ๋Šฅํ…Œ์ŠคํŠธ๊ฐ€ ์ง„ํ–‰๋จ์— ์žˆ์–ด ์šฐ๋ฆฌ๊ฐ€ ์ €์žฅํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•  ์ •๋ณด๋“ค์€ ์–ด๋–ค ๊ฒƒ๋“ค์ด ์žˆ์„๊นŒ?

1. ๊ฒฉ์žํŒ์˜ ํ˜„์žฌ ์˜จ๋„

๊ฐ ์นธ์ด ํ˜„์žฌ ๋ช‡ ๋„์ธ์ง€ ์ €์žฅํ•˜๊ณ  ์žˆ์–ด์„œ ์˜จ๋„ ๋ณ€ํ™”์™€ ์˜จ๋„ ์กฐ์‚ฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

int ํ˜•์˜ 2์ฐจ์›๋ฐฐ์—ด Map[MAX_N][MAX_N]์„ ๋งŒ๋“ค์–ด์„œ ๊ฐ ์นธ๋งˆ๋‹ค์˜ ์˜จ๋„๋ฅผ ์ €์žฅํ–ˆ๋‹ค.

์˜จ๋„์˜ ๋ณ€ํ™”๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด Map์˜ ๊ฐ’์„ ๋ณ€๊ฒฝ์‹œ์ผœ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

2. ์˜จํ’๊ธฐ์˜ ์œ„์น˜, ๋ฐฉํ–ฅ

์˜จํ’๊ธฐ์˜ ์ขŒํ‘œ์™€ ๋ฐ”๋ผ๋ณด๊ณ ์žˆ๋Š” ๋ฐฉํ–ฅ์„ ์•Œ์•„์•ผ ๋ฐ”๋žŒ์„ ๋ณด๋‚ผ ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์˜จํ’๊ธฐ๋Š” ๋ช‡ ๊ฐœ๊ฐ€ ์„ค์น˜๋ ์ง€ ๋ชจ๋ฅด๋ฏ€๋กœ vector๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ์ €์žฅํ•œ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

vector<pair<pair<int,int>,int>> ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์˜จํ’๊ธฐ์˜ ์ขŒํ‘œ์™€, ๋ฐฉํ–ฅ ๋‘ ๊ฐ€์ง€ ์ •๋ณด๋ฅผ ์ €์žฅํ–ˆ๋‹ค.

 

 3. ๋ฒฝ์˜ ์œ„์น˜

๋ฒฝ์€ ์ธ์ ‘ํ•œ ๋‘ ์นธ ์‚ฌ์ด์— ์„ค์น˜๋  ์ˆ˜ ์žˆ๋‹ค. 2์ฐจ์› ๋ฐฐ์—ด๋กœ ๋ฒฝ์˜ ์œ„์น˜๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ์—๋Š” ํ•œ๊ณ„๊ฐ€ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

๋งŒ์•ฝ ( y, x )์™€ (y+1, x) ์‚ฌ์ด์— ๋ฒฝ์ด ์žˆ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ, ๋ฒฝ์œ„ ์œ„์น˜๋ฅผ 2์ฐจ์› ๋ฐฐ์—ด๋กœ Wall[y][x] ๋ผ๊ณ  ํ‘œ์‹œํ•œ๋‹ค๋ฉด (y,x) ์นธ์˜ ์ƒํ•˜์ขŒ์šฐ ๋ฐฉํ–ฅ ์ค‘ ์–ด๋–ค ์œ„์น˜์— ๋ฒฝ์ด ์„ค์น˜๋˜์–ด์žˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค.

๊ทธ๋ž˜์„œ ๋‚˜๋Š” ๋‘ ์นธ ์‚ฌ์ด์— ๋ฒฝ์ด ๋†“์—ฌ์žˆ์Œ์„ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด 4์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค์—ˆ๋‹ค.

Wall[y][x][y+1][x] ๊ฐ’์ด true๋ฉด ๋‘ ์ขŒํ‘œ ์‚ฌ์ด์— ๋ฒฝ์ด ๋†“์—ฌ์žˆ๋‹ค๋Š” ๊ฑธ๋กœ ํŒ๋‹จํ–ˆ๋‹ค.

์ด๋•Œ Wall[y][x][y+1][x] ์™€ Wall[y+1][x][y][x] ์˜ ๊ฐ’์€ ํ•ญ์ƒ ๊ฐ™์•„์•ผํ•œ๋‹ค.

 

4. ์กฐ์‚ฌํ•ด์•ผํ•  ์นธ์˜ ์ขŒํ‘œ

์ขŒํ‘œ์ค‘ ์ž…๋ ฅ๊ฐ’์œผ๋กœ 5๋ฅผ ๋ฐ›๋Š” ์œ„์น˜๋งŒ ์˜จ๋„ ์กฐ์‚ฌ๋ฅผ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

์ž…๋ ฅ๊ฐ’์ด 5๋ผ๋ฉด ํ•ด๋‹น ์นธ์„ inspect๋ผ๋Š” pair<int,int> ๋ฒกํ„ฐ์— ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•ด์„œ ์˜จ๋„๋ฅผ ์กฐ์‚ฌํ•  ๋•Œ ์‚ฌ์šฉํ–ˆ๋‹ค.

 

๊ธฐ๋ณธ์ ์œผ๋กœ ํ•„์š”ํ•œ ์ •๋ณด๋“ค์€ ์ด ์ •๋„๊ฐ€ ์žˆ๋‹ค.

๋‚˜๋จธ์ง€๋Š” ํ•จ์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ์งœ๋Š๋ƒ์— ๋”ฐ๋ผ ์•Œ๊ณ ์žˆ์–ด์•ผ ํ•  ์ •๋ณด๋“ค์ด ๋‹ค๋ฅผ ๊ฒƒ์ด๋‹ค.

 

  • ์˜จํ’๊ธฐ ๋ฐ”๋žŒ์˜ ์ง„ํ–‰

์˜จํ’๊ธฐ ๋ฐ”๋žŒ์€ ๋‚˜์˜ค๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ๋ถ€์ฑ„๊ผด ๋ชจ์–‘์œผ๋กœ ํผ์ ธ๋‚˜๊ฐ€๋ฉฐ ์˜จํ’๊ธฐ์—์„œ ๋ฉ€์–ด์งˆ ์ˆ˜๋ก ์ฆ๊ฐ€ํ•˜๋Š” ์˜จ๋„๊ฐ€ 5->4->3->2->1 ์ˆœ์„œ๋Œ€๋กœ ์ค„์–ด๋“ ๋‹ค.

๋ฐ”๋žŒ์€ ์ง„ํ–‰ํ•˜๋Š” ๋ฐฉํ–ฅ์„ ๊ธฐ์ค€์œผ๋กœ ์ง„ํ–‰๋ฐฉํ–ฅ ์™ผ์ชฝ ์œ„ ๋Œ€๊ฐ์„ , ์œ„, ์˜ค๋ฅธ์ชฝ ์œ„ ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ ์ด๋ ‡๊ฒŒ 3๋ฐฉํ–ฅ์œผ๋กœ ํผ์ ธ๋‚˜๊ฐ„๋‹ค.

์ด๋•Œ ์ง„ํ–‰ํ•˜๋ ค๋Š” ๋ฐฉํ–ฅ์— ๋ฒฝ์ด ์„ค์น˜๋˜์–ด์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๋ฐฉํ–ฅ์œผ๋กœ๋Š” ๋ฐ”๋žŒ์€ ๋” ์ด์ƒ ์ง„ํ–‰ํ•˜์ง€ ๋ชปํ•œ๋‹ค.

๋‚˜๋Š” BFS๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ”๋žŒ์˜ ์ง„ํ–‰์„ ๊ตฌํ˜„ํ–ˆ๋‹ค.

์ฒ˜์Œ ๋ฐ”๋žŒ์ด ๋‚˜์˜ค๋Š” ์นธ์€ ๋ฒฝ์œผ๋กœ ๋ง‰ํ˜€์žˆ์ง€ ์•Š๋‹ค๋Š” ์กฐ๊ฑด์ด ๋ช…์‹œ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ํ์— ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์˜จํ’๊ธฐ ๋ฐ”๋กœ ์•ž ์นธ์˜ ์ขŒํ‘œ์™€ ์ฆ๊ฐ€ํ•˜๋Š” ์˜จ๋„์ธ 5๋ฅผ ๋„ฃ๋Š”๋‹ค. ์ดํ›„ ์•ž์„œ ๋งํ•œ 3๋ฐฉํ–ฅ ์ค‘ ์ง„ํ–‰์ด ๊ฐ€๋Šฅํ•œ ๋ฐฉํ–ฅ์˜ ์ขŒํ‘œ๋ฅผ ํ์— ์ถ”๊ฐ€์ ์œผ๋กœ ๋„ฃ์–ด์ค€๋‹ค. ์ด๋•Œ ์ฆ๊ฐ€ํ•˜๋Š” ์˜จ๋„ ๊ฐ’์€ 1์”ฉ ์ค„์ด๋ฉด์„œ ๋„ฃ๋Š”๋‹ค. ์ฆ๊ฐ€ํ•˜๋Š” ์˜จ๋„๊ฐ€ 1์ธ ๋ฐ”๋žŒ์€ ๋” ์ด์ƒ ์ง„ํ–‰ํ•ด๋„ ์˜จ๋„๋ณ€ํ™”์— ์˜๋ฏธ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๊ทธ ๋ฐ”๋žŒ์€ ์ง„ํ–‰์„ ์ข…๋ฃŒํ•œ๋‹ค.

์ง„ํ–‰ํ•˜๋Š” ๋ฐฉํ–ฅ 3๋ฐฉํ–ฅ์€ ํŠน๋ณ„ํ•œ ๊ทœ์น™์ด ์—†์–ด ๋…ธ๊ฐ€๋‹ค๋กœ ๋ฐฉํ–ฅ์„ ์ •ํ•ด์ฃผ์—ˆ๋‹ค.

  • ์˜จ๋„ ์กฐ์ ˆ

๋ชจ๋“  ์นธ์— ๋Œ€ํ•ด์„œ ์ธ์ ‘ํ•œ 4์นธ์— ๋Œ€ํ•ด  ํ•ด๋‹น์นธ๊ณผ ์ธ์ ‘ํ•œ ์นธ์˜ (์˜จ๋„ ์ฐจ์ด / 4)๋ฅผ ๋‚ด๋ฆผํ•œ ๋งŒํผ ์˜จ๋„๊ฐ€ ์˜ค๊ณ ๊ฐ„๋‹ค. ์ด๋•Œ ์˜จ๋„๊ฐ€ ๋†’์€ ์นธ์—์„œ ๋‚ฎ์€ ์นธ์œผ๋กœ ์ด๋™ํ•œ๋‹ค. ์˜จ๋„์ฐจ์ด๊ฐ€ 4์ด์ƒ ๋‚˜์ง€์•Š์œผ๋ฉด ํ•ด๋‹น์‚ฌํ•ญ์ด ์—†๋‹ค.

์ด๋•Œ ์˜จ๋„ ์กฐ์ ˆ์€ ๋ชจ๋“  ์นธ์— ๋Œ€ํ•ด์„œ ๋™์‹œ์— ์ด๋ฃจ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์„ ๋ฐ”๋กœ ์กฐ์ •ํ•˜์ง€ ์•Š๊ณ  ์กฐ์ •๋  ๊ฐ’์„ temp๋ผ๋Š” ๋ฐฐ์—ด์— ๋ฏธ๋ฆฌ ๋‹ด์•„๋‘๊ณ  ๋งˆ์ง€๋ง‰์— ํ•œ ๋ฒˆ์— ์˜จ๋„๊ฐ’์„ ์กฐ์ ˆํ–ˆ๋‹ค.

์ž„์˜์˜ ๋‘ ์นธ์— ๋Œ€ํ•ด ์˜จ๋„ ์กฐ์ ˆ์€ 1๋ฒˆ๋งŒ ์ผ์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๋ณต์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด visited๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ํ•ด๋‹น ๋‘ ์นธ ์‚ฌ์ด์— ์ด๋ฏธ ์˜จ๋„ ๊ต๋ฅ˜๊ฐ€ ์ผ์–ด๋‚ฌ๋Š”์ง€๋ฅผ ๊ธฐ๋กํ–ˆ๋‹ค.

  • ์˜จ๋„ ๊ฐ์†Œ

์˜จ๋„๊ฐ€ 1 ์ด์ƒ์ธ ๊ฐ€์žฅ ๋ฐ”๊นฅ์ชฝ ์นธ์˜ ์˜จ๋„๊ฐ€ 1์”ฉ ๊ฐ์†Œํ•œ๋‹ค๋ผ๊ณ  ๋˜์–ด์žˆ๋‹ค. ์ฆ‰ ํ…Œ๋‘๋ฆฌ์— ๋Œ€ํ•ด์„œ ์˜จ๋„๊ฐ€ 1์ด์ƒ์ด๋ฉด ์˜จ๋„๊ฐ€ 1 ๊ฐ์†Œํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.

๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ํ…Œ๋‘๋ฆฌ๋งŒ ๋Œ๋ฉด์„œ ์˜จ๋„๊ฐ€ 1 ์ด์ƒ์ด๋ฉด ์˜จ๋„๋ฅผ 1 ๋‚ฎ์ถฐ์ฃผ๋ฉด ๋œ๋‹ค.

pair<int, int> direct[4] = { {0,1},{1,0},{0,-1},{-1,0} };
	pair<int, int> now = { 1,1 };
	for (int i = 0; i < 4; i++) {
		if (i % 2 == 0) {
			for (int j = 1; j < C; j++) {
				now = { now.first + direct[i].first, now.second + direct[i].second };
				if (Map[now.first][now.second])
					Map[now.first][now.second]--;
			}
		}
		else {
			for (int j = 1; j < R; j++) {
				now = { now.first + direct[i].first, now.second + direct[i].second };
				if (Map[now.first][now.second])
					Map[now.first][now.second]--;
			}
		}
	}

๋‚˜๋Š” ์ด๋Ÿฐ์‹์œผ๋กœ ๋ฐฉํ–ฅ์„ ์˜ค๋ฅธ์ชฝ, ์•„๋ž˜, ์™ผ์ชฝ, ์œ„ ๋ฐฉํ–ฅ์„ ์„ค์ •ํ•ด๋‘๊ณ  now ์ขŒํ‘œ๋ฅผ ํ•œ ์นธ์”ฉ ์ด๋™ํ•˜๋ฉด์„œ ํ…Œ๋‘๋ฆฌ๋ฅผ ๋Œ์•˜๋‹ค.

  • ์˜จ๋„ ์กฐ์‚ฌ

์˜จ๋„ ์กฐ์‚ฌ๋Š” ๋ง ๊ทธ๋Œ€๋กœ ์ดˆ๊ธฐ์— ๋ฐ›์•„๋‘” inspect ๋ฐฐ์—ด์„ ์ด์šฉํ•ด์„œ ์กฐ์‚ฌํ•ด์•ผํ•  ์ขŒํ‘œ๋“ค์˜ ์˜จ๋„ ์ค‘ ํ•˜๋‚˜๋ผ๋„ K๋ฏธ๋งŒ์ธ ์นธ์ด ์žˆ์œผ๋ฉด 1๋ฒˆ ๊ณผ์ •์œผ๋กœ ๋Œ์•„๊ฐ€๊ณ  ์•„๋‹ˆ๋ฉด ํ…Œ์ŠคํŠธ๋ฅผ ์ค‘๋‹จํ•œ๋‹ค.

ํ…Œ์ŠคํŠธ ๊ณผ์ •์„ ๋ช‡ ๋ฒˆ ๊ฑฐ์ณค๋Š”์ง€ ์นด์šดํŠธํ•ด์„œ ๋งˆ์ง€๋ง‰์— ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ์ด๋•Œ ์นด์šดํŠธ๊ฐ€ 100์„ ๋„˜์–ด๊ฐ€๊ฒŒ๋˜๋ฉด ํ…Œ์ŠคํŠธ๋ฅผ ์ค‘๋‹จํ•˜๊ณ  101์„ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

<์ฝ”๋“œ>

 
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>
#include <climits>
#include <deque>
#include <algorithm>
#include <iomanip>
#include <map>
#define mod 998244353
using namespace std;
typedef long long ll;
typedef priority_queue<pair<intint>vector<pair<intint>>, greater<pair<intint>>> Priority_queue;
const int INF = INT_MAX;
const int MAX_N = 20 + 5;
int Map[MAX_N][MAX_N];
vector<pair<pair<intint>int>> Fan;
vector<pair<intint>> inspect;
bool Wall[MAX_N][MAX_N][MAX_N][MAX_N];
bool visited[MAX_N][MAX_N];
bool tmp_visited[MAX_N][MAX_N][MAX_N][MAX_N];
int R, C, K, W;
pair<intint> st_direction[] = { {0,1},{0,-1},{-1,0},{1,0} };
pair<intint> dir[4][8= { {{-1,1},{0,1},{1,1},{-1,0},{0,1},{0,1},{1,0},{0,1}}, {{1,-1},{0,-1},{-1,-1},{1,0},{0,-1},{0,-1},{-1,0},{0,-1}},{{-1,-1},{-1,0},{-1,1},{0-1}, { -1,0 }, { -1,0 }, { 0,1 }, { -1,0 }}, { {1,1},{1,0},{1,-1},{0,1},{1,0},{1,0},{0,-1},{1,0}} };
 
void input();
void Wind(pair<intint>int);
bool is_ok(pair<intint>);
bool check_temperature();
void Down_temp();
void Adjust_temp();
 
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    input();
    int Chocolate = 0;
    while (true) {
        for (int i = 0; i < Fan.size(); i++) {
            Wind(Fan[i].first, Fan[i].second);
        }
        Adjust_temp();
        Down_temp();
        Chocolate++;
        if (check_temperature())
            break;
        else if (Chocolate > 100)
            break;
    }
    cout << Chocolate << "\n";
    return 0;
}
 
void input() {
    cin >> R >> C >> K;
    for (int i = 1; i <= R; i++)
        for (int j = 1; j <= C; j++) {
            int num;
            cin >> num;
            if (num >= 1 && num <= 4) {
                Fan.push_back({ {i,j},num - 1 });
            }
            else if (num == 5)
                inspect.push_back({ i,j });
        }
 
    cin >> W;
    for (int i = 1; i <= W; i++) {
        int y, x, wall;
        cin >> y >> x >> wall;
        if (wall == 1) {
            Wall[y][x][y][x + 1= true;
            Wall[y][x + 1][y][x] = true;
        }
        else if (wall == 0) {
            Wall[y][x][y - 1][x] = true;
            Wall[y - 1][x][y][x] = true;
        }
    }
}
 
void Wind(pair<intint> st, int Direction) {
    queue < pair<pair<intint>int>> q;
    memset(visited, falsesizeof(visited));
    q.push({ {st.first + st_direction[Direction].first, st.second + st_direction[Direction].second}, 5 });
    while (!q.empty()) {
        pair<intint> now = q.front().first;
        int strength = q.front().second;
        q.pop();
        if (visited[now.first][now.second])
            continue;
        visited[now.first][now.second] = true;
        Map[now.first][now.second] += strength;
        if (strength == 1)
            continue;
        for (int i = 0; i < 3; i++) {
            pair<intint> next = { now.first + dir[Direction][i].first, now.second + dir[Direction][i].second };
            if (!is_ok(next))
                continue;
            if (visited[next.first][next.second])
                continue;
            if (i == 0) {
                pair<intint> next_ = { now.first + dir[Direction][3].first, now.second + dir[Direction][3].second };
                if (Wall[now.first][now.second][next_.first][next_.second])
                    continue;
                if (Wall[next_.first][next_.second][next_.first + dir[Direction][4].first][next_.second + dir[Direction][4].second])
                    continue;
                q.push({ next, strength - 1 });
            }
            else if (i == 1) {
                pair<intint> next_ = { now.first + dir[Direction][5].first, now.second + dir[Direction][5].second };
                if (Wall[now.first][now.second][next_.first][next_.second])
                    continue;
                q.push({ next,strength - 1 });
            }
            else if (i == 2) {
                pair<intint> next_ = { now.first + dir[Direction][6].first, now.second + dir[Direction][6].second };
                if (Wall[now.first][now.second][next_.first][next_.second])
                    continue;
                if (Wall[next_.first][next_.second][next_.first + dir[Direction][7].first][next_.second + dir[Direction][7].second])
                    continue;
                q.push({ next, strength - 1 });
            }
        }
    }
}
 
bool is_ok(pair<intint> pos) {
    if (pos.first < 1 || pos.first > R || pos.second < 1 || pos.second > C)
        return false;
    return true;
}
 
bool check_temperature() {
    for (int i = 0; i < inspect.size(); i++) {
        if (Map[inspect[i].first][inspect[i].second] < K)
            return false;
    }
    return true;
}
 
void Down_temp() {
    pair<intint> direct[4= { {0,1},{1,0},{0,-1},{-1,0} };
    pair<intint> now = { 1,1 };
    for (int i = 0; i < 4; i++) {
        if (i % 2 == 0) {
            for (int j = 1; j < C; j++) {
                now = { now.first + direct[i].first, now.second + direct[i].second };
                if (Map[now.first][now.second])
                    Map[now.first][now.second]--;
            }
        }
        else {
            for (int j = 1; j < R; j++) {
                now = { now.first + direct[i].first, now.second + direct[i].second };
                if (Map[now.first][now.second])
                    Map[now.first][now.second]--;
            }
        }
    }
}
 
void Adjust_temp() {
    int temp[MAX_N][MAX_N];
    memset(temp, 0sizeof(temp));
    memset(tmp_visited, falsesizeof(tmp_visited));
    for (int i = 1; i <= R; i++) {
        for (int j = 1; j <= C; j++) {
            for (int k = 0; k < 4; k++) {
                pair<intint> next = { i + st_direction[k].first, j + st_direction[k].second };
                if (!is_ok(next))
                    continue;
                if (Wall[i][j][next.first][next.second] || Wall[next.first][next.second][i][j])
                    continue;
                if (tmp_visited[i][j][next.first][next.second] || tmp_visited[next.first][next.second][i][j])
                    continue;
                tmp_visited[i][j][next.first][next.second] = true;
                tmp_visited[next.first][next.second][i][j] = true;
                int Value = (int)floor(abs(Map[next.first][next.second] - Map[i][j]) / 4);
                if (Value >= 1) {
                    if (Map[next.first][next.second] > Map[i][j]) {
                        temp[i][j] += Value;
                        temp[next.first][next.second] -= Value;
                    }
                    else {
                        temp[i][j] -= Value;
                        temp[next.first][next.second] += Value;
                    }
                }
            }
        }
    }
    for (int i = 1; i <= R; i++) {
        for (int j = 1; j <= C; j++)
            Map[i][j] += temp[i][j];
    }
}
cs

์˜ˆ์ œ์—์„œ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ๋Œ€๋ถ€๋ถ„ ์˜จํ’๊ธฐ์˜ ์œ„์น˜๋ฅผ ๊ฐ™๊ฒŒ ์ค˜์„œ ์–ด๋””์„œ ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ๋Š”์ง€ ์ฐพ๊ธฐ๊ฐ€ ์–ด๋ ค์› ๋‹ค. ํ‹€๋ฆฐ ์ฝ”๋“œ์˜€๋Š”๋ฐ๋„ ์šฐ์—ฐํžˆ ์˜ˆ์ œ๋Š” ๋‹ค ๋งž๊ฒŒ๋‚˜์™”์—ˆ๋‹ค.

 

 

728x90