搜索【洛谷P2845】 [USACO15DEC]Switching on the Lights 开关灯
P2845 [USACO15DEC]Switching on the Lights 开关灯
题目背景
来源:usaco-2015-dec
Farm John 最近新建了一批巨大的牛棚。这些牛棚构成了一个N*N的矩形网络。(1<n<100)
然而bessie十分怕黑,他想计算可以把多少个牛棚的灯打开。
题目描述
有NN个房间,组成了一张NN的网格图,Bessie一开始位于左上角(1,1),并且只能上下左右行走。
一开始,只有(1,1)这个房间的灯是亮着的,Bessie只能在亮着灯的房间里活动。
有另外M条信息,每条信息包含四个数a,b,c,d,表示房间(a,b)里有房间(c,d)的灯的开关。
请计算出最多有多少个房间的灯可以被打开
水题,但是题意理解错了WA了好长时间。
code:
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
const int wx=117;
inline int read(){
int sum=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0'; ch=getchar();}
return sum*f;
}
int f[wx][wx],mpx[200017],mpy[200017];
int head[200017],vis[wx][wx],had[wx][wx];
int dx[]={0,1,0,-1,0};
int dy[]={0,0,1,0,-1};
int n,m,num,ans=1,tot;
struct e{
int nxt,to;
}edge[200017];
void add(int from,int to){
edge[++num].nxt=head[from];
edge[num].to=to;
head[from]=num;
}
queue<int > q;
void bfs(){
vis[1][1]=1; had[1][1]=1; q.push(1); q.push(1);
while(q.size()){
int ux=q.front(); q.pop();
int uy=q.front(); q.pop();
for(int i=head[f[ux][uy]];i;i=edge[i].nxt){
int v=edge[i].to;
int ex=mpx[v]; int ey=mpy[v];
if(!had[ex][ey]){
had[ex][ey]=1;ans++;
if(!vis[ex][ey])if(vis[ex-1][ey]||vis[ex+1][ey]||vis[ex][ey-1]||vis[ex][ey+1])vis[ex][ey]=1,q.push(ex),q.push(ey);
}
}
for(int i=1;i<=4;i++){
int ex=ux+dx[i]; int ey=uy+dy[i];
if(ex<1||ey<1||ex>n||ey>n||vis[ex][ey]||!had[ex][ey])continue;
if(vis[ex-1][ey]||vis[ex+1][ey]||vis[ex][ey-1]||vis[ex][ey+1])vis[ex][ey]=1,q.push(ex),q.push(ey);
}
}
printf("%d\n",ans);
}
int main(){
n=read(); m=read();
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
f[i][j]=++tot; mpx[tot]=i; mpy[tot]=j;
}
}
for(int i=1;i<=m;i++){
int x,y,z,c;
x=read(); y=read(); z=read(); c=read();
add(f[x][y],f[z][c]);
}
bfs();
return 0;
}
搜索【洛谷P2845】 [USACO15DEC]Switching on the Lights 开关灯的更多相关文章
- Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯(bfs)
P2845 [USACO15DEC]Switching on the Lights 开关灯 题意 题目背景 来源:usaco-2015-dec \(Farm\ John\)最近新建了一批巨大的牛棚.这 ...
- P2845 [USACO15DEC]Switching on the Lights 开关灯
题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一个N*N的矩形网络.(1<n<100) 然而bessie十分怕黑,他想计算可以把 ...
- 「Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯」
USACO的又一道搜索题 前置芝士 BFS(DFS)遍历:用来搜索.(因为BFS好写,本文以BFS为准还不是因为作者懒) 链式前向星,本题的数据比较水,所以邻接表也可以写,但是链式前向星它不香吗. 具 ...
- 洛谷 P3128 [USACO15DEC]最大流Max Flow-树上差分(点权/点覆盖)(模板题)
因为徐州现场赛的G是树上差分+组合数学,但是比赛的时候没有写出来(自闭),背锅. 会差分数组但是不会树上差分,然后就学了一下. 看了一些东西之后,对树上差分写一点个人的理解: 首先要知道在树上,两点之 ...
- 状压搜索 洛谷T47092 作业
TYM 有 nn 本作业,编号为 1,\dots,n1,…,n. 由于 \mathrm{TYM}TYM 很喜欢偷懒,而且不喜欢消耗脑细胞,所以他选择跳着完成这 nn 本作业.此外,如果将做作业的顺序转 ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...
- 洛谷 P3130 [USACO15DEC]计数haybalesCounting Haybales
P3130 [USACO15DEC]计数haybalesCounting Haybales 题目描述 Farmer John is trying to hire contractors to help ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
随机推荐
- #define和const的区别
下面使用#define和const定义常量: #define n_define 10 int main(int argc, char* argv[],int _version) { ; int *p= ...
- 人脑和CPU
人类的数学运算没有计算机快是因为神经信号速度没有电信号快吗,电信号是光速吧. 不过人类的cpu大脑和存储硬盘和内存超过目前计算机n条街,虽然传输速度慢,但是传输量也是大的,其实计算机就是根据人脑设计的 ...
- 在.net中使用redis(StackExchange.Redis)
本文介绍如何在.net中使用redis 安装 代码使用 StackExchange.Redis基础使用 StackExchange.Redis中的事务 安装(windows平台) 安装Chocolat ...
- sql中IN的用法
1.和where配合使用 IN操作符允许我们在where的子句中规定多个值 SELECT * FROM Persons WHERE LastName IN ('Adams','Carter') 这句 ...
- 基本的数据类型 void关键字 都存在类类型
- SQL基础E-R图基础
ER图分为实体.属性.关系三个核心部分.实体是长方形体现,而属性则是椭圆形,关系为菱形. ER图的实体(entity)即数据模型中的数据对象,例如人.学生.音乐都可以作为一个数据对象,用长方体来表示, ...
- go语言linux环境配置
linux的设置方法:有4个环境变量需要设置:GOROOT.GOPATH.GOBIN以及PATH.需要设置到某一个profile文件中(~/.bash_profile(单一用户)或/etc/profi ...
- R: 对向量中的每个元素,检查其是否包含某个“单词”
#检测一个字符串中,是否包含某个子串,是返回T,否返回Frequire(stringr) require(stringr) test <- c("这里有天气热敏感冒",&qu ...
- python3-file文件操作
# Auther: Aaron Fan '''打开文件的模式有三种:r,只读模式(默认).w,只写模式.[不可读:不存在则创建:存在则删除内容:因为会清空原有文件的内容,一定要慎用]a,追加模式.[可 ...
- Entity Relationships
Entity Relationships: Here, you will learn how entity framework manages the relationships between en ...