Problem 洛谷P2845-Switching on the Lights 开关灯

Accept: 154    Submit: 499
Time Limit: 1000 mSec    Memory Limit : 128MB

Problem Description

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)的灯的开关。

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

 Input

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

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

 Output

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

 

 Sample Input

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

 Sample Output

5

题目链接:https://www.luogu.org/problemnew/show/P2845

题解:乍一看这个题觉得这个题咋这么水,凉了之后画了个稍微大一点图,意识到用普通的广搜的话,会有一些点会被误判为不能到的点而只是点亮而不进队,这样就很明显有问题了。

解决方案其实挺好想的,把原来的vis数组变成vis和illu数组,前者用来标记到过没有,后者标记点亮没有,新到一个房间,有一些它能打开的灯,对于这些点,判断它的四周有没有之前到过的点,

如果有,那它也能到,入队。当然了,判断当前房间四周有没有开灯是必然的。

 #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
using namespace std; const int maxn = +,maxm = +; bool vis[maxn][maxn];
bool illu[maxn][maxn];
int n,m;
int tot,head[maxn*maxn];
int dir[][] = {{,},{,},{-,},{,-}}; struct Point{
int x,y;
Point(int x = ,int y = ) :
x(x),y(y) {}
}; struct Edge{
int to,next;
Edge(int to = ,int next = ) :
to(to),next(next) {}
}edge[maxm]; void AddEdge(int u,int v){
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}; inline void cal(int v,int &x,int &y){
if(v%n == ){
x = v/n,y = n;
}
else x = v/n+,y = v%n;
} inline bool Judge(int x,int y){
if(<=x && <=y && x<=n && y<=n) return true;
return false;
} void BFS(){
queue<Point> que;
que.push(Point(,));
vis[][] = illu[][] = true;
while(!que.empty()){
Point first = que.front();
que.pop();
int u = (first.x-)*n+first.y;
for(int k = ;k < ;k++){
int xx = first.x+dir[k][],yy = first.y+dir[k][];
if(Judge(xx,yy)){
if(!vis[xx][yy] && illu[xx][yy]){
vis[xx][yy] = true;
que.push(Point(xx,yy));
}
}
}
for(int i = head[u];i != -;i = edge[i].next){
int v = edge[i].to;
int x,y;
cal(v,x,y);
illu[x][y] = true;
if(!vis[x][y]){
for(int k = ;k < ;k++){
int xx = x+dir[k][],yy = y+dir[k][];
if(Judge(xx,yy) && vis[xx][yy]){
vis[x][y] = true;
que.push(Point(x,y));
}
}
}
}
}
} int main()
{
//freopen("input.txt","r",stdin);
memset(vis,false,sizeof(vis));
memset(illu,false,sizeof(illu));
memset(head,-,sizeof(head));
tot = ;
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);
int u = (a-)*n+b,v = (c-)*n+d;
AddEdge(u,v);
}
BFS();
int ans = ;
for(int i = ;i <= n;i++){
for(int j = ;j <= n;j++){
if(illu[i][j]) ans++;
}
}
printf("%d\n",ans);
return ;
}

洛谷P2845-Switching on the Lights 开关灯的更多相关文章

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

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

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

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

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

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

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

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

  5. P2845 [USACO15DEC]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. COCI2017-2018#3 Dojave || 洛谷P4443

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

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

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

  9. 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.

    没有上司的舞会  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...

随机推荐

  1. java连接MySQL数据库的方式

    Java连接数据库的几种方法 *说明 1.以MySQL数据库为例 2.分为四个步骤: 建立数据库连接, 向数据库中提交sql 处理数据库返回的结果 关闭数据库连接 一:JDBC 1.建立数据库连接 只 ...

  2. Hibernate入门(十)inverse

    双向关联产生多余的SQL语句 /** * 添加一个订单到id为6的客户中 */ @Test public void fun1(){ Session session = HibernateUtils.g ...

  3. Java设计模式 - 单例模式详解(下)

    单例模式引发相关整理 关联线程安全 在多线程下,懒汉式会有一定修改.当两个线程在if(null == instance)语句阻塞的时候,可能由两个线程进入创建实例,从而返回了两个对象.对此,我们可以加 ...

  4. 如何创建.gitignore文件,忽略git不必要提交的文件

    touch .gitignore 在项目目录里输入以上名利后,会自动生成一个文件 .gitignore,可在文件里写入忽略的文件名,例如 node_modules coverage .idea npm ...

  5. vue项目编辑修改时批量回显数据

    selectCityServiceOne() { let sendData = { token: this.token, id: this.id } post_ajax('backStage/city ...

  6. Unity3D手机斗地主游戏开发实战(04)_出牌判断大小

    之前我们实现了叫地主.玩家和电脑自动出牌主要功能,但是还有个问题,出牌的时候,没有有效性检查和比较牌力大小.比如说,出牌3,4,5,目前是可以出牌的,然后下家可以出任何牌如3,6,9. 问题1:出牌检 ...

  7. iOS----------SVN问题 the operation could not be completed

    可能是服务器磁盘满了或者你本地的内存满了

  8. Android项目实战(五十一):浅谈GreenDao

    比较出名的数据库框架 GreenDao使用步骤: 1.app目录下的build.gradle文件 添加依赖 compile 'org.greenrobot:greendao:3.2.0' 顶部添加插件 ...

  9. Android为TV端助力 转载:android MVC设计模式

    Controller控制器 import android.app.Dialog; import android.app.ProgressDialog; import android.os.Bundle ...

  10. Ubuntu下解压缩文件

    记录Ubuntu下各种压缩和解压方式: .tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)—————— ...