πŸ™‡β€β™€οΈ[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;
}

νƒœκ·Έ:

μΉ΄ν…Œκ³ λ¦¬:

μ—…λ°μ΄νŠΈ: