题目描述

Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of roads, making it impossible for his cows to travel around without crossing many of them.

为什么牛过马路? 其中一个简单的原因就是农民约翰的农场有很多道路,使得他的母牛不得不穿越许多道路。

FJ's farm is arranged as an N \times NN×N square grid of fields (2 \leq N \leq 1002≤N≤100), Certain pairs of adjacent fields (e.g., north-south or east-west) are separated by roads, and a tall fence runs around the external perimeter of the entire grid, preventing cows from leaving the farm. Cows can move freely from any field to any other adjacent field (north, east, south, or west), although they prefer not to cross roads unless absolutely necessary.

FJ的农场在 N\times NN×N的网格中( 2\le N\le 1002≤N≤100),某些相邻的区域(例如,南北或东西)由道路分隔,高大的围栏围绕着整个格栅的外围,防止牛离开农场。 牛可以从任何场地自由移动到任何其他相邻的区域(北,东,南或西),不过除非不得已,她们并不愿意穿越道路。

There are KK cows (1 \leq K \leq 100, K \leq N^21≤K≤100,K≤N​2​​) on FJ's farm, each located in a different field. A pair of cows is said to be "distant" if, in order for one cow to visit the other, it is necessary to cross at least one road. Please help FJ count the number of distant pairs of cows.

在FJ的农场有 KK 头牛(1\le K\le 100,K\le N^{2}1≤K≤100,K≤N​2​​),每个位于不同的区域。 定义一对牛是“遥远的”,是指让一头牛访问另一头牛时,必须至少穿过一条路。 请帮助FJ计算有多少对牛是“遥远的”。

输入输出格式

输入格式:

The first line of input contains NN, KK, and RR. The next RR lines describe RR roads that exist between pairs of adjacent fields. Each line is of the form rr cc r'r​′​​ c'c​′​​ (integers in the range 1 \ldots N1…N), indicating a road between the field in (row rr, column cc) and the adjacent field in (row r'r​′​​, column c'c​′​​). The final KK lines indicate the locations of the KK cows, each specified in terms of a row and column.

第一行输入包含 NN, KK和 RR。 接下来的 RR 行描述存在于相邻区域对之间的 RR 条路。 每行的格式为 rr ; cc ; r'r​′​​ ; c'c​′​​(都是在 1...N1...N中的整数),表示在两个相邻的区域(第rr行第cc列,和第$r​'$ ​​ 行第$c​'$ ​​ 列)之间的路。 最终的KK行表示 KK 头牛的位置,也用行列来表示。

输出格式:

Print the number of pairs of cows that are distant.

输出遥远的牛数量对。

输入输出样例

输入样例#1:

3 3 3
2 2 2 3
3 3 3 2
3 3 2 3
3 3
2 2
2 3
输出样例#1:

2

说明

感谢@太阳之神2015 提供翻译

思路:暴力模拟一下就好。

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 105
using namespace std;
int dx[]={,,,-};
int dy[]={,,-,};
int ans,tim,n,k,r;
int x[MAXN],y[MAXN];
int vis[MAXN][MAXN],b[MAXN][MAXN][];
struct nond{
int x,y;
};
queue<nond>que;
void bfs(int s){
nond v;
v.x=x[s];v.y=y[s];
que.push(v);
vis[x[s]][y[s]]=++tim;
while(!que.empty()){
nond now=que.front();
que.pop();
for(int i=;i<;i++)
if(b[now.x][now.y][i]==){
int tx=now.x+dx[i];
int ty=now.y+dy[i];
if(tx>=&&ty>=&&tx<=n&&ty<=n&&!vis[tx][ty]){
vis[tx][ty]=tim;
nond vv;
vv.x=tx;
vv.y=ty;
que.push(vv);
}
}
}
}
int main(){
scanf("%d%d%d",&n,&k,&r);
memset(b,,sizeof(b));
for(int i=;i<=r;i++){
int x,y,tx,ty;
scanf("%d%d%d%d",&x,&y,&tx,&ty);
if(x==tx){
if(y<ty){
b[x][y][]=;
b[tx][ty][]=;
}
else{
b[x][y][]=;
b[tx][ty][]=;
}
}
else{
if(x<tx){
b[x][y][]=;
b[tx][ty][]=;
}
else{
b[x][y][]=;
b[tx][ty][]=;
}
}
}
for(int i=;i<=k;i++)
scanf("%d%d",&x[i],&y[i]);
for(int i=;i<=k;i++)
if(!vis[x[i]][y[i]])
bfs(i);
for(int i=;i<=k;i++)
for(int j=i+;j<=k;j++)
if(vis[x[i]][y[i]]!=vis[x[j]][y[j]])
ans++;
printf("%d",ans);
}

洛谷 P3663 [USACO17FEB]Why Did the Cow Cross the Road III S的更多相关文章

  1. 洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)

    题目背景 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题目描述 The layout of Farmer ...

  2. 【题解】洛谷P3660 [USACO17FEB]Why Did the Cow Cross the Road III

    题目地址 又是一道奶牛题 从左到右扫描,树状数组维护[左端点出现而右端点未出现]的数字的个数.记录每个数字第一次出现的位置. 若是第二次出现,那么删除第一次的影响. #include <cstd ...

  3. 洛谷 P3662 [USACO17FEB]Why Did the Cow Cross the Road II S

    P3662 [USACO17FEB]Why Did the Cow Cross the Road II S 题目描述 The long road through Farmer John's farm ...

  4. 洛谷 P3659 [USACO17FEB]Why Did the Cow Cross the Road I G

    //神题目(题目一开始就理解错了)... 题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's far ...

  5. 洛谷 P3657 [USACO17FEB]Why Did the Cow Cross the Road II P

    题面 大意:让你把两个n的排列做匹配,连线不想交,而且匹配的数字的差<=4,求最大匹配数 sol:(参考了kczno1的题解)对于第一个排列从左往右枚举,用树状数组维护到达另一个序列第i个数字的 ...

  6. [USACO17FEB]Why Did the Cow Cross the Road III P

    [USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...

  7. [USACO17FEB]Why Did the Cow Cross the Road III S

    题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of ...

  8. P3660 【[USACO17FEB]Why Did the Cow Cross the Road III G】

    题外话:维护区间交集子集的小套路 开两个树状数组,一个维护进入区间,一个维护退出区间 $Query:$ 给定询问区间$l,r$和一些其他区间,求其他区间中与$[l,r]$交集非空的区间个数 用上面维护 ...

  9. [USACO17FEB]Why Did the Cow Cross the Road III P(CDQ分治)

    题意 两列$n$的排列,相同的数连边,如果一对数有交叉且差的绝对值$>k$,则$++ans$,求$ans$ 题解 可以把每一个数字看成一个三元组$(x,y,z)$,其中$x$表示在第一列的位置, ...

随机推荐

  1. python下py2exe打包笔记

    1.下载与python版本一致的py2exe插件包 2.安装py2exe,安装后在python目录下存在:\Lib\site-packages\py2exe\... 3.新建一个python脚本文件, ...

  2. SpringCloud学习笔记(18)----Spring Cloud Netflix之服务网关Zuul原理

    1. Zuul的工作机制 Zuul提供了一个框架,可以对过滤器进行动态的加载,编译,运行.过滤器之间没有直接的相互通信,他们是通过一个RequestContext的静态类来进行数据传递的.Requet ...

  3. 模板层 Template

    每一个 Web 框架都需要一种很便利的方法用于动态生成 HTML 页面. 最常见的做法是使用模板. 模板包含所需 HTML 页面的静态部分,以及一些特殊的模版语法,用于将动态内容插入静态部分. 说白了 ...

  4. [SDOI2008]沙拉公主的困惑 线性筛_欧拉函数_逆元_快速幂

    Code: #include<cstdio> using namespace std; typedef long long ll; const int maxn=10000000+1; l ...

  5. angular.js和vue.js中实现函数去抖(debounce)

    问题描述 搜索输入框中,只当用户停止输入后,才进行后续的操作,比如发起Http请求等. 学过电子电路的同学应该知道按键防抖.原理是一样的:就是说当调用动作n毫秒后,才会执行该动作,若在这n毫秒内又调用 ...

  6. caioj 1161 欧拉函数3:可见点数

    (x, y)被看到仅当x与y互质 由此联想到欧拉函数 x=y是1个点,然后把正方形分成两半,一边是φ(n) 所以答案是2*∑φ(n)+1 #include<cstdio> #include ...

  7. 紫书 习题 8-13 UVa 10570 (枚举+贪心)

    我看到数据范围只有500, 第一反应枚举所有的可能,然后求出每种可能的最小次数. 但是不知道怎么求最小次数.我想的是尽量让一次交换可以让两个不在应该在的位置的数字 到原来应该在的位置的数字, 这样可以 ...

  8. Android端通过HttpURLConnection上传文件到server

    Android端通过HttpURLConnection上传文件到server 一:实现原理 近期在做Androidclient的应用开发,涉及到要把图片上传到后台server中.自己选择了做Sprin ...

  9. 【android】解决Viewpager设置高度为wrap_content无效的方法

    今天发现设置viewpager高度为wrap_content时并没作用.stackoverflow给出了解决方式,就是自己定义viewpager,重写onMesure()方法: public clas ...

  10. 转一篇关于vuex简单理解的文章

    学习vuex半天摸不着头脑无意间发现了这篇文章 对vuex做了一个简单的阐述比较有助于我的理解 现在分享出来希望能给一些朋友一点帮助  这个是原文地址 http://www.ituring.com.c ...