题目背景

来源:usaco-2015-dec

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

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

输入输出格式

输入格式:

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

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

输出格式:

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

输入输出样例

输入样例#1:

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
输出样例#1:

5

说明

这里,如果你看得懂英文的话,这里有样例的说明。

Here, Bessie can use the switch in (1,1)to turn on lights in (1,2)and (1,3). She can then walk to (1,3)and turn on the lights in (2,1),from which she can turn on the lights in (2,2). The switch in (2,3)is inaccessible to her, being in an unlit room. She can therefore illuminate at most 5 rooms.

Solution:

  广搜水题。

  随便照思路模拟一下打个广搜就好了,然后注意一下入队的细节,问题是我死活交都是$41$老WA$6$个点,重写bfs好几遍死活WA,后面删掉代码重写才意识到数组开小了,WTF报错是WA没显示RE,第一份代码开大数组就A了,diss洛谷评测机啊!

代码:

#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=,M=,dx[]={,-,,},dy[]={,,,-};
int n,m,ans,num[N][N],tot;
int to[M],net[M],h[M],cnt;
bool vis[N][N],ct[N][N];
struct node{
int x,y;
node(int a=,int b=){x=a,y=b;}
}mp[M];
queue<node>q; il int gi(){
int a=;char x=getchar();
while(x<''||x>'')x=getchar();
while(x>=''&&x<='')a=(a<<)+(a<<)+x-,x=getchar();
return a;
} il int ID(int x,int y){return (x-)*n+y;} il void add(int u,int v){to[++cnt]=v,net[cnt]=h[u],h[u]=cnt;} il void bfs(){
ans=,vis[][]=,ct[][]=;
q.push(node(,));
while(!q.empty()){
node a=q.front();q.pop();
for(int i=h[num[a.x][a.y]];i;i=net[i]){
int x=mp[to[i]].x,y=mp[to[i]].y;
if(!ct[x][y]){
ct[x][y]=,ans++;
if(!vis[x][y]){
if(vis[x-][y]||vis[x+][y]||vis[x][y-]||vis[x][y+]) vis[x][y]=,q.push(node(x,y));
}
}
}
For(i,,) {
int x=dx[i]+a.x,y=dy[i]+a.y;
if(x<||y<||x>n||y>n)continue;
if(ct[x][y]&&!vis[x][y]){
if(vis[x-][y]||vis[x+][y]||vis[x][y-]||vis[x][y+]) vis[x][y]=,q.push(node(x,y));
}
}
}
} int main(){
n=gi(),m=gi();
For(i,,n) For(j,,n) num[i][j]=++tot,mp[tot].x=i,mp[tot].y=j;
int a,b,c,d;
while(m--){
a=gi(),b=gi(),c=gi(),d=gi();
add(num[a][b],num[c][d]);
}
bfs();
cout<<ans;
return ;
}

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. 「Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯」

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

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

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

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

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

  5. 洛谷P2845-Switching on the Lights 开关灯

    Problem 洛谷P2845-Switching on the Lights 开关灯 Accept: 154    Submit: 499Time Limit: 1000 mSec    Memor ...

  6. bzoj4395[Usaco2015 dec]Switching on the Lights*

    bzoj4395[Usaco2015 dec]Switching on the Lights 题意: n*n个房间,奶牛初始在(1,1),且只能在亮的房间里活动.每当奶牛经过一个房间,就可以打开这个房 ...

  7. luogu P2828 Switching on the Lights(开关灯)

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

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

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

  9. 4395: [Usaco2015 dec]Switching on the Lights

    每次到达一个点,或者点亮一个房间的灯的时候,检查一下它四周的点能否走. 一开始看错题了..要求的是最多能开多少房的灯. #include<cstdio> #include<iostr ...

随机推荐

  1. Rails导出CSV

    版本  ruby 1.9  rails 3.2 完整代码 #引入CSV标准类库 require 'csv' class PeopleController < ApplicationControl ...

  2. jsp传递参数的四种方法

    1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="index.jsp"?a= ...

  3. ElasticSearch搜索引擎安装配置拼音插件pinyin

    近几篇ElasticSearch系列: 1.阿里云服务器Linux系统安装配置ElasticSearch搜索引擎 2.Linux系统中ElasticSearch搜索引擎安装配置Head插件 3.Ela ...

  4. 180531-Spring中JavaConfig知识小结

    原文链接:Spring中JavaConfig知识小结/ Sring中JavaConfig使用姿势 去掉xml的配置方式,改成用Java来配置,最常见的就是将xml中的 bean定义, scanner包 ...

  5. Selenium(Python) ddt读取CSV文件数据驱动

    import csvimport unittestfrom time import sleep from ddt import ddt, data, unpackfrom selenium impor ...

  6. 如何搭建本地svn服务器和搭建本地Git服务器

    搭建git本地服务器使用的软件有很多,例如:gitlab,gitblit,gitbucket,gogs,gitolite,具体比较:http://softlab.sdut.edu.cn/blog/su ...

  7. python学习笔记03 --------------程序交互与格式化输出

    1.读取用户输入内容 语法:input() 例: name = input('你的名字是?) print('你好'+name) 程序会等待用户输入名字后打印:你好(用户输入的名字) 注意:input接 ...

  8. pthon web框架flask(一)

    pthon web框架优劣: 知乎上有一个讨论Python 有哪些好的 Web 框架?,从这个讨论中最后我选择了flask,原因是: Django,流行但是笨重,还麻烦,人生苦短,肯定不选 web.p ...

  9. Java基础知识:Java实现Map集合二级联动1

    Java实现Map集合二级联动 Map集合可以保存键值映射关系,这非常适合本实例所需要的数据结构,所有省份信息可以保存为Map集合的键,而每个键可以保存对应的城市信息,本实例就是利用Map集合实现了省 ...

  10. 如何在线测试Exchange的速度

    最新碰到了客户需要比较国内版和国际版的Office365的速度问题,微软提供在线工具测试 这里以Exchange 测试为例子,请参考. PS Onenote贴过来只能至图片,各位看官只能将就了 这里有