티스토리 뷰

백준

백준 소스코드 [C++] 4677 Oil Deposits

Hani_Levenshtein 2020. 8. 23. 01:26

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

 

4677번: Oil Deposits

The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 ≤ m ≤ 100 and 1 ≤ n ≤ 100. Fo

www.acmicpc.net

백준 소스코드 [C++] 4677 Oil Deposits

#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 t;
	int x = 1, y = 1;
	while(x!=0 && y!=0) {
		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();
				}
			}
		if ( x!=0 && y!=0 )cout << sum << '\n';
	}
	return 0;
}
댓글