코테/백준 문제풀이

백준 3055 탈출

밤밭황제 2021. 8. 24. 02:36

 

#include <iostream>
#include <queue>
using namespace std;

int main()
{
	int R, C;
	int dx[] = { 0, 0, 1, -1 };
	int dy[] = { 1, -1, 0, 0 };
	char MAP[50][50];
	int GDepth[50][50] = { 0};
	int WDepth[50][50] = { 0 };
	int answer = -1;

	queue<pair<int, int>> gos;
	queue<pair<int, int>> water;

	scanf("%d %d", &R, &C);
	char input;

	for (int i = 0; i < R; i++)
	{
		for (int j = 0; j < C; j++)
		{
			scanf("%c", &input);
			if (input == '\n')
			{
				j--;
				continue;
			}
			
			if (input == 'S')
			{
				gos.push(make_pair(i, j));
				GDepth[i][j] = 1;
			}
			if (input == '*')
			{
				WDepth[i][j] = 1;
				water.push(make_pair(i, j));
			}
			MAP[i][j] = input;
		}
	}
		
		while (!water.empty())
		{
			int wr = water.front().first;
			int wc = water.front().second;

			water.pop();
			for (int i = 0; i < 4; i++)
			{
				int r = wr + dx[i];
				int c = wc + dy[i];
				if (r >= 0 && c >= 0 && r < R && c < C && MAP[r][c] != 'X' && WDepth[r][c] == 0 && MAP[r][c] != 'D')
				{
					WDepth[r][c] = WDepth[wr][wc] + 1;
					water.push(make_pair(r, c));
				}
				else
					continue;
			}
		}

		while (!gos.empty())
		{
			int gr = gos.front().first;
			int gc = gos.front().second;
			gos.pop();

			for (int i = 0; i < 4; i++)
			{
				int rr = gr + dx[i];
				int cc = gc + dy[i];
				if (MAP[rr][cc] == 'D')
				{
					if (WDepth[gr][gc] == 0 || GDepth[gr][gc] < WDepth[gr][gc])
					{
						if (answer == -1)
							answer = GDepth[gr][gc];
						else if (GDepth[gr][gc] < answer)
							answer = GDepth[gr][gc];
					}

				}
				if (rr >= 0 && cc >= 0 && cc < C && rr < R && MAP[rr][cc] != 'X' && GDepth[rr][cc] == 0 )
				{
					if (WDepth[rr][cc] == 0 || WDepth[rr][cc] > GDepth[gr][gc] + 1)
					{
						GDepth[rr][cc] = GDepth[gr][gc] + 1;
						gos.push(make_pair(rr, cc));
					}
				}
				else
					continue;
			}
		}
	
	if (answer == -1)
		cout << "KAKTUS";
	else
		printf("%d", answer);

	return 0;
}

https://www.acmicpc.net/status?user_id=jmhee3410&problem_id=3055&from_mine=1 

 

채점 현황

 

www.acmicpc.net

 

728x90