-
소가 길을 건너간 이유 6 - 14466Algorithm/BOJ 2020. 4. 1. 16:5912345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394//14466 - 소가 길을 건너간 이유 6#include <iostream>#include <vector>using namespace std;int N, K, R;int arr[100][100];vector<vector<int>> adj;bool isCow[10000];int xarr[2] = {1, 0};int yarr[2] = {0, 1};vector<int> bucket;bool visit[10000];int count, ans;int ch(int x1, int y1, int x2, int y2){if(y1 + 1 == y2) return 1;else if(x1 + 1 == x2) return 2;else return 0;}void init(){cin >> N >> K >> R;ans = 0;bucket = vector<int>();adj = vector<vector<int>>(N*N);int x1, y1, x2, y2;for (int i = 0; i < R; ++i){cin >> x1 >> y1 >> x2 >> y2;--x1, --y1, --x2, --y2;if(x1 > x2 || y1 > y2) swap(x1, x2) , swap(y1, y2);arr[x1][y1] += ch(x1, y1, x2, y2);}for(int x = 0; x<N; ++x)for(int y = 0; y<N; ++y){int here = x*N+y;int right = x*N+y+1;int down = (x+1)*N+y;if(y+1 == N) right = here;if(x+1 == N) down = here;if(!arr[x][y] || arr[x][y] == 1){adj[here].push_back(down);adj[down].push_back(here);}if(!arr[x][y] || arr[x][y] == 2){adj[here].push_back(right);adj[right].push_back(here);}}for(int i = 0; i<K; ++i){cin >> x1 >> y1;--x1, --y1;isCow[x1*N+y1] = true;}}void dfs(int here){visit[here] = true;if(isCow[here]) ++count;for(int i = 0; i<adj[here].size(); ++i){int there = adj[here][i];if(!visit[there])dfs(there);}}int main(){init();for(int i = 0; i<N*N; ++i)if(!visit[i]){count = 0;dfs(i);bucket.push_back(count);}for(int i = 0; i<bucket.size(); ++i)for(int j = i+1; j<bucket.size(); ++j)ans += bucket[i]*bucket[j];cout << ans;}
cs https://www.acmicpc.net/problem/14466
14466번: 소가 길을 건너간 이유 6
문제 소가 길을 건너간 이유는 그냥 길이 많아서이다. 존의 농장에는 길이 너무 많아서, 길을 건너지 않고서는 별로 돌아다닐 수가 없다. 존의 농장에 대대적인 개편이 있었다. 이제 작은 정사각형 목초지가 N×N (2 ≤ N ≤ 100) 격자로 이루어져 있다. 인접한 목초지 사이는 일반적으로 자유롭게 건너갈 수 있지만, 그 중 일부는 길을 건너야 한다. 농장의 바깥에는 높은 울타리가 있어서 소가 농장 밖으로 나갈 일은 없다. K마리의 (1 ≤ K ≤ 100,
www.acmicpc.net
소가 길을 건너간 이유 6
그래프로 모델링 한 후 dfs를 이용해 길을 건너지 않고 만날수 있는 소들의 마리수를 셌습니다.
'Algorithm > BOJ' 카테고리의 다른 글
사탕 줍는 로봇 - 15892 (0) 2020.07.06 세 용액 - 2473 (0) 2020.04.16 네트워크 연결 - 3780 (0) 2020.04.01 정점들의 거리 - 1761 (0) 2020.03.30 알파벳 - 1987 (0) 2020.03.29