카테고리 없음

[2178] 미로탐색

밍디 ٩(ˊ ᗜˋ*)و 2021. 2. 5. 00:36

미로같은거 다 필요없어ㅠㅠㅠㅠㅠㅠ

 

#pragma warning(disable:4996)
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>

using namespace std;


int a[101][101];
int d[101][101];
bool is_visited[101][101];

int dx[4] = { 0,0,-1,1 };
int dy[4] = { -1, 1, 0,0 };

int main() {

	int n, m, res = 0;
	queue<pair<int, int>> q;
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			scanf("%1d", &a[i][j]);

		}
	}
	q.push(make_pair(0, 0));
	is_visited[0][0] = true;
	d[0][0] = 1;

	while (!q.empty()) {
		int y = q.front().first;
		int x = q.front().second;

		q.pop();

		for (int i = 0; i < 4; i++) {
			int nx = x + dx[i];
			int ny = y + dy[i];
			if (0 <= nx && nx < m && 0 <= ny && ny < n) {
				if (a[ny][nx] == 1 && !is_visited[ny][nx]) {
					q.push(make_pair(ny, nx));
					d[ny][nx] = d[y][x] + 1;
					is_visited[ny][nx] = true;
				}
			}
		}
	}


	printf("%d\n", d[n - 1][m - 1]);

}