문제
미로탐색
코드
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
using namespace std;
int board[100][100];
int visit[100][100];
int n, m;
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
queue<pair<int, int>> q;
cin >> n >> m;
string s;
for (int i = 0; i < n; i++)
{
cin >> s;
for (int j = 0; j < m; j++)
{
board[i][j] = s[j] - '0';
}
}
visit[0][0] = 1;
q.push({0,0});
while (!q.empty())
{
pair<int, int> cur = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int nx = cur.first + dx[i];
int ny = cur.second + dy[i];
if (nx < 0 || nx >= n || ny < 0 || ny >= m)
continue;
if (visit[nx][ny] != 0 || board[nx][ny] != 1)
continue;
visit[nx][ny] = visit[cur.first][cur.second] + 1;
q.push({nx, ny});
}
}
cout << visit[n-1][m-1];
return (0);
}