洛谷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.他们有 ...
随机推荐
- java反射知识相关的文章
整理的反射相关的文章: (1).通俗理解反射(知乎):学习java应该如何理解反射? (2).关于反射比较深入的博文地址:深入解析Java反射(1) - 基础 贴出我反射调用代码:(craw,dept ...
- epoll代码示例
#include <errno.h> #include <string.h> #include <stdlib.h> #include <sys/types. ...
- 12 Linux Which Command, Whatis Command, Whereis Command Examples
This Linux tutorial will explain the three "W" commands. The three "W"s are what ...
- eclipse安装阿里编码规约插件
点击帮助,Install New Software... 地址为https://p3c.alibaba.com/plugin/eclipse/update 然后选择安装, 一路next即可
- CSS table-layout 属性
设置表格布局算法: table { table-layout:fixed; } 所有浏览器都支持 table-layout 属性. 定义 tableLayout 属性用来显示表格单元格.行.列的算法规 ...
- GA中的术语及经常分析的指标
GA中的术语 跳出客流:只浏览了网站的一个页面,并且没有进一步动作的访客目标转化:通常缩写为目标或转化,这是网站上面的一个预期或动作,通常被认为比标准网页更有价值,例如:"确认购买" ...
- linux学习笔记-wget相关知识
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! wget是非交互式的网络文件下载工具,这里参考帮助文档,记录下实用参数和使用方法. 一.wget的实用参数: wget: 用法: ...
- finally知识讲解
finally语句一定会执行吗,很多人认为一定会,其实未必,只有与 finally 相对应的 try 语句块得到执行的情况下,finally 语句块才会执行.假如在try语句之前执行了return操作 ...
- Python 捕捉traceback异常栈信息
捕捉traceback异常栈信息 by:授客 QQ:1033553122 相关函数简介 sys.exc_info() 返回包含3个元素(type, value, traceback)的元组,提供关 ...
- Android为TV端助力 修改videoview的宽度和高度
如果直接用android的videoview.他是不允许你随意的修改宽度和高度的,所以我们要重写videoview! package com.hysmarthotel.view; import and ...