반응형
-문제
-코드
#include<stdio.h>
#include<queue>
#include<vector>
using namespace std;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int map[101][101];
int dis[101][101];
int main()
{
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
scanf("%1d", &map[i][j]);
// 붙어있는 수
}
}
queue<pair<int,int> > q;
dis[1][1] = 1;
q.push({ 1,1 });
map[1][1] = 0;
while (!q.empty())
{
pair<int, int> tmp = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int xx = tmp.first + dx[i];
int yy = tmp.second + dy[i];
if (xx <= 0 || yy <= 0 || xx > n || yy > m) continue;
if (map[xx][yy] == 1)
{
map[xx][yy] = 0;
q.push({ xx,yy });
dis[xx][yy] = dis[tmp.first][tmp.second] + 1;
}
}
}
printf("%d", dis[n][m]);
return 0;
}
반응형
'이론 > 코딩테스트' 카테고리의 다른 글
[백준] 1920번 수 찾기 (이분탐색) (0) | 2022.07.30 |
---|---|
[백준] 2667번 단지번호붙이기 (BFS) (0) | 2022.07.30 |
[백준] 1697번 숨바꼭질 (BFS) (0) | 2022.07.30 |
[백준] 2467번 용액 (투포인터 알고리즘) (0) | 2022.07.30 |
[백준] 1940번 주몽 (투포인터 알고리즘) (0) | 2022.07.30 |