-
[C++] ์ผ์ฑ 1868. ํํํํ ์ง๋ขฐ์ฐพ๊ธฐ์ฝ๋ฉํ ์คํธ/์ฝ๋ฉํ ์คํธ ์ฐ์ต 2020. 10. 28. 09:30728x90
(2์๊ฐ) ๋ฑ๋ด๋ ํ ์ฌ์ฉํด์ ์ฐ์์ ์ผ๋ก ์ซ์๊ฐ ํ์๋๋ ๋ฒ์๋ฅผ ๊ตฌํ๊ณ ํ๋ฒ์ผ๋ก ์น๋ฉด ๋ ๊ฑฐ ๊ฐ์๋๋ฐ ๋ง์ ๊ตฌํํ ๋๋ ํ์ฐธ ํค๋งด.
#include<iostream> #include<vector> #include<queue> #include<string.h> using namespace std; int table[300][300]; bool check[300][300]; int N; int dir[8][2] = { {-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1} }; // ์ฃผ๋ณ์ ์ง๋ขฐ๊ฐ ์๋์ง ํ๋ณ bool mineCheck(int x, int y) { for (int i = 0; i < 8; i++) { int new_x = x + dir[i][0]; int new_y = y + dir[i][1]; if (new_x < 0 || new_y < 0 || new_x >= N || new_y >= N) continue; if (table[new_x][new_y] == 1) return false; } return true; } // i, j ์ขํ๋ฅผ ๊ธฐ์ค์ผ๋ก ์ฐ์์ ์ผ๋ก ์ซ์๊ฐ ๋ฐํ์ง๋ ๋ถ๋ถ์ ์ ๋ถ ํ์ void findMine(int i, int j) { queue<pair<int, int>> q; q.push(make_pair(i, j)); while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); for (int i = 0; i < 8; i++) { int new_x = x + dir[i][0]; int new_y = y + dir[i][1]; if (new_x < 0 || new_y < 0 || new_x >= N || new_y >= N) continue; if (!check[new_x][new_y]) { check[new_x][new_y] = true; if (mineCheck(new_x, new_y)) { q.push(make_pair(new_x, new_y)); } } } } } int main() { int T; cin >> T; for (int t = 1; t <= T; t++) { memset(check, false, sizeof(check)); memset(table, 0, sizeof(table)); cin >> N; // ์ง๋ขฐ๊ฐ ์๋ ๊ฒฝ์ฐ 1๋ก ์นํ for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { char tmp; cin >> tmp; if (tmp == '*') { table[i][j] = 1; check[i][j] = true; } } } int answer = 0; // ์ฃผ๋ณ์ ์ง๋ขฐ๊ฐ ์์ด ์ฐ์์ ์ผ๋ก ์ซ์๊ฐ ํ์๋๋ ๋ถ๋ถ for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (!check[i][j]) { if (mineCheck(i, j)) { check[i][j] = true; findMine(i, j); answer++; } } } } // ์ฃผ๋ณ์ ์ง๋ขฐ๊ฐ ์์ด ๋ฐ๋ก ํด๋ฆญํด์ผ ๋๋ ๋ถ๋ถ for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (!check[i][j]) { answer++; } } } cout << "#" << t << " " << answer << endl; } return 0; }
728x90'์ฝ๋ฉํ ์คํธ > ์ฝ๋ฉํ ์คํธ ์ฐ์ต' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ