๐Ÿ™‡โ€โ™€๏ธ[Gold I] ์—ด์‡  - 9328

๋ฌธ์ œ ๋งํฌ

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

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

๋ถ„๋ฅ˜

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

์ œ์ถœ ์ผ์ž

2024๋…„ 3์›” 12์ผ 14:56:59

๋ฌธ์ œ ์„ค๋ช…

์ƒ๊ทผ์ด๋Š” 1์ธต ๋นŒ๋”ฉ์— ์นจ์ž…ํ•ด ๋งค์šฐ ์ค‘์š”ํ•œ ๋ฌธ์„œ๋ฅผ ํ›”์ณ์˜ค๋ ค๊ณ  ํ•œ๋‹ค. ์ƒ๊ทผ์ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ‰๋ฉด๋„์—๋Š” ๋ฌธ์„œ์˜ ์œ„์น˜๊ฐ€ ๋ชจ๋‘ ๋‚˜ํƒ€๋‚˜ ์žˆ๋‹ค. ๋นŒ๋”ฉ์˜ ๋ฌธ์€ ๋ชจ๋‘ ์ž ๊ฒจ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋ฌธ์„ ์—ด๋ ค๋ฉด ์—ด์‡ ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์ƒ๊ทผ์ด๋Š” ์ผ๋ถ€ ์—ด์‡ ๋ฅผ ์ด๋ฏธ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ์ผ๋ถ€ ์—ด์‡ ๋Š” ๋นŒ๋”ฉ์˜ ๋ฐ”๋‹ฅ์— ๋†“์—ฌ์ ธ ์žˆ๋‹ค. ์ƒ๊ทผ์ด๋Š” ์ƒํ•˜์ขŒ์šฐ๋กœ๋งŒ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ƒ๊ทผ์ด๊ฐ€ ํ›”์น  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์„œ์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ˆ˜๋Š” 100๊ฐœ๋ฅผ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ฒซ์งธ ์ค„์—๋Š” ์ง€๋„์˜ ๋†’์ด์™€ ๋„ˆ๋น„ h์™€ w (2 โ‰ค h, w โ‰ค 100)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ h๊ฐœ ์ค„์—๋Š” ๋นŒ๋”ฉ์„ ๋‚˜ํƒ€๋‚ด๋Š” w๊ฐœ์˜ ๋ฌธ์ž๊ฐ€ ์ฃผ์–ด์ง€๋ฉฐ, ๊ฐ ๋ฌธ์ž๋Š” ๋‹ค์Œ ์ค‘ ํ•˜๋‚˜์ด๋‹ค.

  • '.'๋Š” ๋นˆ ๊ณต๊ฐ„์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  • '*'๋Š” ๋ฒฝ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์ƒ๊ทผ์ด๋Š” ๋ฒฝ์„ ํ†ต๊ณผํ•  ์ˆ˜ ์—†๋‹ค.
  • '$'๋Š” ์ƒ๊ทผ์ด๊ฐ€ ํ›”์ณ์•ผํ•˜๋Š” ๋ฌธ์„œ์ด๋‹ค.
  • ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋Š” ๋ฌธ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  • ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋Š” ์—ด์‡ ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ๊ทธ ๋ฌธ์ž์˜ ๋Œ€๋ฌธ์ž์ธ ๋ชจ๋“  ๋ฌธ์„ ์—ด ์ˆ˜ ์žˆ๋‹ค.

๋งˆ์ง€๋ง‰ ์ค„์—๋Š” ์ƒ๊ทผ์ด๊ฐ€ ์ด๋ฏธ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์—ด์‡ ๊ฐ€ ๊ณต๋ฐฑ์—†์ด ์ฃผ์–ด์ง„๋‹ค. ๋งŒ์•ฝ, ์—ด์‡ ๋ฅผ ํ•˜๋‚˜๋„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” "0"์ด ์ฃผ์–ด์ง„๋‹ค.

์ƒ๊ทผ์ด๋Š” ์ฒ˜์Œ์—๋Š” ๋นŒ๋”ฉ์˜ ๋ฐ–์— ์žˆ์œผ๋ฉฐ, ๋นŒ๋”ฉ ๊ฐ€์žฅ์ž๋ฆฌ์˜ ๋ฒฝ์ด ์•„๋‹Œ ๊ณณ์„ ํ†ตํ•ด ๋นŒ๋”ฉ ์•ˆํŒŽ์„ ๋“œ๋‚˜๋“ค ์ˆ˜ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋ฌธ์— ๋Œ€ํ•ด์„œ, ๊ทธ ๋ฌธ์„ ์—ด ์ˆ˜ ์žˆ๋Š” ์—ด์‡ ์˜ ๊ฐœ์ˆ˜๋Š” 0๊ฐœ, 1๊ฐœ, ๋˜๋Š” ๊ทธ ์ด์ƒ์ด๊ณ , ๊ฐ๊ฐ์˜ ์—ด์‡ ์— ๋Œ€ํ•ด์„œ, ๊ทธ ์—ด์‡ ๋กœ ์—ด ์ˆ˜ ์žˆ๋Š” ๋ฌธ์˜ ๊ฐœ์ˆ˜๋„ 0๊ฐœ, 1๊ฐœ, ๋˜๋Š” ๊ทธ ์ด์ƒ์ด๋‹ค. ์—ด์‡ ๋Š” ์—ฌ๋Ÿฌ ๋ฒˆ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๋งˆ๋‹ค, ์ƒ๊ทผ์ด๊ฐ€ ํ›”์น  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์„œ์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

๋ฌด์‹ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์—ˆ๋Š”๋ฐ ํ†ต๊ณผ๋๋‹ค..!!

bfs๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๊ณ , bfs๋ฅผ ๋Œ๋ฆด ๋•Œ ์ƒˆ๋กœ์šด ํ‚ค๋ฅผ ๋ฐ›์•˜๋‹ค๋ฉด ๊ทธ ๊ณณ์—์„œ ํ•œ๋ฒˆ๋” bfs๋ฅผ ๋Œ๋ ค์•ผํ•œ๋‹ค.

๋ชจ๋“  ๋ฌธ์—์„œ ํ•œ๋ฒˆ์”ฉ ๋Œ๋ฆฌ๋ฉด ๋๋‚˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๋งˆ์ง€๋ง‰ ๋ฌธ์—์„œ ์ƒˆ๋กœ์šดํ‚ค๋ฅผ ์–ป๊ณ  ์ฒ˜์Œ ๋ฌธ์—์„œ ์‹œ์ž‘ํ•ด์•ผ ์—ด๋ฆฌ๋Š” ๊ตฌ์กฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ด์ค‘for๋ฌธ์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.

image

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

#define _CRT_SECURE_NO_WARNINGS

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

using namespace std;

int _loopCnt = 0, _document = 0;
int _t, _h, _w, cnt = 0;
int dy[] = { -1, 0, 1, 0 };
int dx[] = { 0, 1, 0, -1 };
vector<vector<char>> _board;
vector<pair<int, int>> _gate;
vector<vector<bool>> _visited;
map<char, bool> _inventory;

bool BFS(int y, int x)
{
	bool getKey = false;
	if (_board[y][x] >= 'A' && _board[y][x] <= 'Z')
	{
		// ์ธ๋ฒคํ† ๋ฆฌ ํ™•์ธ.
		if (_inventory[_board[y][x] + 32] == true)
			_board[y][x] = '.';
		else
			return false;
	}
	else if (_board[y][x] >= 'a' && _board[y][x] <= 'z')
	{
		_inventory[_board[y][x]] = true;
		_board[y][x] = '.';
	}
	else if (_board[y][x] == '$')
	{
		cnt++;
		_board[y][x] = '.';
	}

	queue<pair<int, int>> q;
	q.push({ y, x });
	_visited[y][x] = true;

	pair<int, int> here;

	while (q.empty() == false)
	{
		here = q.front();
		q.pop();

		for (int i = 0; i < 4; ++i)
		{
			int ny = here.first + dy[i];
			int nx = here.second + dx[i];

			if (ny < 0 || ny >(_h - 1) || nx < 0 || nx >(_w - 1))
				continue;
			if (_visited[ny][nx])
				continue;
			if (_board[ny][nx] == '*')
				continue;

			if (_board[ny][nx] == '.')
			{
				_visited[ny][nx] = true;
				q.push({ ny, nx });
			}
			else if (_board[ny][nx] >= 'A' && _board[ny][nx] <= 'Z')
			{
				// ์ธ๋ฒคํ† ๋ฆฌ ํ™•์ธ ํ•„์š”.
				if (_inventory[_board[ny][nx] + 32] == true)
				{
					_board[ny][nx] = '.';
					_visited[ny][nx] = true;
					q.push({ ny, nx });
				}
			}
			else if (_board[ny][nx] >= 'a' && _board[ny][nx] <= 'z')
			{
				_visited[ny][nx] = true;
				q.push({ ny, nx });
				_inventory[_board[ny][nx]] = true;
				_board[ny][nx] = '.';
				getKey = true;
			}
			else if (_board[ny][nx] == '$')
			{
				_visited[ny][nx] = true;
				q.push({ ny, nx });
				_board[ny][nx] = '.';
				cnt++;

				if (cnt == _document)
					return false;
			}
		}
	}

	_visited.clear();
	_visited = vector<vector<bool>>(_h, vector<bool>(_w));

	if (getKey == true)
		return true;
	else
		return false;
}

void solve()
{
	cin >> _t;

	for (int i = 0; i < _t; ++i)
	{
		cin >> _h >> _w;
		cnt = 0;
		_board.clear();
		_board = vector<vector<char>>(_h, vector<char>(_w));
		_visited.clear();
		_visited = vector<vector<bool>>(_h, vector<bool>(_w));
		_gate.clear();
		_inventory.clear();

		_loopCnt = 0;
		_document = 0;

		//_map.clear();
		// ํ‰๋ฉด๋„ ์ž…๋ ฅ
		for (int y = 0; y < _h; ++y)
		{
			string str;
			cin >> str;
			for (int x = 0; x < _w; ++x)
			{
				_board[y][x] = str[x];
				if ((x == 0 && _board[y][x] != '*') || (y == 0 && _board[y][x] != '*') || (x == _w - 1 && _board[y][x] != '*') || (y == _h - 1 && _board[y][x] != '*'))
					_gate.push_back({ y, x });
				if (_board[y][x] >= 'A' && _board[y][x] <= 'Z')
					_loopCnt++;
				if (_board[y][x] == '$')
					_document++;
			}
		}

		for (int key = 'a'; key < 'a' + 26; ++key)
		{
			_inventory[key] = false;
		}

		// ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์—ด์‡ 
		string keys;
		cin >> keys;
		for (int key = 0; key < keys.size(); ++key)
		{
			_inventory[keys[key]] = true;
		}

		for (int l = 0; l < _gate.size(); ++l)
		{
			for (int k = 0; k < _gate.size(); ++k)
			{
				if (cnt == _document)
					break;

				if (BFS(_gate[k].first, _gate[k].second) == true)
				{
					k--;
				}

			}
		}
		cout << cnt << '\n';
	}
}

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

	solve();

	return 0;
}

ํƒœ๊ทธ:

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

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