반응형

https://www.acmicpc.net/problem/17836

 

17836번: 공주님을 구해라!

용사는 마왕이 숨겨놓은 공주님을 구하기 위해 (N, M) 크기의 성 입구 (1,1)으로 들어왔다. 마왕은 용사가 공주를 찾지 못하도록 성의 여러 군데 마법 벽을 세워놓았다. 용사는 현재의 가지고 있는

www.acmicpc.net

 

bfs를 이용하여 구현하였습니다. "그람"을 습득했을 때와 습득하지 않았을 때의 visit를 별도로 분리하였습니다.

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

int n, m, T;
int map[100][100];
bool visit[2][100][100];
int dx[] = { -1,1,0,0 };
int dy[] = { 0, 0, -1, 1 };

void bfs() {
	queue<pair<pair<int, int>, pair<int, int>>> q;
	q.push({ {0, 0}, {0, 0} });
	visit[0][0][0] = true;
	while (!q.empty()) {
		int x = q.front().first.first;
		int y = q.front().first.second;
		int t = q.front().second.first;
		int s = q.front().second.second;
		q.pop();

		if (t > T) {
			printf("Fail");
			return;
		}
		if (x == n - 1 && y == m - 1) {
			printf("%d", t);
			return;
		}
		for (int i = 0; i < 4; i++) {
			int nx = x + dx[i];
			int ny = y + dy[i];
			if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
			if (visit[s][nx][ny]) continue;
			if (!s && map[nx][ny] == 1) continue;
			visit[s][nx][ny] = true;
			if (map[nx][ny] == 2) {
				visit[1][nx][ny] = true; // 방금 지나온 위치
				q.push({ {nx, ny}, {t + 1, 1} });
			}
			else {
				q.push({ {nx, ny}, {t + 1, s} });
			}
			
		}
	}
	printf("Fail");
}
int main() {
	scanf("%d %d %d", &n, &m, &T);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			scanf("%d", &map[i][j]);
	bfs();
}
반응형

'Algorithm' 카테고리의 다른 글

백준 2437 : 저울  (0) 2021.11.11
백준 2302 : 극장 좌석  (0) 2021.11.11
백준 16936 : 나3곱2  (0) 2021.11.11
백준 17070 : 파이프 옮기기 1  (0) 2021.11.11
백준 16916 : 부분 문자열  (0) 2021.11.11

+ Recent posts