BOJ 2206 벽 부수고 이동하기

 
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <functional>

typedef long long int ll;
using namespace std;
#define INF 1234567890
#define N 200000

const int LIMIT = 1e+9;
int n, m;

string mmap[1005];
int dist1[1005][1005];
int dist2[1005][1005];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0 ,0};

bool is_out_of_range(int x, int y) {
	return x < 0 || x >= n || y < 0 || y >= m;
}


int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> n >> m;

	for (int i = 0; i < n; i++) {
		cin >> mmap[i];
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {

			dist1[i][j] = dist2[i][j] = -1;
		}
	}

	queue<pair<int, int>> que;
	que.push({0, 0});
	dist1[0][0] = 1;
	
	while (!que.empty()) {
		pair<int, int> here = que.front();
		que.pop();

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

			if (is_out_of_range(cx, cy) || mmap[cx][cy] == '1' || dist1[cx][cy] != -1)
				continue;
			dist1[cx][cy] = dist1[here.first][here.second] + 1;
			que.push({ cx, cy });
		}
	}

	que.push({ n - 1, m - 1 });
	dist2[n - 1][m - 1] = 1;

	while (!que.empty()) {
		pair<int, int> here = que.front();
		que.pop();

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

			if (is_out_of_range(cx, cy) || mmap[cx][cy] == '1' || dist2[cx][cy] != -1)
				continue;
			dist2[cx][cy] = dist2[here.first][here.second] + 1;
			que.push({ cx, cy });
		}
	}
	int res = 1234567890;

	if (dist1[n - 1][m - 1] != -1) {
		res = dist1[n - 1][m - 1];
	}


	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (mmap[i][j] == '0') continue;

			int min_1 = 1234567890;
			int min_2 = 1234567890;

			for (int d = 0; d < 4; d++) {
				int cx = i + dx[d];
				int cy = j + dy[d];

				if (is_out_of_range(cx, cy))
					continue;
				if (dist1[cx][cy] != -1)
					min_1 = min(min_1, dist1[cx][cy]);
				if (dist2[cx][cy] != -1)
					min_2 = min(min_2, dist2[cx][cy]);
			}
			if (min_1 == 1234567890 || min_2 == 1234567890)
				continue;
			res = min(res, min_1 + min_2 + 1);
			
		}
	}

	if (res == 1234567890)
		cout << "-1";
	else
		cout << res;

	return 0;
}



벽 부수고 이동하기

시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
2 초 192 MB 19342 4351 2700 23.087%

문제

N×M의 행렬로 표현되는 맵이 있다. 맵에서 0은 이동할 수 있는 곳을 나타내고, 1은 이동할 수 없는 벽이 있는 곳을 나타낸다. 당신은 (1, 1)에서 (N, M)의 위치까지 이동하려 하는데, 이때 최단 경로로 이동하려 한다. 최단경로는 맵에서 가장 적은 개수의 칸을 지나는 경로를 말하는데, 이때 시작하는 칸과 끝나는 칸도 포함해서 센다.

만약에 이동하는 도중에 한 개의 벽을 부수고 이동하는 것이 좀 더 경로가 짧아진다면, 벽을 한 개 까지 부수고 이동하여도 된다.

맵이 주어졌을 때, 최단 경로를 구해 내는 프로그램을 작성하시오.

입력

첫째 줄에 N(1 ≤ N ≤ 1,000), M(1 ≤ M ≤ 1,000)이 주어진다. 다음 N개의 줄에 M개의 숫자로 맵이 주어진다. (1, 1)과 (N, M)은 항상 0이라고 가정하자.

출력

첫째 줄에 최단 거리를 출력한다. 불가능할 때는 -1을 출력한다.

예제 입력 1

6 4
0100
1110
1000
0000
0111
0000

예제 출력 1

15

예제 입력 2

4 4
0111
1111
1111
1110

예제 출력 2

-1

출처