-
Notifications
You must be signed in to change notification settings - Fork 1
22-froglike6 #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
22-froglike6 #88
Conversation
dohyeondol1
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ์ ๋จธ๋ฆฌ๋ฅผ ์ธ๋งค๋ค๊ฐ ๋ํ๋์ ํ์ด๋ฅผ ๋ณด๊ณ ์ฝ๋๋ฅผ ์งฐ์ต๋๋ค.
์ผ๋ฐ์ ์ธ ๊ธธ์ฐพ๊ธฐ์๋ค๋ฉด ํ ์ ์์์ ๊ฒ ๊ฐ์๋๋ฐ... ์กฐํฉ์ ์๋ฅผ ๊ตฌํ๋ผ๊ณ ํด์ ๋ชปํ์์ต๋๋ค.. ใ
ใ
๋ํ๋์ ํ์ด๋ฅผ ๋ณด๊ณ ํ์ด์ ์ฝ๋์์์ ๋ฆฌ๋ทฐํ ๊ฑด ๋ฐ๋ก ์๋ ๊ฒ ๊ฐ๋ค์..
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M, K;
cin >> N >> M >> K;
vector<vector<int>> grid(N, vector<int>(M, 0));
for(int i = 0; i < K; i++) {
int x, y;
cin >> x >> y;
grid[x-1][y-1] = 1;
}
int a1, b1, a2, b2, a3, b3, a4, b4;
cin >> a1 >> b1 >> a2 >> b2 >> a3 >> b3 >> a4 >> b4;
a1--; b1--; a2--; b2--; a3--; b3--; a4--; b4--;
vector<vector<bool>> visited(N, vector<bool>(M, false));
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
long long answer = 0;
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
if(grid[i][j] == 0 && !visited[i][j]) {
stack<pair<int, int>> st;
st.push({i, j});
visited[i][j] = true;
long long countA = 0, countB = 0;
while(!st.empty()) {
auto [x, y] = st.top();
st.pop();
if(x >= a1 && x <= a2 && y >= b1 && y <= b2)
countA++;
if(x >= a3 && x <= a4 && y >= b3 && y <= b4)
countB++;
for(int d = 0; d < 4; d++) {
int nx = x + dir[d][0];
int ny = y + dir[d][1];
if(nx >= 0 && nx < N && ny >= 0 && ny < M && grid[nx][ny] == 0 && !visited[nx][ny]) {
visited[nx][ny] = true;
st.push({nx, ny});
}
}
}
answer += countA * countB;
}
}
}
cout << answer << '\n';
return 0;
}๋ฆฌ๋ทฐ๋ฅผ ์ผ์ฃผ์ผ์ ์ ๋ฌ๋ ค๊ณ ํ๋๋ฐ...
์ฃ์กํฉ๋๋ค.. ใ
|
๋ฐ๋ฆฐpr์ ๋๋์ด ์ฐ๊ฒ ๋์๋ค์! ์ ๋ ๋น์ทํ๊ฒ ๋ํ๋์ ์๋์ฝ๋๋ฅผ ์ฐธ๊ณ ํ์ต๋๋ค. ์ ๋ฐ ๋ฐ์์ ์ ์ ๋ก ๋ ์ฌ๋ฆด ์ ์๋๋ก ๋ฐ์ ํด์ผ๊ฒ ์ต๋๋ค.. #include <iostream>
#include <vector>
#include <stack>
#define X first
#define Y second
using namespace std;
using Pos = vector<pair<int, int>>;
template <typename T>
using Grid = vector<vector<T>>;
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
long long dfs(const Pos& starPos, const Pos& boxPos, int n, int m){
long long ans = 0;
Grid<int> grid(n, vector(m, 0));
Grid<bool> visited(n, vector(m, false));
for (auto pos : starPos)
if (pos.X >= 0 && pos.X < n && pos.Y >= 0 && pos.Y < m)
grid[pos.X][pos.Y] = 1;
for (int i=0;i<n;i++)
{
for (int j=0;j<m;j++)
{
if (visited[i][j] == true || grid[i][j] == 1) continue;
long long countA=0, countB=0;
stack<pair<int, int>> st;
st.push({i, j});
visited[i][j] = true;
while (!st.empty())
{
auto [x, y] = st.top();
st.pop();
if(x >= boxPos[0].X && x <= boxPos[1].X && y >= boxPos[0].Y && y <= boxPos[1].Y)
countA++;
if(x >= boxPos[2].X && x <= boxPos[3].X && y >= boxPos[2].Y && y <= boxPos[3].Y)
countB++;
for (int d=0;d<4;d++)
{
int nx = x + dx[d];
int ny = y + dy[d];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && grid[nx][ny] == false && visited[nx][ny] == false){
visited[nx][ny] = true;
st.push({nx, ny});
}
}
}
ans += countA * countB;
}
}
return ans;
}
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int n, m, k;
cin >> n >> m >> k;
Pos starPos(k);
Pos boxPos(4);
for (int i=0;i<k;i++){
cin >> starPos[i].X >> starPos[i].Y;
starPos[i].X--;
starPos[i].Y--;
}
for (int i=0;i<4;i++){
cin >> boxPos[i].X >> boxPos[i].Y;
boxPos[i].X--;
boxPos[i].Y--;
}
cout << dfs(starPos, boxPos, n, m);
return 0;
} |
๐ ๋ฌธ์ ๋งํฌ
โ๏ธ ์์๋ ์๊ฐ
20๋ถ
โจ ์๋ ์ฝ๋
์๋ ์ฝ๋
์ด ๋ฌธ์ ๋$N \times M$ ๊ฒฉ์ ์์ ๋์ธ $K$ ๊ฐ์ ๋ณ์ ํผํด, ๋ ๊ฐ์ ์ง์ฌ๊ฐํ ๊ตฌ์ญ์ ์ฐ๊ฒฐํ ์ ์๋ ์์์ ์์ ๊ฐ์๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ์
๋๋ค.$A$ ์ $B$ ์์ ๋ณ์ด ์๋ ํ ์นธ์ฉ์ ์ถ๋ฐ์ ์ผ๋ก ์ ํด, ์๋ก ์ํ์ข์ฐ๋ก๋ง ์ด๋ํด ๋ง๋ ์ ์๋์ง๋ฅผ ํ๋จํด์ผ ํฉ๋๋ค.
๊ฐ๊ฐ์ ์ง์ฌ๊ฐํ
๋ชจ๋$A$ ๊ตฌ์ญ์ ์นธ๊ณผ ๋ชจ๋ $B$ ๊ตฌ์ญ์ ์นธ์ ์ผ์ผ์ด DFS๋ก ํ์ํ๋ฉด ์๊ฐ๋ณต์ก๋๊ฐ ๋๋ฌด ์ปค์ ธ์ ์๊ฐ์ด๊ณผ๊ฐ ๋ ์ ์์ต๋๋ค.
๊ทธ๋์ ์ ๋ ๋ณ์ ์ํด ๋๋์ด์ง ๋น ์นธ ๋ฉ์ด๋ฆฌ ๋จ์๋ก ์ ๊ทผํ์ต๋๋ค.
๋จผ์ ์ฐ๊ฒฐ๋ ๋ฉ์ด๋ฆฌ๋ณ๋ก ํ ๋ฒ์ DFS๋ฅผ ๋๋ ธ์ต๋๋ค. ๊ฒฉ์๋ฅผ ์ํํ๋ฉฐ ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ๋น ์นธ์ ์ฐพ์ผ๋ฉด, ๊ทธ ์นธ์์ DFS๋ฅผ ๋๋ ค ํ๋์ ๋ฉ์ด๋ฆฌ๋ฅผ ๋ชจ๋ ํ์ํฉ๋๋ค.
๊ทธ๋ฐ ๋ค์, ๊ทธ ๋ฉ์ด๋ฆฌ ์์์$A$ ๊ตฌ์ญ์ ์ํ ์นธ์ ๊ฐ์๋ฅผ $B$ ๊ตฌ์ญ์ ์ํ ์นธ์ ๊ฐ์๋ฅผ
countA์,countB์ ๋ฃ์ ํ, ์ด ๋์ ๊ณฑํด ์ ๋ต์ ๋ํด์ฃผ๋ ํ์์ผ๋ก ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ต๋๋ค.์ด๋ ๊ฒ ๊ธ๋ก ๋ณธ๋ค๋ฉด ์ด๋ ค์ธ ์ ์๋๋ฐ, ๋จ์ํ DFS ๋ฌธ์ (์ฅ์ ๋ฌผ์ด ์๋ ๊ฒฉ์)๋ผ๊ณ ์๊ฐํ๋ฉด ์ฝ์ต๋๋ค ใ ใ .. ๋ฉ์ด๋ฆฌ๋ก ์ ๊ทผํ๋ ๋ฐฉ์์ ๋ ์ฌ๋ฆฌ๋๋ฐ ๋๋ถ๋ถ์ ์๊ฐ์ ์ด ๊ฒ ๊ฐ์ต๋๋ค.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ