题目描述

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. 手工清理win7系统C盘的技巧

    在我们日常使用电脑的过程中,随着使用的时候越久,大家就会发现电脑的运行速度变的越慢了,大家都知道很多系统东西一般都会安装在C盘,系统在运行的时候就会不断的产生垃圾文件以及其他我们根本用不到的文件,这样 ...

  2. 【参考】IBM sun.io.MalformedInputException and text encoding conversions transforms numerals to their word equivalents - United States

    Problem(Abstract) When converting contents from a file or string using WebSphere Application Server, ...

  3. 微信小程序------开发测试

    一.注册小程序 注:微信小程序注册的邮箱不能被其他微信公众平台注册,未被微信开放平台注册,未被给人微信号绑定的微信号. 二.注册完小程序后,下载开发者工具 开发者工具的使用: 1.打开开发者工具:用已 ...

  4. ARC下dealloc过程及.cxx_destruct的探究

    我是前言 这次探索源自于自己一直以来对ARC的一个疑问,在MRC时代,经常写下面的代码: - (void)dealloc { self.array = nil; self.string = nil; ...

  5. gcd的queue与group

    queue相当于事件处理机制里的事件池:只是任务池: 线程作为事件处理的实施者,由线程池从任务池中获取任务进行调度派发: group相当与工作组,按照任务的相关性对任务进行组织.

  6. 优动漫PAINT-简单的树、叶教学

    如题,简单.好用:其实说的还是一个观察的事.看你是否足够细心,对于树叶的生长.枝桠和树干的关系是否了解咯. 对于这样的树枝丫和叶子完全可以使用优动漫PAINT完成,简单又快捷,软件下载:www.don ...

  7. FCC高级编程之Inventory Update

    Inventory Update Compare and update the inventory stored in a 2D array against a second 2D array of ...

  8. 《virtual san 最佳实践》节选 Virtual SAN的发展与现状

    Virtual SAN的发展与现状Virtual SAN已经迭代更新到第四代,即Virtual SAN 6.2.通过三次主版本迭代,Virtual SAN已经成为一款非常成熟的软件定义存储软件.在此, ...

  9. __weak修饰符

    前言 在 HAL 库中,很多回调函数前面使用__weak 修饰符. weak 顾名思义是“弱”的意思,所以如果函数名称前面加上__weak 修饰符,我们一般称这个函数为“弱函数”. 加上了__weak ...

  10. UVA 11248 Frequency Hopping

    Frequency Hopping Time Limit: 10000ms Memory Limit: 131072KB This problem will be judged on UVA. Ori ...