Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct loc{
- int xc;
- int yc;
- };
- struct rd{
- int r1;
- int c1;
- int r2;
- int c2;
- };
- inline bool operator<(const rd& rr1, const rd& rr2) {
- return tie(rr1.r1, rr1.c1, rr1.r2, rr1.c2)
- < tie(rr2.r1, rr2.c1, rr2.r2, rr2.c2);
- }
- vector<vector<int>> grid;
- vector<vector<bool>> visited;
- set<rd> road;
- //vector<loc> road[100][100];
- vector<loc> cowLoc;
- bool reached = false;
- int N, K, R;
- //vector<vector<vector<pair<int, int>>>> road(100, vector<vector<pair<int, int>>>(100));
- //bool isRoad(int a, int b, int a1, int b1){
- //if()
- //}
- //map<loc, loc> road;
- bool isRoad(rd r){
- if(road.find(r) != road.end()){
- return true;
- }
- return false;
- }
- void dfs(loc u, loc en){
- //cout << "N: " << N << endl;
- //if(visited[0][2]){
- //cout << "visited[0][2]" << endl;
- //}
- if(visited[u.xc][u.yc]){
- //cout << "visited: " << u.xc << " " << u.yc << endl;
- return;
- }
- visited[u.xc][u.yc] = true;
- if(u.xc == en.xc && u.yc == en.yc){
- reached = true;
- }
- //loc v;
- if(u.xc > 0){
- //v.yc = u.yc;
- //v.xc = u.xc - 1;
- if(!isRoad(rd{u.xc, u.yc, u.xc - 1, u.yc})){
- //cout << "going left: curr coor: " << u.xc << " " << u.yc << ", next grid: " << u.xc - 1 << " " << u.yc << endl;
- dfs(loc{u.xc - 1, u.yc}, en);
- }else{
- //cout << "is Road" << endl;
- }
- }
- //cout << "u."
- if(u.xc < N-1){
- //v.xc = u.xc + 1;
- //v.yc = u.yc;
- if(!isRoad(rd{u.xc, u.yc, u.xc + 1, u.yc})){
- //cout << "going right: curr coor: " << u.xc << " " << u.yc << ", next grid: " << u.xc + 1 << " " << u.yc << endl;
- dfs(loc{u.xc + 1, u.yc}, en);
- }else{
- //cout << "is Road" << endl;
- }
- }else{
- //cout << "can't go right, u.xc: " << u.xc << ", N-1: " << N-1 << endl;
- }
- if(u.yc < N-1){
- //v.yc = u.yc+1;
- //v.xc = u.xc;
- if(!isRoad(rd{u.xc, u.yc, u.xc, u.yc+1})){
- //cout << "going up: curr coor: " << u.xc << " " << u.yc << ", next grid: " << u.xc << " " << u.yc+1 << endl;
- dfs(loc{u.xc, u.yc+1}, en);
- }else{
- //cout << "is Road" << endl;
- }
- }else{
- //cout << "can't go up, u.yc: " << u.yc << ", N-1: " << N-1 << endl;
- }
- if(u.yc > 0){
- //v.yc = u.yc-1;
- //v.xc = u.xc;
- if(!isRoad(rd{u.xc, u.yc, u.xc, u.yc-1})){
- //cout << "going down: curr coor: " << u.xc << " " << u.yc << ", next grid: " << u.xc << " " << u.yc-1 << endl;
- dfs(loc{u.xc, u.yc-1}, en);
- }else{
- //cout << "is Road" << endl;
- }
- }else{
- //cout << "can't "
- }
- }
- int main(){
- ifstream fin("countcross.in");
- ofstream fout("countcross.out");
- //int N, K, R;
- fin >> N >> K >> R;
- //cout << "N: " << N << endl;
- grid.resize(N, vector<int>(N));
- cowLoc.resize(K);
- visited.resize(100, vector<bool>(100));
- //visited.resize(N, 0);
- for(int i = 0; i<R; i++){
- int x, y, x1, y1; fin >> x >> y >> x1 >> y1; x--; y--; x1--; y1--;
- //loc c1, c;
- //c1.xc = x1;
- //c1.yc = y1;
- //c.xc = x;
- //c.yc = y;
- //road.insert({x1, y1})
- road.insert(rd{x1, y1, x, y});
- road.insert(rd{x, y, x1, y1});
- //road[x][y].push_back(c1);
- //road[x1][y1].push_back(c);
- }
- for(int i = 0; i<K; i++){
- int a, b; fin >> a >> b; a--; b--;
- cowLoc[i].xc = a;
- cowLoc[i].yc = b;
- }
- for(int c = 0; c<N; c++){
- for(int d = 0; d<N; d++){
- visited[c][d] = false;
- }
- }
- int ans = 0;
- for(int i = 0; i<K; i++){
- for(int j = 0; j<K; j++){
- if(i == j) continue;
- for(int c = 0; c<N; c++){
- for(int d = 0; d<N; d++){
- visited[c][d] = 0;
- }
- }
- //visited.resize(N, 0);
- loc start;
- start.xc = cowLoc[i].xc;
- start.yc = cowLoc[i].yc;
- //cout << "start: " << start.xc << " " << start.yc << endl;
- loc dest;
- dest.xc = cowLoc[j].xc;
- dest.yc = cowLoc[j].yc;
- //cout << "destination: " << dest.xc << " " << dest.yc << endl;
- reached = false;
- dfs(start, dest);
- //cout << "worked" << endl;
- if(!reached){
- ans++;
- }
- }
- }
- fout << ans/2 << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment