[BOJ] 1780λ² | μ’
μ΄μ κ°μ (C++)
2023. 8. 31. 22:38γCoding Test/BOJ
πλ¬Έμ 보λ¬κ°κΈ°
π§π»π»νμ΄ κ³Όμ
π1992λ² λ¬Έμ μ λΉμ·νλ°, 4κ°μ λΆν μμ 9κ°λ‘ λμλ€λ μ λ§ λ€λ₯Έ κ²μ λΉΌλ©΄ νμ΄λ λκ°μ΅λλ€.
μ λ§λ‘ λκ°μμ, λ°λ‘ μ μ λ§μ΄ μλ€μ...
βοΈμμ€ μ½λ λ° κ²°κ³Ό
#include <iostream>
#include <vector>
#include <map>
#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
using namespace std;
const int ALL_NOT_SAME = -2;
int FindNumberInSquare(const vector<vector<int>>& square, int N, int rowBegin, int colBegin)
{
int number = ALL_NOT_SAME;
for (int row = rowBegin; row < rowBegin + N; row++)
{
for (int col = colBegin; col < colBegin + N; col++)
{
if (row == rowBegin and col == colBegin)
{
number = square[row][col];
continue;
}
if (square[row][col] != number)
return ALL_NOT_SAME;
}
}
return number;
}
void Solution(const vector<vector<int>>& paper, map<int, int>& counts, int N, int rowBegin, int colBegin)
{
if (N == 1)
{
int number = paper[rowBegin][colBegin];
counts[number]++;
return;
}
int number = FindNumberInSquare(paper, N, rowBegin, colBegin);
if (number != ALL_NOT_SAME)
{
counts[number]++;
return;
}
N /= 3;
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
Solution(paper, counts, N, rowBegin + N * row, colBegin + N * col);
}
int main()
{
FAST_IO
int N;
cin >> N;
vector<vector<int>> paper(N, vector<int>(N));
for (int row = 0; row < N; row++)
for(int col = 0; col < N; col++)
cin >> paper[row][col];
map<int, int> answers
{
{-1, 0},
{0, 0},
{1, 0}
};
Solution(paper, answers, N, 0, 0);
for (const auto& answer : answers)
cout << answer.second << "\n";
return 0;
}
μ¬κ· ν¨μλ₯Ό 9κ° μ¨λ¨λ κ±Έ, 2μ€ forλ¬ΈμΌλ‘ λ°κΎΈμλλ° μ μκ°μ΄ λ λμμκΉμ...
728x90
λ°μν
'Coding Test > BOJ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] 1654λ² | λμ μλ₯΄κΈ° (C++) (0) | 2023.09.02 |
---|---|
[BOJ] 1629λ² | κ³±μ (C++) (0) | 2023.09.01 |
[BOJ] 1992λ² | μΏΌλνΈλ¦¬ (C++) (0) | 2023.08.31 |
[BOJ] 2630λ² | μμ’ μ΄ λ§λ€κΈ° (C++) (0) | 2023.08.31 |
[BOJ] 11053λ² | κ°μ₯ κΈ΄ μ¦κ°νλ λΆλΆ μμ΄ (C++) (0) | 2023.08.29 |