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 开关灯的更多相关文章

  1. Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯(bfs)

    P2845 [USACO15DEC]Switching on the Lights 开关灯 题意 题目背景 来源:usaco-2015-dec \(Farm\ John\)最近新建了一批巨大的牛棚.这 ...

  2. P2845 [USACO15DEC]Switching on the Lights 开关灯

    题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一个N*N的矩形网络.(1<n<100) 然而bessie十分怕黑,他想计算可以把 ...

  3. 「Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯」

    USACO的又一道搜索题 前置芝士 BFS(DFS)遍历:用来搜索.(因为BFS好写,本文以BFS为准还不是因为作者懒) 链式前向星,本题的数据比较水,所以邻接表也可以写,但是链式前向星它不香吗. 具 ...

  4. 洛谷 P3128 [USACO15DEC]最大流Max Flow-树上差分(点权/点覆盖)(模板题)

    因为徐州现场赛的G是树上差分+组合数学,但是比赛的时候没有写出来(自闭),背锅. 会差分数组但是不会树上差分,然后就学了一下. 看了一些东西之后,对树上差分写一点个人的理解: 首先要知道在树上,两点之 ...

  5. 状压搜索 洛谷T47092 作业

    TYM 有 nn 本作业,编号为 1,\dots,n1,…,n. 由于 \mathrm{TYM}TYM 很喜欢偷懒,而且不喜欢消耗脑细胞,所以他选择跳着完成这 nn 本作业.此外,如果将做作业的顺序转 ...

  6. 洛谷P3128 [USACO15DEC]最大流Max Flow

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...

  7. 洛谷 P3130 [USACO15DEC]计数haybalesCounting Haybales

    P3130 [USACO15DEC]计数haybalesCounting Haybales 题目描述 Farmer John is trying to hire contractors to help ...

  8. 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

  9. 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

随机推荐

  1. 基于OpenCV的火焰检测(二)——RGB颜色判据

    上文跟大家分享了在做火焰检测中常用到的图像预处理方法,从这一篇博文开始,我将向大家介绍如何一步一步地检测出火焰区域.火焰提取要用 到很多判据,今天我要向大家介绍的是最简单的但是很有效的判据--RGB判 ...

  2. c语言-顺序表

    在数据结构中包含两种,一种线性结构(包括顺序表,链表,栈,队列),一种非线性结构(树,图), 顺序表,其实就是在内存动态数组,Java中的ArrayList就是一个典型的顺序表,它在顺序表的基础上增加 ...

  3. 【转载】基于TINY4412的Andorid开发-------简单的LED灯控制

    阅读目录(Content) 一.编写驱动程序 二.编写代码测试驱动程序 三.编写HAL代码 四.编写Framework代码 五.编写JNI代码 六.编写App 参考资料: <Andriod系统源 ...

  4. ListView的ScrollListener

    @Override public void onScrollStateChanged(AbsListView paramAbsListView, int paramInt) { //当屏幕停止滚动时为 ...

  5. Android常用开源库集合【持续更新】

    1.FastJson  阿里巴巴工程师做的一个方便的JSON转换库 2.ButterKnife 只要作用能代替代码中大量的findviewbyid语句的使用,使用@injectview注入方式 3.v ...

  6. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  7. Linux 设置新创建目录或文件的默认权限

    一.简介 在unix或者linux中,每创建一个文件或者目录时,这个文件或者目录都具有一个默认的权限,比如目录755,文件644,这些默认权限是通过"umask"权限掩码控制的.一 ...

  8. python常用uuid模块

    uuid.uuid4(),会根据我们当前的网卡和时间生成的一个随机字符串. 注意:uuid.uuid4()生成的是一个对象,需要强转为字符串. uid = str(uuid.uuid4()) #当前网 ...

  9. Entity Framework Tutorial Basics(18):DBEntityEntry Class

    DBEntityEntry Class DBEntityEntry is an important class, which is useful in retrieving various infor ...

  10. Person.post请求------详细过程

    首先,在PersonRepository的父类中查找save方法,如下: @Override @TransactionalMethod public <S extends D> S sav ...