BFS 톺아보기_섬의 개수, 음식물 피하기

2021. 2. 28. 21:12· Koala - 2기/C반
목차
  1. [섬의 개수 정답 코드]
  2. [음식물 피하기 정답 코드]

youtu.be/Q6pa2akgUGI

[섬의 개수 정답 코드]

#include <iostream>
#include <queue>
using namespace std;

int field[51][51];
int visited[51][51];
//팔방 check
int dx[] = { 0,0,-1,1,-1,1,-1,1};
int dy[] = { -1,1,0,0,-1,1,1,-1 };
int n, m;
queue<pair <int, int> > q;

void bfs(int x, int y,int cnt);
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);


	while (1)
	{
		int cnt = 1;

		cin >> n >> m;
		if (n == 0 && m == 0) return 0;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				cin >> field[i][j];
				visited[i][j] = 0;
			}
		}
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (field[i][j] == 1 && visited[i][j] == 0)
				{
					bfs(i, j, cnt++);
				}
			}
		}
		cout << cnt-1<<'\n';
	}
}

void bfs(int x, int y, int cnt)
{
	q.push(make_pair(x, y));
	visited[x][y] = cnt;
	while (!q.empty())
	{
		x = q.front().first;
		y = q.front().second;
		q.pop();
		for (int k = 0; k < 8; k++)
		{
			int nx = x + dx[k];
			int ny = y + dy[k];
			if (0 <= nx && nx < m && 0 <= ny && ny < n)
			{
				if (field[nx][ny] == 1 && visited[nx][ny] == 0)
				{
					q.push(make_pair(nx, ny));
					visited[nx][ny] = cnt;
				}
			}
		}
	}
	
}

[음식물 피하기 정답 코드]

#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

int field[105][105] = { 0, };
int visited[105][105] = { 0, };
int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };
int n, m, k, max_size = -10;

void bfs(int i, int j);
int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	int row, col;

	cin >> n >> m >> k;

	for (int i = 0; i < k; i++)
	{
		cin >> row >> col;
		field[row][col] = 1;
	}

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			if (field[i][j] == 1 && visited[i][j] == 0)
			{
				bfs(i, j);
			}
		}
	}
	cout << max_size;
	return 0;
}

void bfs(int i, int j)
{
	queue<pair<int, int> > q;
	int cnt = 0;

	q.push({ i, j });
	visited[i][j] = 1;

	while (!q.empty())
	{
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		cnt++;

		for (int k = 0; k < 4; k++)
		{
			int nx = x + dx[k];
			int ny = y + dy[k];

			if (1 <= nx && nx <= n && 1 <= ny && ny <= m)
			{
				if (field[nx][ny] == 1 && visited[nx][ny] == 0)
				{
					q.push({ nx, ny });
					visited[nx][ny] = 1;
				}
			}

 		}
	}
	max_size = max(max_size, cnt);
}

'Koala - 2기 > C반' 카테고리의 다른 글

[BOJ] 14502번. 연구소  (0) 2021.02.21
[BOJ] 2468번. 안전영역  (0) 2021.02.21
[BOJ] 1260번. DFS와 BFS  (0) 2021.02.21
[BOJ] 2346번. 풍선 터뜨리기  (0) 2021.02.18
[1072번] 게임  (0) 2021.02.03
  1. [섬의 개수 정답 코드]
  2. [음식물 피하기 정답 코드]
'Koala - 2기/C반' 카테고리의 다른 글
  • [BOJ] 14502번. 연구소
  • [BOJ] 2468번. 안전영역
  • [BOJ] 1260번. DFS와 BFS
  • [BOJ] 2346번. 풍선 터뜨리기
KauKoala
KauKoala
항공대 알고리즘 동아리 Koala 🥰
Koala항공대 알고리즘 동아리 Koala 🥰
KauKoala
Koala
KauKoala
전체
오늘
어제
  • 분류 전체보기 (1888)
    • 공지 게시판 (10)
    • 정보 게시판 (8)
    • Codeforce (15)
    • acm-icpc (6)
    • Koala - 1기 (16)
    • Koala - 2기 (111)
      • Programming Contest (1)
      • A반 (20)
      • B반 (39)
      • C반 (22)
      • 기초 강의 (18)
    • Koala - 3기 (10)
      • 기초 스터디 (7)
    • Koala - 4기 (67)
    • Koala - 5기 (144)
      • 기초 알고리즘 스터디 (75)
      • 코딩테스트 준비 스터디 (68)
    • Koala - 6기 (102)
      • 기초 알고리즘 스터디 (75)
      • 코딩테스트 준비 스터디 (25)
      • 모의 테스트 스터디 (1)
    • Koala - 7기 (167)
      • 기초 알고리즘 스터디 (97)
      • 코딩테스트 준비 스터디 (68)
      • 모의 테스트 스터디 (1)
    • Koala - 8기 (44)
      • 기초 알고리즘 스터디 (32)
      • 코딩테스트 준비 스터디 (10)
      • 코드포스 버츄얼 스터디 (0)
      • 프로그래머스 LV2 스터디 (0)
    • Koala - 9기 (205)
      • 기초 알고리즘 스터디 (138)
      • 코딩테스트 준비 스터디 (64)
      • 모의테스트 준비 스터디 (1)
    • Koala - 10기 (117)
      • 기초 알고리즘 스터디 (30)
      • 코딩테스트 준비 스터디 (86)
      • 모의테스트 준비 스터디 (1)
    • Koala - 11기 (151)
      • 기초 알고리즘 스터디 (46)
      • 코딩테스트 준비 스터디 (104)
      • 모의테스트 준비 스터디 (1)
    • Koala - 12기 (86)
      • 기초 알고리즘 스터디 (31)
      • 코딩테스트 준비 스터디 (55)
    • Koala - 13기 (119)
      • 기초 알고리즘 스터디 (52)
      • 코딩테스트 준비 스터디 (67)
    • Koala - 14기 (116)
      • 기초 알고리즘 스터디 (39)
      • 코딩테스트 준비 스터디 (77)
    • Koala - 15기 (138)
      • 기초 알고리즘 스터디 (73)
      • 코딩테스트 준비 스터디 (65)
    • Koala - 16기 (47)
      • 코딩테스트 기초 스터디 (16)
      • 코딩테스트 심화 스터디 (31)
    • Koala - 17기 (62)
      • 코딩테스트 기초 스터디 (15)
      • 코딩테스트 심화 스터디 (47)
    • Koala - 18기 (31)
      • 코딩테스트 기초 스터디 (11)
      • 코딩테스트 심화 스터디 (20)
    • Koala - 19기 (42)
      • 코딩테스트 기초 스터디 (7)
      • 코딩테스트 심화 스터디 (35)
    • Koala - 20기 (0)
      • 코딩테스트 기초 스터디 (0)
      • 코딩테스트 심화 스터디 (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • 🐨항공대 알고리즘 학회 Koala 3기 모집
  • 🐨항공대 알고리즘 학회 Koala 2기 모집
  • 소모임 소개

인기 글

태그

  • 백트래킹
  • BFS
  • 백준
  • BOJ
  • 파이썬
  • dp
  • C++
  • dfs

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.3.0
KauKoala
BFS 톺아보기_섬의 개수, 음식물 피하기
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.