반응형
#include < iostream>
#include < vector>
#include < string>
#include < queue>
using namespace std;
bool check[100][100];
int board[100][100];
int dis[100][100];
int dy[4] = {-1,0,1,0};
int dx[4] = {0,-1,0,1};
int n,m;
void bfs(int y,int x){
queue<pair<int,int>> q;
q.push(make_pair(y,x));
check[y][x] = true;
dis[y][x] = 1;
while(!q.empty()){
y = q.front().first;
x = q.front().second;
q.pop();
for(int dir=0;dir<4;dir++){
int ny = y + dy[dir];
int nx = x + dx[dir];
if(ny<0 || nx<0 || ny>=n || nx>=m)
continue;
if(check[ny][nx] == false && board[ny][nx] == 1){
q.push(make_pair(ny,nx));
check[ny][nx] = true;
dis[ny][nx] = dis[y][x] + 1;
}
}
}
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++){
string temp;
cin>>temp;
for(int j=0;j<temp.length();j++){
if(temp[j] == '0'){
board[i][j] = 0;
}else{
board[i][j] = 1;
}
}
}
/*
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cout<<board[i][j]<<" ";
}
cout<<endl;
}
*/
int cnt;
bfs(0,0);
cout<<dis[n-1][m-1]<<endl;
return 0;
}
https://www.acmicpc.net/problem/2178
2178번: 미로 탐색
첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.
www.acmicpc.net
반응형
'개발자 > 알고리즘' 카테고리의 다른 글
[BOJ] 14226번 이모티콘 (0) | 2019.10.08 |
---|---|
[BOJ] 1697번 숨바꼭질 (0) | 2019.10.07 |
[BOJ] 4963번 섬의 개수 (0) | 2019.09.22 |
[BOJ] 2667번 단지번호붙이기 (0) | 2019.09.22 |
[BOJ] 11724번 연결 요소의 개수 (0) | 2019.09.21 |