Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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 29 30 31
Archives
Today
Total
관리 메뉴

최's 먹공로그

BOJ11559_Puyo Puyo C++ 본문

APS

BOJ11559_Puyo Puyo C++

ChoiSH313 2021. 5. 23. 14:59
  • 문제정리
    https://choish313.tistory.com/121 JAVA 버젼 확인
  • C++ 익숙해 지기 위한 다시 풀기
  • 소스코드
    #include <iostream>
    #include <utility>
    #include <vector>
    #include <queue>
    
    using namespace std;
    
    char board[13][7];
    char board2[13][7];
    int down[7];
    bool vis[13][7];
    vector<pair<int, int>> v;
    queue<pair<int, int>> q;
    queue<pair<int, int>> pq;
    
    void input() {
    	ios::sync_with_stdio(false);
    	cout.tie(NULL);
    	cin.tie(NULL);
    
    	for (int i = 0; i < 12; i++) {
    		for (int j = 0; j < 6; j++) {
    			cin >> board[i][j];
    			board2[i][j] = board[i][j];
    		}
    	}
    }
    
    vector<pair<int, int>> init() {
    	vector<pair<int, int>> temp;
    	for (int i = 0; i < 12; i++) {
    		for (int j = 0; j < 6; j++) {
    			if (board2[i][j] != '.') temp.push_back({ i,j });
    		}
    	}
    	for (int i = 0; i < 6; i++) {
    		down[i] = 0;
    	}
    	return temp;
    }
    
    void initVis() {
    	for (int i = 0; i < 12; i++) {
    		for (int j = 0; j < 6; j++) {
    			vis[i][j] = 0;
    		}
    	}
    }
    
    bool pop(int y, int x) {
    	char color = board2[y][x];
    	int cnt = 1;
    	q.push({ y,x });
    	pq.push({ y,x });
    	vis[y][x] = 1;
    
    	int dy[4] = { -1,1,0,0 };
    	int dx[4] = { 0,0,-1,1 };
    	while (!q.empty()) {
    		int cy = q.front().first;
    		int cx = q.front().second;
    		q.pop();
    
    		for (int i = 0; i < 4; i++) {
    			int ny = cy + dy[i];
    			int nx = cx + dx[i];
    			if (ny < 0 || nx < 0 || ny >= 12 || nx >= 6) continue;
    			if (vis[ny][nx] || board2[ny][nx] != color) continue;
    			vis[ny][nx] = 1;
    			cnt++;
    			q.push({ ny,nx });
    			pq.push({ ny,nx });
    		}
    	}
    
    	if (cnt < 4) {
    		while (!pq.empty()) pq.pop();
    		return 0;
    	}
    	while (!pq.empty()) {
    		int cy = pq.front().first;
    		int cx = pq.front().second;
    		pq.pop();
    		board2[cy][cx] = '.';
    		down[cx] = 1;
    	}
    	return 1;
    }
    
    void downBoard() {
    	for (int j = 0; j < 6; j++) {
    		if (!down[j]) continue;
    		for (int i = 11; i > -1; i--) {
    			if (board2[i][j] == '.') {
    				continue;
    			}
    			int ny = 12;
    			if (ny > 11 || board2[ny][j] != '.') {
    				while (ny > 11 || board2[ny][j] != '.') ny--;
    			}
    			if (ny < i) continue;
    			board2[ny][j] = board2[i][j];
    			if (ny != i) board2[i][j] = '.';
    		}
    	}
    }
    
    int solve() {
    	int cnt = 0;
    	bool flag = 1;
    	while (flag) {
    		flag = 0;
    		v = init();
    
    		for (int i = 0; i < v.size(); i++) {
    			if (board2[v[i].first][v[i].second] == '.') continue;
    			initVis();
    			if (pop(v[i].first, v[i].second)) flag = 1;
    		}
    		if (flag) {
    			downBoard();
    			cnt++;
    		}
    	}
    	return cnt;
    }
    
    int main() {
    	input();
    	cout << solve();
    	return 0;
    }​
  • 숙제
    (1) vector 내용 정리
    (2) pair 내용 정리
    (3) 입력 받을 때 ios::sync_with_stdio 이거 뭔지

언어 익숙해지기 위한 따라치기 백준알고리즘 ID : km7865 감사합니다.

'APS' 카테고리의 다른 글

BOJ1260 DFS와 BFS  (0) 2022.05.22
BOJ1018 체스판 다시 칠하기  (0) 2022.05.22
BOJ11559_Puyo Puyo  (1) 2019.09.05
BOJ17136_색종이 붙이기  (0) 2019.09.03
SWEA1767_[SW Test 샘플문제] 프로세서 연결하기  (2) 2019.09.03