소에 index를 매긴 뒤 소마다 bfs를 돌아 index가 더 큰 소를 만나지 않는 횟수를 세주면 된다.
쌍을 구해야하는데 마리를 출력해서 헤맸다...
#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(0), cin.tie(0)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define all(v) v.begin(), v.end()
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
bool roads[101][101][101][101], visited[101][101];
vector<pii> cows;
int n, k, r;
void input() {
cin >> n >> k >> r;
for(int i = 0; i < r; ++i) {
int r1, c1, r2, c2; cin >> r1 >> c1 >> r2 >> c2;
roads[r1][c1][r2][c2] = true;
}
for(int i = 1; i <= k; ++i) {
int a, b; cin >> a >> b;
cows.push_back({a, b});
}
}
void bfs(int x, int y) {
queue<pii> q;
visited[x][y] = true;
q.push({x, y});
while(!q.empty()) {
auto [fx, fy] = q.front(); q.pop();
for(int i = 0; i < 4; ++i) {
int nx = fx + dx[i], ny = fy + dy[i];
if(nx < 1 || nx > n || ny < 1 || ny > n) continue;
if(visited[nx][ny] || roads[fx][fy][nx][ny] || roads[nx][ny][fx][fy]) continue;
visited[nx][ny] = true;
q.push({nx, ny});
}
}
}
int main() {
fastio;
input();
int cnt = 0;
for(int i = 0; i < k; ++i) {
memset(visited, false, sizeof(visited));
bfs(cows[i].first, cows[i].second);
for(int j = i+1; j < k; ++j) {
if(!visited[cows[j].first][cows[j].second]) cnt++;
}
}
cout << cnt;
}
백준 21922 학부 연구생 민상 혼내주기 (0) | 2022.02.28 |
---|---|
백준 21921 블로그 혼내주기 (0) | 2022.02.28 |
백준 1937 욕심쟁이 판다 혼내주기 (0) | 2021.09.27 |
백준 22358 스키장 (0) | 2021.08.02 |
백준 22352 항체 인식 혼내주기 (0) | 2021.08.02 |
댓글 영역