https://programmers.co.kr/learn/courses/30/lessons/1844
최단거리 = BFS 풀이
using namespace std;
int dist[101][101];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int solution(vector<vector<int> > maps)
{
int n = maps.size();
int m = maps[0].size();
queue<pair<int,int>> q;
for(int i=0;i<n;i++) fill(dist[i],dist[i]+m, -1);
q.push({0,0});
dist[0][0]=0;
while(!q.empty()){
auto cur =q.front(); q.pop();
for(int dir = 0 ; dir < 4; dir++){
int nx = cur.first +dx[dir];
int ny = cur.second +dy[dir];
if(nx<0 || nx>=n || ny<0 || ny>=m) continue;
if(dist[nx][ny]>=0 || maps[nx][ny] !=1) continue;
dist[nx][ny] = dist[cur.first][cur.second]+1;
q.push({nx,ny});
}
}
int answer = dist[n-1][m-1];
if(answer==-1) return -1;
else answer++;
return answer;
}