티스토리 뷰
2638번: 치즈
첫째 줄에는 모눈종이의 크기를 나타내는 두 개의 정수 N, M (5≤N, M≤100)이 주어진다. 그 다음 N개의 줄에는 모눈종이 위의 격자에 치즈가 있는 부분은 1로 표시되고, 치즈가 없는 부분은 0으로 표
www.acmicpc.net
백준 소스코드 [C++] 2638 치즈
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#include <set>
#define all(v) v.begin(), v.end()
#define pii pair<int,int>
#define make_unique(v) v.erase(unique(v.begin(), v.end()), v.end())
typedef long long ll;
using namespace std;
int arr[102][102];
int one[102][102];
bool chk[102][102];
int cheese = 0;
int dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };
typedef struct dot {
int y, x;
}dot;
void bfs() {
queue<dot>q;
q.push({ 1,1 });
chk[1][1] = true;
while (q.empty() != true) {
dot a = q.front();
q.pop();
for(int i=0;i<4;i++)
if (chk[a.y + dy[i]][a.x + dx[i]] == false) {
if (arr[a.y + dy[i]][a.x + dx[i]] == 0) {
chk[a.y + dy[i]][a.x + dx[i]] = true;
q.push({ a.y + dy[i],a.x + dx[i] });
}
else if (arr[a.y + dy[i]][a.x + dx[i]] == 1) {
one[a.y + dy[i]][a.x + dx[i]]++;
if (2 == one[a.y + dy[i]][a.x + dx[i]]) {
arr[a.y + dy[i]][a.x + dx[i]] = 0;
chk[a.y + dy[i]][a.x + dx[i]] = true;
one[a.y + dy[i]][a.x + dx[i]] = 0;
cheese--;
}
}
}
}
memset(chk, false, sizeof(chk));
memset(one, 0, sizeof(one));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
for (int i = 0;i <= n + 1;i++)
for (int j = 0;j <= m + 1;j++)
arr[i][j] = 2;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++) {
cin >> arr[i][j];
if (arr[i][j] == 1) cheese++;
}
int cnt = 0;
while (0 < cheese) {
bfs();
cnt++;
}
cout << cnt;
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 11723 집합 (0) | 2021.01.05 |
---|---|
백준 소스코드 [C++] 2096 내려가기 (0) | 2021.01.05 |
백준 소스코드 [C++] 1167 트리의 지름 (0) | 2021.01.05 |
백준 소스코드 [C++] 1967 트리의 지름 (0) | 2021.01.05 |
백준 소스코드 [C++] 1149 RGB거리 (0) | 2021.01.04 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- test coverage
- HIG
- 최대 매칭
- 에드몬드 카프 알고리즘
- 네트워크 유량
- 최단경로 알고리즘
- Testable
- 벨만포드 알고리즘
- 부스트캠프 6기
- 강한 순환 참조
- IOS
- CompositionalLayout
- 컴퓨터 추상화
- MeTal
- 최단경로 문제
- WWDC16
- 다익스트라 시간복잡도
- mach-o
- State Restoration
- 네트워크 플로우
- 코딩대회
- rxswift
- observeOn
- CPU와 Memory
- 벨만포드 시간복잡도
- WWDC17
- WWDC19
- WWDC21
- 최단경로문제
- 포드 풀커슨 알고리즘
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함