티스토리 뷰

백준

백준 소스코드 [C++] 15092 Sheba's Amoebas

Hani_Levenshtein 2020. 8. 23. 01:29

https://www.acmicpc.net/problem/15092

 

15092번: Sheba’s Amoebas

The first line of input contains two integers m and n, (1 ≤ m, n ≤ 100). This is followed by m lines, each containing n characters. A ‘#’ denotes a black pixel, a ‘.’ denotes a white pixel. For every black pixel, exactly two of its eight neighb

www.acmicpc.net

백준 소스코드 [C++] 15092 Sheba's Amoebas

#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
using namespace std;
int  sum;
char arr[102][102];
bool check[102][102];
queue <pair<int, int>> q;
pair<int, int> pp, p[8] = { {1,-1},{-1,1},{1,1},{-1,-1}, {1,0},{-1,0},{0,1},{0,-1} };
void bfs() {
	while (q.empty() != true) {
		pp = q.front();
		q.pop();
		for (int i = 0;i < 8;i++) {
			if (arr[pp.first + p[i].first][pp.second + p[i].second] == '#'
				&& check[pp.first + p[i].first][pp.second + p[i].second] == true) {
				q.push(make_pair(pp.first + p[i].first, pp.second + p[i].second));
				check[pp.first + p[i].first][pp.second + p[i].second] = false;
			}
		}
	}
	sum++;
	return;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int x,y;

	cin >> y >> x;
	sum = 0;
	for (int i = 0;i < 102;i++) {
		memset(arr[i], 0, sizeof(char) * 102);
		memset(check[i], true, sizeof(bool) * 102);
	}
	for (int j = 1;j <= y;j++)
		for (int i = 1;i <= x;i++) {
			cin >> arr[j][i];
		}

	for (int j = 1;j <= y;j++)
		for (int i = 1;i <= x;i++) {
			if (arr[j][i] == '#' && check[j][i] == true) {
				q.push(make_pair(j, i));
				check[j][i] = false;
				bfs();
			}
		}
	cout << sum << '\n';
	
	return 0;
}
댓글