공부/SWEA

SWEA 11226. [S/W 문제해결 기본] 7일차 - 미로1 (C++)

밤톨ㅇl 2024. 5. 15. 18:31
#include <stdio.h>
#include <queue>

using namespace std;

int map[16][16];
queue<pair<int, int>> q;
int visited[16][16];
bool flag;
int dx[4] = { 0, 0, 1, -1 };
int dy[4] = { 1, -1, 0, 0 };

void solve(int x, int y)
{
	q.push({x, y});
	visited[x][y] = 1;

	while (!q.empty())
	{
		int cntx = q.front().first;
		int cnty = q.front().second;

		visited[cntx][cnty] = 1;

		q.pop();

		for (int i = 0; i < 4; i++)
		{
			int nx = dx[i] + cntx;
			int ny = dy[i] + cnty;

			if (nx < 0 || ny < 0 || nx >= 16 || ny >= 16 || visited[nx][ny])
				continue;
			if (map[nx][ny] == 1)
				continue;
			else if (map[nx][ny] == 0)
				q.push({nx, ny});
			else if (map[nx][ny] == 3)
				flag = true;
		}
	}
}

int main()
{
	for (int t = 1; t <= 10; t++)
	{
		int tc;
		scanf("%d", &tc);

		flag = false;
		int sx = 0, sy = 0;
		for (int i = 0; i < 16; i++)
		{
			for (int j = 0; j < 16; j++)
			{
				scanf("%1d", &map[i][j]);
				if (map[i][j] == 2)
				{
					sx = i;
					sy = j;
				}
			}
		}
		solve(sx, sy);
		printf("#%d %d\n",t, flag);
		for (int i = 0; i < 16; i++)
		{
			for (int j = 0; j < 16; j++)
			{
				visited[i][j] = 0;
			}
		}
	}
	return 0;
}

1. bfs 사용