洛谷 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$表示在第一列的位置, ...
随机推荐
- PostgreSQL 数据库性能调优的注意点
PostgreSQL提供了一些性能调优的功能.主要有如下几个方面.1.使用EXPLAIN EXPLAIN命令可以查看执行计划,这个方法是我们最主要的调试工具. 2.及时更新执行计划中使用的统计信息 ...
- 51nod-1134 最长递增子序列,用线段树将N^2的dp降到NlogN
题目链接 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10. Input 第1行 ...
- (六)Redux进阶
1 UI组件与容器组件的拆分 UI组件(傻瓜组件):只负责页面显示,没有任何逻辑 容器组件(聪明组件):并不去管UI到底长成什么样,关注的是整个业务逻辑 2 无状态组件 一个普通的函数就是无状态组件 ...
- axios使用方法
npm install axios 创建文件夹api/index.js import axios from 'axios'; let http = axios.create({ baseURL: '' ...
- 洛谷 P1373 小a和uim之大逃离 (差值型dp总结)
这道题和多米诺骨牌那道题很像 ,都是涉及到差值的问题. 这道题是二维的,同时要取模. 这种题,因为当前的决策有后效性,会影响到差值,所以直接把 差值作为维度,然后计算答案的时候把差值为0的加起来就行了 ...
- 使用vuex的流程随笔
1.在建好的vue项目中新建一个vuex文件夹在此文件夹下建一个index.js文件,在此文件下引入vuex 模块(当然需要先npm下载)和vue模块,在引入你所有的自定义的module.js模块(下 ...
- 最多包含2/k个不同字符的最长串
看这里的解答: http://www.cnblogs.com/grandyang/p/5351347.html 通用解决了2和k的问题.
- (六)storm-kafka源代码走读之PartitionManager
PartitionManager算是storm-kafka的核心类了,如今開始简单分析一下.还是先声明一下,metric部分这里不做分析. PartitionManager主要负责的是消息的发送.容错 ...
- iOS6和iOS7处理push不同之处,解决反复push,-(void) application: didReceiveRemoteNotification: fetchCompletionHandl
如果读者已经知道push的基本知识,本文仅仅是解决一些适配,兼容问题.如果对push 不甚了解,參考以下的文章 1.[iOS push全方位解析](一) push的概述 2.[iOS push全方位解 ...
- 一个使用sbt编译的JNI C++ 的模板
假设你须要在Scala或是Java中调用C或C++函数库,就须要使用JNI. 这里就涉及到编译scala ,java 和C(C++)代码,在这里给出一个程序的框架,我们使用sbt 缺省的代码文件夹 文 ...