P2828 Switching on the Lights(开关灯)

题目背景

来源:usaco-2015-dec

Farm John 最近新建了一批巨大的牛棚。这些牛棚构成了一个N*N的矩形网络。(1<n<100)

然而bessie十分怕黑,他想计算可以把多少个牛棚的灯打开。

题目描述

有N*N个房间,组成了一张N*N的网格图,Bessie一开始位于左上角(1,1),并且只能上下左右行走。

一开始,只有(1,1)这个房间的灯是亮着的,Bessie只能在亮着灯的房间里活动。

有另外M条信息,每条信息包含四个数a,b,c,d,表示房间(a,b)里有房间(c,d)的灯的开关。

请计算出最多有多少个房间的灯可以被打开

输入输出格式

输入格式:

第一行,两个数:N,M(1<m<200000);

第2-m+1行:坐标(x1,y1),(x2,y2)代表房间的坐标(x1,y1)及可以点亮的·房间的坐标(x2,y2);

输出格式:

一个数,最多可以点亮的房间数

输入输出样例

输入样例#1: 复制

3 6
1 1 1 2
2 1 2 2
1 1 1 3
2 3 3 1
1 3 1 2
1 3 2 1
输出样例#1: 复制

5

说明

这里,如果你看得懂英文的话,这里有样例的说明。

Here, Bessie can use the switch in (1,1)to turn on lights in (1,2)and (1,3). She can then walk to (1,3)and turn on the lights in (2,1),from which she can turn on the lights in (2,2). The switch in (2,3)is inaccessible to her, being in an unlit room. She can therefore illuminate at most 5 rooms.

/*
一个特别需要注意的细节:一个房间可以走多次!比较蠢的,我就另外写了一个宽搜来check这种情况
*/
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#define maxn 110
using namespace std;
int n,m,e[][]={{,},{,-},{-,},{,}};
bool vis[maxn][maxn],bri[maxn][maxn],hav[maxn][maxn],v[maxn][maxn];
struct node{
int x,y;
}cur,nxt,map[maxn][maxn][];
node make_node(int x,int y){
node res;res.x=x;res.y=y;
return res;
}
int main(){
freopen("Cola.txt","r",stdin);
scanf("%d%d",&n,&m);
int a,b,c,d;
for(int i=;i<=m;i++){
scanf("%d%d%d%d",&a,&b,&c,&d);
map[a][b][++map[a][b][].x].x=c;map[a][b][map[a][b][].x].y=d;
hav[a][b]=;
}
queue<node>q;
cur=make_node(,);vis[][]=;bri[][]=;
q.push(cur);
while(!q.empty()){
while(!q.empty()){
cur=q.front();q.pop();
if(hav[cur.x][cur.y]){
for(int i=;i<=map[cur.x][cur.y][].x;i++){
int tx=map[cur.x][cur.y][i].x,ty=map[cur.x][cur.y][i].y;
if(bri[tx][ty])continue;
bri[tx][ty]=;
}
}
for(int i=;i<;i++){
int xx=cur.x+e[i][],yy=cur.y+e[i][];
if(xx<||xx>n||yy<||yy>n)continue;
if(vis[xx][yy]||bri[xx][yy]==)continue;
q.push(make_node(xx,yy));
vis[xx][yy]=;
}
}
queue<node>que;
while(!que.empty())que.pop();
memset(v,,sizeof(v));
que.push(make_node(,));v[][]=;
while(!que.empty()){
cur=que.front();que.pop();
for(int i=;i<;i++){
int xx=e[i][]+cur.x,yy=e[i][]+cur.y;
if(xx>n||xx<||yy>n||yy<)continue;
if(!v[xx][yy]&&bri[xx][yy]){
v[xx][yy]=;
que.push(make_node(xx,yy));
if(!vis[xx][yy])
q.push(make_node(xx,yy)),vis[xx][yy]=;
}
}
}
}
int cnt=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(bri[i][j])cnt++;
printf("%d",cnt);
}

洛谷P2828 Switching on the Lights(开关灯)的更多相关文章

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

    传送门 题目大意:n*n的网格,每个网格是一个房间 都关着灯,只有(1,1)开着灯,且(x,y)有着(z,k)房间灯的开关. 问从(1,1)开始走最多点开几盏灯. 题解:搜索+骗分. 劳资的骗分天下无 ...

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

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

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

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

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

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

  5. luogu P2828 Switching on the Lights(开关灯)

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

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

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

  7. 洛谷P2845-Switching on the Lights 开关灯

    Problem 洛谷P2845-Switching on the Lights 开关灯 Accept: 154    Submit: 499Time Limit: 1000 mSec    Memor ...

  8. COCI2017-2018#3 Dojave || 洛谷P4443

    题目传送门............................................................................................... ...

  9. 洛谷1640 bzoj1854游戏 匈牙利就是又短又快

    bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...

随机推荐

  1. C++(八)— 死锁原因及解决方法

    1.死锁原因 死锁问题被认为是线程/进程间切换消耗系统性能的一种极端情况.在死锁时,线程/进程间相互等待资源,而又不释放自身的资源,导致无穷无尽的等待,其结果是任务永远无法执行完成. 打个比方,假设有 ...

  2. Python基础-内置函数总结

    内置函数 int('123') float() string() tuple() set() dict(name='zdd',age=18) type()#查看类型 len()#看长度,其实是元素的个 ...

  3. codeforces 612D The Union of k-Segments (线段排序)

    D. The Union of k-Segments time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  4. stl_heap.h

    stl_heap.h // Filename: stl_heap.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: http://b ...

  5. 3.1 第一个场景 HelloWorldScene

    HelloWorldScene.h #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos ...

  6. HDU3579Hello Kiki(中国剩余定理)(不互质的情况)

    One day I was shopping in the supermarket. There was a cashier counting coins seriously when a littl ...

  7. 迁移学习-微调(fine-tune)的注意事项:

    选取微调形式的两个重要因素:新数据集的大小(size)和相似性(与预训练的数据集相比).牢记卷积网络在提取特征时,前面的层所提取的更具一般性,后面的层更加具体,更倾向于原始的数据集(more orig ...

  8. java blob 文件上传下载

    1.文件上传 pojo中为byte[] 类型,数据库中对应为blob类型. 主要代码: FileInputStream fis = null; fis = new FileInputStream(ne ...

  9. 利用SharedRegion实现核间共享

    导入SharedRegion模块 SharedRegion模块是一个共享区域,特别是对于多处理器环境下,SharedRegion模块就是用于让一个内存区域能被不同处理器共享并操作.这个模块会给每个处理 ...

  10. 类方法,实例方法,静态方法,@property的应用

    class test(object): h = 'hello' w = 'world' def demo(self): print("demo") def test_class(s ...