반응형
✏️ 문제 링크
https://www.acmicpc.net/problem/17086
✏️ 문제 설명 (더보기 클릭 👆🏻)
✏️ 문제 코드
#include <bits/stdc++.h>
using namespace std;
using pii=pair<int, int>;
#define MAX 51
typedef struct Node{
int x, y; long cnt;
}Node;
int dx[]={-1,1,1,-1,-1,1,0,0};
int dy[]={1,1,-1,-1,0,0,-1,1};
int bfs(int cx, int cy){
int r=98765432;
bool vis[MAX][MAX];
for(int i=0; i<N; i++){
for(int j=0; j<M; j++){
vis[i][j]=false;
}
}
queue<Node> q; q.push({cx, cy, 0}); vis[cx][cy]=true;
while(!q.empty()){
Node cur=q.front(); q.pop();
int x=cur.x; int y=cur.y; int c=cur.cnt;
if(m[x][y]==1){
return c;
}
for(int i=0; i<8; i++){
int nx=x+dx[i]; int ny=y+dy[i];
if(nx<0 || nx>=N || ny<0 || ny>=M || vis[nx][ny]) continue;
q.push({nx, ny, c+1});
vis[nx][ny]=true;
}
}
}
int main(){
int answer=0; cin>>N>>M;
for(int i=0; i<N; i++){
for(int j=0; j<M; j++){
cin>>m[i][j];
}
}
for(int i=0; i<N; i++){
for(int j=0; j<M; j++){
if(m[i][j]==0){
answer=max(answer, bfs(i, j));
}
}
}
cout<<answer<<"\n";
return 0;
}
⭐ if feedback and question : comment please⭐
반응형
'Algorithm 💫 > Problem Solving' 카테고리의 다른 글
[백준] 14226번: 이모티콘 / C++ (0) | 2021.09.17 |
---|---|
[백준] 13549번: 숨바꼭질3 / C++ (0) | 2021.09.17 |
[백준] 16953번: A -> B / C++ (0) | 2021.09.16 |
[백준] 2606번: 바이러스 / C++ (0) | 2021.09.16 |
[백준] 1743번: 음식물 피하기 / C++ (0) | 2021.09.16 |
댓글