洛谷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
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 开关灯的更多相关文章
- 洛谷P2828 Switching on the Lights(开关灯)
P2828 Switching on the Lights(开关灯) 题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一个N*N的矩形网络.( ...
- 洛谷 P2828 Switching on the Lights(开关灯)
传送门 题目大意:n*n的网格,每个网格是一个房间 都关着灯,只有(1,1)开着灯,且(x,y)有着(z,k)房间灯的开关. 问从(1,1)开始走最多点开几盏灯. 题解:搜索+骗分. 劳资的骗分天下无 ...
- 搜索【洛谷P2845】 [USACO15DEC]Switching on the Lights 开关灯
P2845 [USACO15DEC]Switching on the Lights 开关灯 题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一 ...
- 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为准还不是因为作者懒) 链式前向星,本题的数据比较水,所以邻接表也可以写,但是链式前向星它不香吗. 具 ...
- COCI2017-2018#3 Dojave || 洛谷P4443
题目传送门............................................................................................... ...
- 洛谷1640 bzoj1854游戏 匈牙利就是又短又快
bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
- 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.
没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...
随机推荐
- js高级:event,事件冒泡,事件捕获
1.事件 浏览器客户端上客户触发的行为都称为事件 所有的事件都是天生自带的,不需要我们去绑定,只需要我们去触发. 通过 obj.事件名=function(){} 事件名:onmouseover 鼠标悬 ...
- TP5.0 PHPExcel 数据表格导出导入(引)
TP5.0 PHPExcel 数据表格导出导入(引) 今天看的是PHPExcel这个扩展库,Comporse 下载不下来,最后只能自己去github里面手动下载,但有一个问题就是下载下来的PHPExc ...
- Spider-two
一.网络数据加密:1. md5 / sha1 不可逆加密算法: 结果是十六进制数, 结果不可逆, 多用于文件验证 import hashlib md5_obj = hashlib.md5() sha1 ...
- element vue validate验证名称重复 输入框与后台重复验证 特殊字符 字符长度 及注意事项
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 吴恩达机器学习笔记 —— 19 应用举例:照片OCR(光学字符识别)
http://www.cnblogs.com/xing901022/p/9374258.html 本章讲述的是一个复杂的机器学习系统,通过它可以看到机器学习的系统是如何组装起来的:另外也说明了一个复杂 ...
- Android 程序崩溃之后fragment出现画面重叠问题
1.解决方法: 直接在包含Fragment的Activity中复写onSaveInstanceState()方法,使其空实现 @Override protected void onSaveInstan ...
- wangEditor更改默认高度
在使用WangEditor时觉得高度太低,默认是300px;想调下高度,借鉴https://blog.csdn.net/qq_31384551/article/details/83240188, 网址 ...
- NoHttp封装--06 NoHttp之队列、队列优先级
public class Main { /** * 程序入口 */ public void start() { // 第一种,先进先出的队列 // YolandaLinkedQueue queue = ...
- eclipse安装中java环境的搭建
转自博客园:amandaj 做了小小改动. 一.java 开发环境的搭建 这里主要说的是在windows 环境下怎么配置环境. 1.首先安装JDK java的sdk简称JDK ,去其官方网站下载最近 ...
- git 入门教程之个性化 git
前情概要 初识 git 时,我们就已经接触过 git 的基本配置,使用 git config 命令配置用户名和邮箱: # 配置当前项目(`local`)的用户名(`snowdreams1006`) g ...