P2845 [USACO15DEC]Switching on the Lights 开关灯

题目背景

来源:usaco-2015-dec

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

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

题目描述

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

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

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

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

水题,但是题意理解错了WA了好长时间。

code:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <queue>
  4. using namespace std;
  5. const int wx=117;
  6. inline int read(){
  7. int sum=0,f=1; char ch=getchar();
  8. while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
  9. while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0'; ch=getchar();}
  10. return sum*f;
  11. }
  12. int f[wx][wx],mpx[200017],mpy[200017];
  13. int head[200017],vis[wx][wx],had[wx][wx];
  14. int dx[]={0,1,0,-1,0};
  15. int dy[]={0,0,1,0,-1};
  16. int n,m,num,ans=1,tot;
  17. struct e{
  18. int nxt,to;
  19. }edge[200017];
  20. void add(int from,int to){
  21. edge[++num].nxt=head[from];
  22. edge[num].to=to;
  23. head[from]=num;
  24. }
  25. queue<int > q;
  26. void bfs(){
  27. vis[1][1]=1; had[1][1]=1; q.push(1); q.push(1);
  28. while(q.size()){
  29. int ux=q.front(); q.pop();
  30. int uy=q.front(); q.pop();
  31. for(int i=head[f[ux][uy]];i;i=edge[i].nxt){
  32. int v=edge[i].to;
  33. int ex=mpx[v]; int ey=mpy[v];
  34. if(!had[ex][ey]){
  35. had[ex][ey]=1;ans++;
  36. if(!vis[ex][ey])if(vis[ex-1][ey]||vis[ex+1][ey]||vis[ex][ey-1]||vis[ex][ey+1])vis[ex][ey]=1,q.push(ex),q.push(ey);
  37. }
  38. }
  39. for(int i=1;i<=4;i++){
  40. int ex=ux+dx[i]; int ey=uy+dy[i];
  41. if(ex<1||ey<1||ex>n||ey>n||vis[ex][ey]||!had[ex][ey])continue;
  42. if(vis[ex-1][ey]||vis[ex+1][ey]||vis[ex][ey-1]||vis[ex][ey+1])vis[ex][ey]=1,q.push(ex),q.push(ey);
  43. }
  44. }
  45. printf("%d\n",ans);
  46. }
  47. int main(){
  48. n=read(); m=read();
  49. for(int i=1;i<=n;i++){
  50. for(int j=1;j<=n;j++){
  51. f[i][j]=++tot; mpx[tot]=i; mpy[tot]=j;
  52. }
  53. }
  54. for(int i=1;i<=m;i++){
  55. int x,y,z,c;
  56. x=read(); y=read(); z=read(); c=read();
  57. add(f[x][y],f[z][c]);
  58. }
  59. bfs();
  60. return 0;
  61. }

搜索【洛谷P2845】 [USACO15DEC]Switching on the Lights 开关灯的更多相关文章

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

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

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

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

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

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

  4. 洛谷 P3128 [USACO15DEC]最大流Max Flow-树上差分(点权/点覆盖)(模板题)

    因为徐州现场赛的G是树上差分+组合数学,但是比赛的时候没有写出来(自闭),背锅. 会差分数组但是不会树上差分,然后就学了一下. 看了一些东西之后,对树上差分写一点个人的理解: 首先要知道在树上,两点之 ...

  5. 状压搜索 洛谷T47092 作业

    TYM 有 nn 本作业,编号为 1,\dots,n1,…,n. 由于 \mathrm{TYM}TYM 很喜欢偷懒,而且不喜欢消耗脑细胞,所以他选择跳着完成这 nn 本作业.此外,如果将做作业的顺序转 ...

  6. 洛谷P3128 [USACO15DEC]最大流Max Flow

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...

  7. 洛谷 P3130 [USACO15DEC]计数haybalesCounting Haybales

    P3130 [USACO15DEC]计数haybalesCounting Haybales 题目描述 Farmer John is trying to hire contractors to help ...

  8. 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

  9. 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

随机推荐

  1. #define和const的区别

    下面使用#define和const定义常量: #define n_define 10 int main(int argc, char* argv[],int _version) { ; int *p= ...

  2. 人脑和CPU

    人类的数学运算没有计算机快是因为神经信号速度没有电信号快吗,电信号是光速吧. 不过人类的cpu大脑和存储硬盘和内存超过目前计算机n条街,虽然传输速度慢,但是传输量也是大的,其实计算机就是根据人脑设计的 ...

  3. 在.net中使用redis(StackExchange.Redis)

    本文介绍如何在.net中使用redis 安装 代码使用 StackExchange.Redis基础使用 StackExchange.Redis中的事务 安装(windows平台) 安装Chocolat ...

  4. sql中IN的用法

    1.和where配合使用 IN操作符允许我们在where的子句中规定多个值 SELECT * FROM Persons  WHERE LastName IN ('Adams','Carter') 这句 ...

  5. 基本的数据类型 void关键字 都存在类类型

  6. SQL基础E-R图基础

    ER图分为实体.属性.关系三个核心部分.实体是长方形体现,而属性则是椭圆形,关系为菱形. ER图的实体(entity)即数据模型中的数据对象,例如人.学生.音乐都可以作为一个数据对象,用长方体来表示, ...

  7. go语言linux环境配置

    linux的设置方法:有4个环境变量需要设置:GOROOT.GOPATH.GOBIN以及PATH.需要设置到某一个profile文件中(~/.bash_profile(单一用户)或/etc/profi ...

  8. R: 对向量中的每个元素,检查其是否包含某个“单词”

    #检测一个字符串中,是否包含某个子串,是返回T,否返回Frequire(stringr) require(stringr) test <- c("这里有天气热敏感冒",&qu ...

  9. python3-file文件操作

    # Auther: Aaron Fan '''打开文件的模式有三种:r,只读模式(默认).w,只写模式.[不可读:不存在则创建:存在则删除内容:因为会清空原有文件的内容,一定要慎用]a,追加模式.[可 ...

  10. Entity Relationships

    Entity Relationships: Here, you will learn how entity framework manages the relationships between en ...