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数组,前者用来标记到过没有,后者标记点亮没有,新到一个房间,有一些它能打开的灯,对于这些点,判断它的四周有没有之前到过的点,

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

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <queue>
  6. using namespace std;
  7.  
  8. const int maxn = +,maxm = +;
  9.  
  10. bool vis[maxn][maxn];
  11. bool illu[maxn][maxn];
  12. int n,m;
  13. int tot,head[maxn*maxn];
  14. int dir[][] = {{,},{,},{-,},{,-}};
  15.  
  16. struct Point{
  17. int x,y;
  18. Point(int x = ,int y = ) :
  19. x(x),y(y) {}
  20. };
  21.  
  22. struct Edge{
  23. int to,next;
  24. Edge(int to = ,int next = ) :
  25. to(to),next(next) {}
  26. }edge[maxm];
  27.  
  28. void AddEdge(int u,int v){
  29. edge[tot].to = v;
  30. edge[tot].next = head[u];
  31. head[u] = tot++;
  32. };
  33.  
  34. inline void cal(int v,int &x,int &y){
  35. if(v%n == ){
  36. x = v/n,y = n;
  37. }
  38. else x = v/n+,y = v%n;
  39. }
  40.  
  41. inline bool Judge(int x,int y){
  42. if(<=x && <=y && x<=n && y<=n) return true;
  43. return false;
  44. }
  45.  
  46. void BFS(){
  47. queue<Point> que;
  48. que.push(Point(,));
  49. vis[][] = illu[][] = true;
  50. while(!que.empty()){
  51. Point first = que.front();
  52. que.pop();
  53. int u = (first.x-)*n+first.y;
  54. for(int k = ;k < ;k++){
  55. int xx = first.x+dir[k][],yy = first.y+dir[k][];
  56. if(Judge(xx,yy)){
  57. if(!vis[xx][yy] && illu[xx][yy]){
  58. vis[xx][yy] = true;
  59. que.push(Point(xx,yy));
  60. }
  61. }
  62. }
  63. for(int i = head[u];i != -;i = edge[i].next){
  64. int v = edge[i].to;
  65. int x,y;
  66. cal(v,x,y);
  67. illu[x][y] = true;
  68. if(!vis[x][y]){
  69. for(int k = ;k < ;k++){
  70. int xx = x+dir[k][],yy = y+dir[k][];
  71. if(Judge(xx,yy) && vis[xx][yy]){
  72. vis[x][y] = true;
  73. que.push(Point(x,y));
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80.  
  81. int main()
  82. {
  83. //freopen("input.txt","r",stdin);
  84. memset(vis,false,sizeof(vis));
  85. memset(illu,false,sizeof(illu));
  86. memset(head,-,sizeof(head));
  87. tot = ;
  88. scanf("%d%d",&n,&m);
  89. int a,b,c,d;
  90. for(int i = ;i <= m;i++){
  91. scanf("%d%d%d%d",&a,&b,&c,&d);
  92. int u = (a-)*n+b,v = (c-)*n+d;
  93. AddEdge(u,v);
  94. }
  95. BFS();
  96. int ans = ;
  97. for(int i = ;i <= n;i++){
  98. for(int j = ;j <= n;j++){
  99. if(illu[i][j]) ans++;
  100. }
  101. }
  102. printf("%d\n",ans);
  103. return ;
  104. }

洛谷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反射知识相关的文章

    整理的反射相关的文章: (1).通俗理解反射(知乎):学习java应该如何理解反射? (2).关于反射比较深入的博文地址:深入解析Java反射(1) - 基础 贴出我反射调用代码:(craw,dept ...

  2. epoll代码示例

    #include <errno.h> #include <string.h> #include <stdlib.h> #include <sys/types. ...

  3. 12 Linux Which Command, Whatis Command, Whereis Command Examples

    This Linux tutorial will explain the three "W" commands. The three "W"s are what ...

  4. eclipse安装阿里编码规约插件

    点击帮助,Install New Software... 地址为https://p3c.alibaba.com/plugin/eclipse/update 然后选择安装, 一路next即可

  5. CSS table-layout 属性

    设置表格布局算法: table { table-layout:fixed; } 所有浏览器都支持 table-layout 属性. 定义 tableLayout 属性用来显示表格单元格.行.列的算法规 ...

  6. GA中的术语及经常分析的指标

    GA中的术语 跳出客流:只浏览了网站的一个页面,并且没有进一步动作的访客目标转化:通常缩写为目标或转化,这是网站上面的一个预期或动作,通常被认为比标准网页更有价值,例如:"确认购买" ...

  7. linux学习笔记-wget相关知识

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! wget是非交互式的网络文件下载工具,这里参考帮助文档,记录下实用参数和使用方法. 一.wget的实用参数: wget: 用法: ...

  8. finally知识讲解

    finally语句一定会执行吗,很多人认为一定会,其实未必,只有与 finally 相对应的 try 语句块得到执行的情况下,finally 语句块才会执行.假如在try语句之前执行了return操作 ...

  9. Python 捕捉traceback异常栈信息

    捕捉traceback异常栈信息   by:授客 QQ:1033553122 相关函数简介 sys.exc_info() 返回包含3个元素(type, value, traceback)的元组,提供关 ...

  10. Android为TV端助力 修改videoview的宽度和高度

    如果直接用android的videoview.他是不允许你随意的修改宽度和高度的,所以我们要重写videoview! package com.hysmarthotel.view; import and ...