洛谷 P3663 [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 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≤N2) 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≤N2),每个位于不同的区域。 定义一对牛是“遥远的”,是指让一头牛访问另一头牛时,必须至少穿过一条路。 请帮助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.
输出遥远的牛数量对。
输入输出样例
3 3 3
2 2 2 3
3 3 3 2
3 3 2 3
3 3
2 2
2 3
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的更多相关文章
- 洛谷 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 ...
- 【题解】洛谷P3660 [USACO17FEB]Why Did the Cow Cross the Road III
题目地址 又是一道奶牛题 从左到右扫描,树状数组维护[左端点出现而右端点未出现]的数字的个数.记录每个数字第一次出现的位置. 若是第二次出现,那么删除第一次的影响. #include <cstd ...
- 洛谷 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 ...
- 洛谷 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 ...
- 洛谷 P3657 [USACO17FEB]Why Did the Cow Cross the Road II P
题面 大意:让你把两个n的排列做匹配,连线不想交,而且匹配的数字的差<=4,求最大匹配数 sol:(参考了kczno1的题解)对于第一个排列从左往右枚举,用树状数组维护到达另一个序列第i个数字的 ...
- [USACO17FEB]Why Did the Cow Cross the Road III P
[USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...
- [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 ...
- P3660 【[USACO17FEB]Why Did the Cow Cross the Road III G】
题外话:维护区间交集子集的小套路 开两个树状数组,一个维护进入区间,一个维护退出区间 $Query:$ 给定询问区间$l,r$和一些其他区间,求其他区间中与$[l,r]$交集非空的区间个数 用上面维护 ...
- [USACO17FEB]Why Did the Cow Cross the Road III P(CDQ分治)
题意 两列$n$的排列,相同的数连边,如果一对数有交叉且差的绝对值$>k$,则$++ans$,求$ans$ 题解 可以把每一个数字看成一个三元组$(x,y,z)$,其中$x$表示在第一列的位置, ...
随机推荐
- 洛谷P1231 教辅的组成 网络流
Code: #include<cstdio> #include<cstring> #include<algorithm> #include<vector> ...
- NodeJS学习笔记 (2)文件系统操作-fs(ok)
原文:https://github.com/chyingp/nodejs-learning-guide/blob/master/%E6%A8%A1%E5%9D%97/fs.md#%E9%80%9A%E ...
- mven系列问题
1.前言 Maven,发音是[`meivin],"专家"的意思.它是一个很好的项目管理工具,很早就进入了我的必备工具行列,但是这次为了把 project1项目完全迁移并应用mave ...
- 安装 glusterfs yum源报错
yum install glusterfs-server yum 一直报错 把/etc/yum.repos.d 备份 删除了所有文件,从测试机192..168.59.128上同步过来 一直报错 已加载 ...
- Linux学习之计算机基础理论
一.描述计算机的组成及其功能. 计算机系统是由硬件系统(hardware)和软件系统(software system)两部分组成. 硬件系统: 从硬件基本结构上来讲,计算机是由运算器.控制器.存储器. ...
- SpringBoot实战(四)获取接口请求中的参数(@PathVariable,@RequestParam,@RequestBody)
上一篇SpringBoot实战(二)Restful风格API接口中写了一个控制器,获取了前端请求的参数,现在我们就参数的获取与校验做一个介绍: 一:获取参数 SpringBoot提供的获取参数注解包括 ...
- 2015 Multi-University Training Contest 1 Tricks Device
Tricks Device Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- 兼顾php和jquery新版本的checkbox的写法
<input type="checkbox" class="scate_item" name="scategories[]" valu ...
- 在PyCharm中以root权限运行和调试python代码
python中有的代码可能需要su权限,如 import os os.mkdir('/media/xxx/disk_a') 如果在交互式环境中使用,需要以sudo的方式启动python.而在PyCha ...
- STL源代码学习(vector篇)
#include <concept_checks.h> #include<stl_allocate.h> /// The vector base class's constru ...