题目描述

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. http_build_query 字符串拼接

    http_build_query 字符串拼接 产生一个urlencode之后的请求字符串. 1.将数组转化成url问号(?)后的字符串 <?php $date=array( 'name'=> ...

  2. c#获取DataTable某一列不重复的值,或者获取某一列的所有值

    实现该功能是用了DataView的筛选功能,DataView表示用于排序.筛选.搜索.编辑和导航的 DataTable 的可绑定数据的自定义视图. 这里做了一个简单易懂的Demo来讲述该方法. 1.建 ...

  3. 获取mapper

    static UpdateLogMapper updateLogMapper = (UpdateLogMapper)SpringContextUtil.getBean(UpdateLogMapper. ...

  4. linux 下的小知识

    Linux中有7种启动级别 运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆运行级别2:多用户状态(没有NFS ...

  5. pip 出错

    pip 升级到10以上出错 ImportError: cannot import name 'main' 解决方法一: 降低pip的版本号 python -m pip install pip==9.0 ...

  6. pandas 7 合并 merge 水平合并,数据会变宽

    pd.merge( df1, df2, on=['key1', 'key2'], left_index=True, right_index=True, how=['left', 'right', 'o ...

  7. OpenJDK源码研究笔记(四)-编写和组织可复用的工具类和方法

    本篇主要讲解java.util.Arrays这个针对数组的工具类. 1.可复用的工具类和方法.  这个工具类里,包含很多针对数组的工具方法,如 排序.交换.二分查找.比较.填充.复制.hashcode ...

  8. 【codeforces 367C】Sereja and the Arrangement of Numbers

    [题目链接]:http://codeforces.com/problemset/problem/367/C [题意] 我们称一个数列a[N]美丽; 当且仅当,数列中出现的每一对数字都有相邻的. 给你n ...

  9. 养活一款APP要“烧”多少钱?

    Duang!又一款APP刷爆朋友圈.大片范儿的电影截图.意味深长的经典对白均出自一款名为“足记”的APP. 足记团队刚于去年8月完成天使期融资,投资方是光速创投和紫辉创投,目前正准备A轮融资.且近一周 ...

  10. nginx php No input file specified 怎样处理?

    配置nginx支持php 出现了No input file specified ? 仅仅要改动下安装文件夹下的  nginx.conf下的 location ~ \.php$ {           ...