bzoj4395[Usaco2015 dec]Switching on the Lights

题意:

n*n个房间,奶牛初始在(1,1),且只能在亮的房间里活动。每当奶牛经过一个房间,就可以打开这个房间里控制其它房间灯的开关。问奶牛最多可点亮多少个房间。n≤100。

题解:

因为只要一个房间灯亮了,它将一直亮着,所以可以做bfs,每次由队列中的节点扩展可以到的节点。然而这样做不行,因为可能之前尝试过不能到达的房间的灯可以在之后到达的房间里被打开。解决方法是不停做bfs,直到答案不再更新。

代码:

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define maxn 110
using namespace std; inline int read(){
char ch=getchar(); int f=,x=;
while(ch<''||ch>''){if(ch=='-')f=-; ch=getchar();}
while(ch>=''&&ch<='')x=x*+ch-'',ch=getchar();
return f*x;
}
struct nd{int x,y,n;}nds[maxn*]; int g[maxn][maxn];
int n,m,ans; bool lg[maxn][maxn],vis[maxn][maxn]; queue<pair<int,int> >q;
void bfs(int x,int y){
while(!q.empty())q.pop(); memset(vis,,sizeof(vis)); q.push(make_pair(x,y)); vis[x][y]=;
for(int i=g[x][y];i;i=nds[i].n)if(!lg[nds[i].x][nds[i].y])lg[nds[i].x][nds[i].y]=,ans++;
while(!q.empty()){
int nowx=q.front().first,nowy=q.front().second; q.pop();
if(nowx>&&lg[nowx-][nowy]&&!vis[nowx-][nowy]){
q.push(make_pair(nowx-,nowy)); vis[nowx-][nowy]=;
for(int i=g[nowx-][nowy];i;i=nds[i].n)
if(!lg[nds[i].x][nds[i].y])lg[nds[i].x][nds[i].y]=,ans++;
}
if(nowx<n&&lg[nowx+][nowy]&&!vis[nowx+][nowy]){
q.push(make_pair(nowx+,nowy)); vis[nowx+][nowy]=;
for(int i=g[nowx+][nowy];i;i=nds[i].n)
if(!lg[nds[i].x][nds[i].y])lg[nds[i].x][nds[i].y]=,ans++;
}
if(nowy>&&lg[nowx][nowy-]&&!vis[nowx][nowy-]){
q.push(make_pair(nowx,nowy-)); vis[nowx][nowy-]=;
for(int i=g[nowx][nowy-];i;i=nds[i].n)
if(!lg[nds[i].x][nds[i].y])lg[nds[i].x][nds[i].y]=,ans++;
}
if(nowy<n&&lg[nowx][nowy+]&&!vis[nowx][nowy+]){
q.push(make_pair(nowx,nowy+)); vis[nowx][nowy+]=;
for(int i=g[nowx][nowy+];i;i=nds[i].n)
if(!lg[nds[i].x][nds[i].y])lg[nds[i].x][nds[i].y]=,ans++;
}
}
}
int main(){
n=read(); m=read();
inc(i,,m){
int a=read(),b=read(),c=read(),d=read(); nds[i]=(nd){c,d,g[a][b]}; g[a][b]=i;
}
ans=; lg[][]=;
while(){int x=ans; bfs(,); if(ans==x)break;} printf("%d",ans); return ;
}

20160908

bzoj4395[Usaco2015 dec]Switching on the Lights*的更多相关文章

  1. 4395: [Usaco2015 dec]Switching on the Lights

    每次到达一个点,或者点亮一个房间的灯的时候,检查一下它四周的点能否走. 一开始看错题了..要求的是最多能开多少房的灯. #include<cstdio> #include<iostr ...

  2. BZOJ 4390: [Usaco2015 dec]Max Flow

    4390: [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 177  Solved: 113[Submi ...

  3. 【BZOJ4391】[Usaco2015 dec]High Card Low Card(贪心)

    [BZOJ4391][Usaco2015 dec]High Card Low Card(贪心) 题面 BZOJ 题解 预处理前缀后缀的结果,中间找个地方合并就好了. #include<iostr ...

  4. bzoj 4397: [Usaco2015 dec]Breed Counting -- 前缀和

    4397: [Usaco2015 dec]Breed Counting Time Limit: 10 Sec  Memory Limit: 128 MB Description Farmer John ...

  5. [Usaco2015 dec]Max Flow 树上差分

    [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 353  Solved: 236[Submit][Sta ...

  6. 搜索【洛谷P2845】 [USACO15DEC]Switching on the Lights 开关灯

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

  7. 洛谷P2828 Switching on the Lights(开关灯)

    P2828 Switching on the Lights(开关灯) 题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一个N*N的矩形网络.( ...

  8. bzoj4393: [Usaco2015 Dec]Fruit Feast

    题意: T,A,B.T是上限.A和B可以随意吃但是不能超过T.有一次将吃的东西/2的机会.然后可以继续吃,不能超过T.问最多可以吃多少. =>我们先处理不能/2可以吃到哪些.然后弄个双指针扫一扫 ...

  9. BZOJ4390: [Usaco2015 dec]Max Flow

    BZOJ4390: [Usaco2015 dec]Max Flow Description Farmer John has installed a new system of N−1 pipes to ...

随机推荐

  1. TensorFlow从0到1之TensorFlow多层感知机实现MINIST分类(22)

    TensorFlow 支持自动求导,可以使用 TensorFlow 优化器来计算和使用梯度.它使用梯度自动更新用变量定义的张量.本节将使用 TensorFlow 优化器来训练网络. 前面章节中,我们定 ...

  2. 使用matplotlib进行可视化

    转自:https://blog.csdn.net/qq_30614345/article/details/99049790 https://blog.csdn.net/qq_30614345/arti ...

  3. Axis WSDD文件参考文档

    本部分介绍的所有的WSDD元素的名称空间都是"http://xml.apache.org/axis/wsdd/". <deployment> 告诉Axis Engine ...

  4. junit配合catubuter统计单元测试的代码覆盖率

    1.视频参考孔浩老师ant视频笔记 对应的build-junit.xml脚步如下所示: <?xml version="1.0" encoding="UTF-8&qu ...

  5. java android 序列号serializable和parcelable

    why 为什么要了解序列化?—— 进行Android开发的时候,无法将对象的引用传给Activities或者Fragments,我们需要将这些对象放到一个Intent或者Bundle里面,然后再传递. ...

  6. java异常处理之try_catch_finally

    java异常处理之try_catch_finally 看下面的一个列子: public class TestException { int goabl=1; public TestException( ...

  7. 网络虚拟化之linux虚拟网络基础

    1 linux虚拟网络基础 1.1 Device 在linux里面devic(设备)与传统网络概念里的物理设备(如交换机.路由器)不同,Linux所说的设备,其背后指的是一个类似于数据结构.内核模块或 ...

  8. vs code 初始化vue项目框架

    1.首先安装npm组件  下载地址:https://nodejs.org/en/ 安装完 2.配置环境变量 3.验证是否成功 node -v npm -v 4.替换npm 输入npm install ...

  9. Ribbon软负载 (F版)

    Spring Cloud 为开发者提供了在分布式系统中的一些常用的组件(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁定,决策竞选,分布式会话集群状态).使用Sprin ...

  10. 集成Swagger在线调试

    SpringBoot 是为了简化 Spring 应用的创建.运行.调试.部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖 ...