자기개발👨‍💻/코딩 알고리즘

[c++] 백준 1652 누울 자리를 찾아라

천숭이 2020. 8. 15. 22:25

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

 

1652번: 누울 자리를 찾아라

첫째 줄에 방의 크기 N이 주어진다. N은 1이상 100이하의 정수이다. 그 다음 N줄에 걸쳐 N개의 문자가 들어오는데 '.'은 아무것도 없는 곳을 의미하고, 'X'는 짐이 있는 곳을 의미한다.

www.acmicpc.net

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

int main() {
	int n;
	int cnta = 0;
	int cntb = 0;
	int dot = 0;
	string arr[100];
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
	}

	//가로 카운트 시작
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (arr[i][j] =='.' ) {   // . 을 만날때마다 dot+=1
				dot += 1;
			}
			else {                   // x 를 만났을때 dot가 2이상이면 cnt++ 그런다음 dot초기화
				if (dot >= 2){
					cnta += 1;
				}
				dot = 0;
			}

		}
		if (dot >= 2) {             //다음줄로 넘어갈 때는 x를 만나지 않기때문에 
			cnta += 1;              //x를 만나지 않더라도 dot개수가 2 이상이면 cnt++

		}
		dot = 0;
	}

	dot = 0;
	//세로 카운트 시작 전에 dot초기화!
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (arr[j][i] == '.') {
				dot += 1;
			}
			else {
				if (dot >= 2) {
					cntb += 1;
				}
				dot = 0;				
			}
		}
		if (dot >= 2) {
			cntb += 1;
		}
		dot = 0;
	}

	cout << cnta << ' ' << cntb;
}