๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ์ธ๊ณต๋ถ€/Algorithm

[๋ฐฑ์ค€][Node.js] 1012๋ฒˆ : ์œ ๊ธฐ๋† ๋ฐฐ์ถ”

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2021. 6. 1.

 

 

 

Algorithm

-  ์œ ๊ธฐ๋† ๋ฐฐ์ถ” -

 


 

๋ฌธ์ œ

์ฐจ์„ธ๋Œ€ ์˜๋†์ธ ํ•œ๋‚˜๋Š” ๊ฐ•์›๋„ ๊ณ ๋žญ์ง€์—์„œ ์œ ๊ธฐ๋† ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค.

๋†์•ฝ์„ ์“ฐ์ง€ ์•Š๊ณ  ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๋ ค๋ฉด ๋ฐฐ์ถ”๋ฅผ ํ•ด์ถฉ์œผ๋กœ๋ถ€ํ„ฐ ๋ณดํ˜ธํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ๋‚˜๋Š” ํ•ด์ถฉ ๋ฐฉ์ง€์— ํšจ๊ณผ์ ์ธ ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด๋ฅผ ๊ตฌ์ž…ํ•˜๊ธฐ๋กœ ๊ฒฐ์‹ฌํ•œ๋‹ค.

์ด ์ง€๋ ์ด๋Š” ๋ฐฐ์ถ”๊ทผ์ฒ˜์— ์„œ์‹ํ•˜๋ฉฐ ํ•ด์ถฉ์„ ์žก์•„ ๋จน์Œ์œผ๋กœ์จ ๋ฐฐ์ถ”๋ฅผ ๋ณดํ˜ธํ•œ๋‹ค.

ํŠนํžˆ, ์–ด๋–ค ๋ฐฐ์ถ”์— ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด๊ฐ€ ํ•œ ๋งˆ๋ฆฌ๋ผ๋„ ์‚ด๊ณ  ์žˆ์œผ๋ฉด ์ด ์ง€๋ ์ด๋Š” ์ธ์ ‘ํ•œ ๋‹ค๋ฅธ ๋ฐฐ์ถ”๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์–ด, ๊ทธ ๋ฐฐ์ถ”๋“ค ์—ญ์‹œ ํ•ด์ถฉ์œผ๋กœ๋ถ€ํ„ฐ ๋ณดํ˜ธ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

(ํ•œ ๋ฐฐ์ถ”์˜ ์ƒํ•˜์ขŒ์šฐ ๋„ค ๋ฐฉํ–ฅ์— ๋‹ค๋ฅธ ๋ฐฐ์ถ”๊ฐ€ ์œ„์น˜ํ•œ ๊ฒฝ์šฐ์— ์„œ๋กœ ์ธ์ ‘ํ•ด์žˆ๋‹ค๊ณ  ๊ฐ„์ฃผํ•œ๋‹ค)

 

ํ•œ๋‚˜๊ฐ€ ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๋Š” ๋•…์€ ๊ณ ๋ฅด์ง€ ๋ชปํ•ด์„œ ๋ฐฐ์ถ”๋ฅผ ๊ตฐ๋ฐ๊ตฐ๋ฐ ์‹ฌ์–ด๋†“์•˜๋‹ค.

๋ฐฐ์ถ”๋“ค์ด ๋ชจ์—ฌ์žˆ๋Š” ๊ณณ์—๋Š” ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด๊ฐ€ ํ•œ ๋งˆ๋ฆฌ๋งŒ ์žˆ์œผ๋ฉด ๋˜๋ฏ€๋กœ ์„œ๋กœ ์ธ์ ‘ํ•ด์žˆ๋Š” ๋ฐฐ์ถ”๋“ค์ด ๋ช‡ ๊ตฐ๋ฐ์— ํผ์ ธ์žˆ๋Š”์ง€ ์กฐ์‚ฌํ•˜๋ฉด ์ด ๋ช‡ ๋งˆ๋ฆฌ์˜ ์ง€๋ ์ด๊ฐ€ ํ•„์š”ํ•œ์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด ๋ฐฐ์ถ”๋ฐญ์ด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉด ์ตœ์†Œ 5๋งˆ๋ฆฌ์˜ ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

(0์€ ๋ฐฐ์ถ”๊ฐ€ ์‹ฌ์–ด์ ธ ์žˆ์ง€ ์•Š์€ ๋•…์ด๊ณ , 1์€ ๋ฐฐ์ถ”๊ฐ€ ์‹ฌ์–ด์ ธ ์žˆ๋Š” ๋•…์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.)

 

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด ํ•„์š”ํ•œ ์ตœ์†Œ์˜ ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด ๋งˆ๋ฆฌ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

๋ฌธ์ œ ํ’€๊ธฐ

const readline = require("readline");
const rl = readline.createInterface({
	input: process.stdin,
	output: process.stdout,
});
let problemCount = 0; //๋ฌธ์ œ ๊ฐœ์ˆ˜
let arrInfo = []; // ๊ฐ€๋กœ์„ธ๋กœ์™€ ๋ฐฐ์ถ”์ •๋ณด
let inputIndex = 0; // input์•ˆ ์œ„์น˜ ์„ค์ •
let untilIndex = 0; // ๊ณ ์ •๋˜์–ด์•ผ ํ•˜๋Š” ์ธ๋ฑ์Šค
let input = [];
let graph = [];
let visited = [];
let result = [];
let count = 0;
rl.on("line", function (line) {
	if (problemCount === 0) problemCount = Number(line.toString());
	else if (line.toString().split(" ").length === 3) {
		arrInfo.push(
			line
				.toString()
				.split(" ")
				.map((el) => Number(el))
		);
	} else {
		input.push(
			line
				.toString()
				.split(" ")
				.map((el) => Number(el))
		);
	}
}).on("close", function () {
	for (let i = 0; i < problemCount; i++) {
		count = 0;
		solution(arrInfo[i]);
	}
	console.log(result.join("\n"));
	process.exit();
});

let solution = (problem) => {
	graph = []; // ์ดˆ๊ธฐํ™”
	visited = []; // ์ดˆ๊ธฐํ™”
	graph = Array.from(Array(problem[1]), () => Array(problem[0]).fill(0));
	visited = Array.from(Array(problem[1]), () => Array(problem[0]).fill(false));
	for (let i = inputIndex; i < inputIndex + problem[2]; i++) {
		graph[input[i][1]][input[i][0]] = 1;
	}

	let BFS = (x, y) => {
		let dy = [0, 0, -1, 1];
		let dx = [-1, 1, 0, 0];
		let queue = [];

		visited[x][y] = true;
		queue.push([x, y]);

		while (queue.length) {
			let [mx, my] = queue.shift();

			for (let i = 0; i < 4; i++) {
				let xx = mx + dx[i];
				let yy = my + dy[i];

				if (0 <= xx && xx < problem[1] && 0 <= yy && yy < problem[0]) {
					if (graph[xx][yy] === 1 && visited[xx][yy] === false) {
						visited[xx][yy] = true;
						queue.push([xx, yy]);
					}
				}
			}
		}
		return;
	};
	for (let i = untilIndex; i < untilIndex + problem[2]; i++) {
		if (visited[input[i][1]][input[i][0]] === false) {
			BFS(input[i][1], input[i][0]);
			count++;
		}
		inputIndex++;
	}
	untilIndex = inputIndex;
	result.push(count);
	return;
};

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€