https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14hwZqABsCFAYD&categoryId=AV14hwZqABsCFAYD&categoryType=CODE&problemTitle=1220&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

#include <iostream>
#include <vector>

using namespace std;

int cnt;
void check(vector<int>arr) {
	while (1) {
		if (arr.empty() || arr[0] == 1) break;
		arr.erase(arr.begin());
	}

	while (1) {
		if (arr.empty() || arr[arr.size()-1] == 2) break;
		arr.pop_back();
	}

	for (int i = 1; i < arr.size(); i++) { //N극에서 S극으로 바뀌는 횟수만 카운트
		if (arr[i - 1] == 1 && arr[i] == 2) cnt++;
	}

}

int main() {

	for (int i = 0; i < 10; i++)
	{
		cnt = 0;
		int map[100][100];
		int n;
		cin >> n;
		for (int y = 0; y < n; y++) {
			for (int x = 0; x < n; x++) {
				cin >> map[y][x];
			}
		}

		vector<int>tmp;
		for (int y = 0; y < n; y++) {
			for (int x = 0; x < n; x++) {
				if (map[x][y] != 0) tmp.push_back(map[x][y]);
			}
			check(tmp);
			tmp.clear();
		}

		cout << "#" << i + 1 << " " << cnt << endl;
		
		
	}

	return 0;
}

+ Recent posts