๐Ÿ™‡โ€โ™€๏ธ[Gold I] ๊ตฌ์Šฌ ํƒˆ์ถœ 2 - 13460

๋ฌธ์ œ ๋งํฌ

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 2040 KB, ์‹œ๊ฐ„: 0 ms

๋ถ„๋ฅ˜

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰, ๊ทธ๋ž˜ํ”„ ์ด๋ก , ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰, ๊ตฌํ˜„, ์‹œ๋ฎฌ๋ ˆ์ด์…˜

์ œ์ถœ ์ผ์ž

2024๋…„ 2์›” 6์ผ 14:33:50

๋ฌธ์ œ ์„ค๋ช…

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

๋ณด๋“œ์˜ ์„ธ๋กœ ํฌ๊ธฐ๋Š” N, ๊ฐ€๋กœ ํฌ๊ธฐ๋Š” M์ด๊ณ , ํŽธ์˜์ƒ 1ร—1ํฌ๊ธฐ์˜ ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค. ๊ฐ€์žฅ ๋ฐ”๊นฅ ํ–‰๊ณผ ์—ด์€ ๋ชจ๋‘ ๋ง‰ํ˜€์ ธ ์žˆ๊ณ , ๋ณด๋“œ์—๋Š” ๊ตฌ๋ฉ์ด ํ•˜๋‚˜ ์žˆ๋‹ค. ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ์˜ ํฌ๊ธฐ๋Š” ๋ณด๋“œ์—์„œ 1ร—1ํฌ๊ธฐ์˜ ์นธ์„ ๊ฐ€๋“ ์ฑ„์šฐ๋Š” ์‚ฌ์ด์ฆˆ์ด๊ณ , ๊ฐ๊ฐ ํ•˜๋‚˜์”ฉ ๋“ค์–ด๊ฐ€ ์žˆ๋‹ค. ๊ฒŒ์ž„์˜ ๋ชฉํ‘œ๋Š” ๋นจ๊ฐ„ ๊ตฌ์Šฌ์„ ๊ตฌ๋ฉ์„ ํ†ตํ•ด์„œ ๋นผ๋‚ด๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋•Œ, ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ๊ตฌ๋ฉ์— ๋“ค์–ด๊ฐ€๋ฉด ์•ˆ ๋œ๋‹ค.

์ด๋•Œ, ๊ตฌ์Šฌ์„ ์†์œผ๋กœ ๊ฑด๋“œ๋ฆด ์ˆ˜๋Š” ์—†๊ณ , ์ค‘๋ ฅ์„ ์ด์šฉํ•ด์„œ ์ด๋ฆฌ ์ €๋ฆฌ ๊ตด๋ ค์•ผ ํ•œ๋‹ค. ์™ผ์ชฝ์œผ๋กœ ๊ธฐ์šธ์ด๊ธฐ, ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ธฐ์šธ์ด๊ธฐ, ์œ„์ชฝ์œผ๋กœ ๊ธฐ์šธ์ด๊ธฐ, ์•„๋ž˜์ชฝ์œผ๋กœ ๊ธฐ์šธ์ด๊ธฐ์™€ ๊ฐ™์€ ๋„ค ๊ฐ€์ง€ ๋™์ž‘์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๊ฐ๊ฐ์˜ ๋™์ž‘์—์„œ ๊ณต์€ ๋™์‹œ์— ์›€์ง์ธ๋‹ค. ๋นจ๊ฐ„ ๊ตฌ์Šฌ์ด ๊ตฌ๋ฉ์— ๋น ์ง€๋ฉด ์„ฑ๊ณต์ด์ง€๋งŒ, ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ๊ตฌ๋ฉ์— ๋น ์ง€๋ฉด ์‹คํŒจ์ด๋‹ค. ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ๋™์‹œ์— ๊ตฌ๋ฉ์— ๋น ์ ธ๋„ ์‹คํŒจ์ด๋‹ค. ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ์€ ๋™์‹œ์— ๊ฐ™์€ ์นธ์— ์žˆ์„ ์ˆ˜ ์—†๋‹ค. ๋˜, ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ์˜ ํฌ๊ธฐ๋Š” ํ•œ ์นธ์„ ๋ชจ๋‘ ์ฐจ์ง€ํ•œ๋‹ค. ๊ธฐ์šธ์ด๋Š” ๋™์ž‘์„ ๊ทธ๋งŒํ•˜๋Š” ๊ฒƒ์€ ๋” ์ด์ƒ ๊ตฌ์Šฌ์ด ์›€์ง์ด์ง€ ์•Š์„ ๋•Œ ๊นŒ์ง€์ด๋‹ค.

๋ณด๋“œ์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ตœ์†Œ ๋ช‡ ๋ฒˆ ๋งŒ์— ๋นจ๊ฐ„ ๊ตฌ์Šฌ์„ ๊ตฌ๋ฉ์„ ํ†ตํ•ด ๋นผ๋‚ผ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ๋ณด๋“œ์˜ ์„ธ๋กœ, ๊ฐ€๋กœ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋‘ ์ •์ˆ˜ N, M (3 โ‰ค N, M โ‰ค 10)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์— ๋ณด๋“œ์˜ ๋ชจ์–‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ธธ์ด M์˜ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ๋ฌธ์ž์—ด์€ '.', '#', 'O', 'R', 'B' ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. '.'์€ ๋นˆ ์นธ์„ ์˜๋ฏธํ•˜๊ณ , '#'์€ ๊ณต์ด ์ด๋™ํ•  ์ˆ˜ ์—†๋Š” ์žฅ์• ๋ฌผ ๋˜๋Š” ๋ฒฝ์„ ์˜๋ฏธํ•˜๋ฉฐ, 'O'๋Š” ๊ตฌ๋ฉ์˜ ์œ„์น˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค. 'R'์€ ๋นจ๊ฐ„ ๊ตฌ์Šฌ์˜ ์œ„์น˜, 'B'๋Š” ํŒŒ๋ž€ ๊ตฌ์Šฌ์˜ ์œ„์น˜์ด๋‹ค.

์ž…๋ ฅ๋˜๋Š” ๋ชจ๋“  ๋ณด๋“œ์˜ ๊ฐ€์žฅ์ž๋ฆฌ์—๋Š” ๋ชจ๋‘ '#'์ด ์žˆ๋‹ค. ๊ตฌ๋ฉ์˜ ๊ฐœ์ˆ˜๋Š” ํ•œ ๊ฐœ ์ด๋ฉฐ, ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ์€ ํ•ญ์ƒ 1๊ฐœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

์ตœ์†Œ ๋ช‡ ๋ฒˆ ๋งŒ์— ๋นจ๊ฐ„ ๊ตฌ์Šฌ์„ ๊ตฌ๋ฉ์„ ํ†ตํ•ด ๋นผ๋‚ผ ์ˆ˜ ์žˆ๋Š”์ง€ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ, 10๋ฒˆ ์ดํ•˜๋กœ ์›€์ง์—ฌ์„œ ๋นจ๊ฐ„ ๊ตฌ์Šฌ์„ ๊ตฌ๋ฉ์„ ํ†ตํ•ด ๋นผ๋‚ผ ์ˆ˜ ์—†์œผ๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

๋‘ ์‹œ๊ฐ„์„ ์‚ฌ์šฉํ•˜๊ณ  ํ’€์ง€๋ชปํ•œ ๋ฌธ์ œ์ด๋‹คโ€ฆ..

๋‹ค๋ฅธ ๋ธ”๋กœ๊ทธ ๊ทธ๋Œ€๋กœ ์ด์šฉํ•จ.. ใ…Ž;;

BFS ๋ฌธ์ œ์ด๊ณ  ๋ฌธ์ œ์˜ ์กฐ๊ฑด๋“ค์„ ๋‹ค ์ฒดํฌํ•ด์•ผํ•œ๋‹ค.

๊ณต์ด ์›€์ง์ด๋Š” ๊ฒƒ์€ move ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ๋‹ค.

void move(int& rx, int& ry, int& distance, int& i)
{
	while (map[rx + dx[i]][ry + dy[i]] != '#' && map[rx][ry] != 'O')
	{
		rx += dx[i];
		ry += dy[i];
		distance += 1;
	}
}

์ด ๋•Œ ๊ณต์ด ์›€์ง์ธ ํšŸ์ˆ˜๊นŒ์ง€ ์ฒดํฌํ•˜๋Š”๋ฐ ์ด๋Š” ๊ณต์ด ๊ณ‚์ณค์„ ๋–„ ๋” ๋งŽ์ด ์›€์ง์ธ ๊ณต์ด ๋˜๋Œ์•„๊ฐ€์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

BFS์ด๋ฏ€๋กœ queue๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ step ๊ตฌ์กฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค.

struct step
{
	int Rx, Ry;
	int Bx, By;
	int Count;
};

Count๊ฐ€ ์ด ์›€์ง์ด ํšŸ์ˆ˜์ด๋‹ค.

BFS

void BFS(int Rx, int Ry, int Bx, int By)
{
	queue<step> q;
	q.push({ Rx,Ry,Bx,By,0 });
	visit[Rx][Ry][Rx][Ry] = true;
	while (!q.empty())
	{
		int rx = q.front().Rx;
		int ry = q.front().Ry;
		int bx = q.front().Bx;
		int by = q.front().By;
		int count = q.front().Count;
		q.pop();

		if (count >= 10) break;

		for (int i = 0; i < 4; i++)
		{
			int nrx = rx, nry = ry, nbx = bx, nby = by;
			int rc = 0, bc = 0, ncount = count + 1;

			move(nrx, nry, rc, i);
			move(nbx, nby, bc, i);

			if (map[nbx][nby] == 'O') continue;
			if (map[nrx][nry] == 'O')
			{
				cout << ncount;
				return;
			}

			if (nrx == nbx && nry == nby)
			{
				if (rc > bc) nrx -= dx[i], nry -= dy[i];
				else nbx -= dx[i], nby -= dy[i];
			}

			if (visit[nrx][nry][nbx][nby]) continue;
			visit[nrx][nry][nbx][nby] = true;
			q.push({ nrx,nry,nbx,nby,ncount });
		}
	}
	cout << -1;
}

๋ฐฉ๋ฌธ ํ™•์ธ visited๋Š” 4์ฐจ์›๋ฐฐ์—ด๋กœ ๋นจ๊ฐ„๊ณต ํŒŒ๋ž€๊ณต ๋‘ ๊ฐœ๋ฅผ ํ™•์ธํ•œ๋‹ค.
ํŒŒ๋ž€๊ณต์ด ๊ตฌ๋ฉ์— ๋“ค์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ๋Š” ๋„˜์–ด๊ฐ„๋‹ค.
๋นจ๊ฐ„๊ณต์ด ๊ตฌ๋ฉ์— ๋“ค์–ด๊ฐ€๋ฉด ์›€์ง์ธ ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ๋๋‚ธ๋‹ค.
์›€์ง์ธ ํšŸ์ˆ˜๊ฐ€ 10 ์ด์ƒ์ด๋ฉด ๊ทธ๋ƒฅ ๋๋‚ธ๋‹ค.

๋นจ๊ฐ„๊ณต๊ณผ ํŒŒ๋ž€๊ณต์ด ๊ฐ™์€ ๊ณณ์ด๋ฉด ๋” ๋งŽ์ด ์›€์ง์ธ ๊ณต์ด ๊ทธ ํ•œ ์นธ ์ „์œผ๋กœ ๋˜๋Œ์•„๊ฐ„๋‹ค.

๊ฐ™์€ ํ–‰๋™์„ ๋ฐ˜๋ณต ์•ˆํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฐฉ๋ฌธํ•œ ๊ณณ์€ ๋„˜์–ด๊ฐ„๋‹ค.

void solve()
{
	cin >> N >> M;
	int Rx = 0, Ry = 0, Bx = 0, By = 0;
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < M; j++)
		{
			cin >> map[i][j];
			if (map[i][j] == 'R')
			{
				Rx = i; Ry = j;
			}
			else if (map[i][j] == 'B')
			{
				Bx = i; By = j;
			}
		}
	}
	BFS(Rx, Ry, Bx, By);
}

solveํ•จ์ˆ˜๋Š” ์ด๋ ‡๊ฒŒ ํ•ด์„œ ์ฒ˜์Œ ๋นจ๊ฐ„๊ณต๊ณผ ํŒŒ๋ž€๊ณต์˜ ์œ„์น˜๋ฅผ ๊ธฐ์–ตํ•ด์ค€๋‹ค.

๊ทธ ํ›„ BFS๋ฅผ ๋Œ๋ฆฌ๋ฉด ๋!

๐Ÿš€์ „์ฒด ์ฝ”๋“œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

struct step
{
	int Rx, Ry;
	int Bx, By;
	int Count;
};

char map[11][11];
bool visit[11][11][11][11];
int N, M;
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };

void move(int& rx, int& ry, int& distance, int& i)
{
	while (map[rx + dx[i]][ry + dy[i]] != '#' && map[rx][ry] != 'O')
	{
		rx += dx[i];
		ry += dy[i];
		distance += 1;
	}
}

void BFS(int Rx, int Ry, int Bx, int By)
{
	queue<step> q;
	q.push({ Rx,Ry,Bx,By,0 });
	visit[Rx][Ry][Rx][Ry] = true;
	while (!q.empty())
	{
		int rx = q.front().Rx;
		int ry = q.front().Ry;
		int bx = q.front().Bx;
		int by = q.front().By;
		int count = q.front().Count;
		q.pop();

		if (count >= 10) break;

		for (int i = 0; i < 4; i++)
		{
			int nrx = rx, nry = ry, nbx = bx, nby = by;
			int rc = 0, bc = 0, ncount = count + 1;

			move(nrx, nry, rc, i);
			move(nbx, nby, bc, i);

			if (map[nbx][nby] == 'O') continue;
			if (map[nrx][nry] == 'O')
			{
				cout << ncount;
				return;
			}

			if (nrx == nbx && nry == nby)
			{
				if (rc > bc) nrx -= dx[i], nry -= dy[i];
				else nbx -= dx[i], nby -= dy[i];
			}

			if (visit[nrx][nry][nbx][nby]) continue;
			visit[nrx][nry][nbx][nby] = true;
			q.push({ nrx,nry,nbx,nby,ncount });
		}
	}
	cout << -1;
}

void solve()
{
	cin >> N >> M;
	int Rx = 0, Ry = 0, Bx = 0, By = 0;
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < M; j++)
		{
			cin >> map[i][j];
			if (map[i][j] == 'R')
			{
				Rx = i; Ry = j;
			}
			else if (map[i][j] == 'B')
			{
				Bx = i; By = j;
			}
		}
	}
	BFS(Rx, Ry, Bx, By);
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	//freopen("input.txt", "rt", stdin);

	solve();

	return 0;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: