题目背景

来源: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. 优步UBER司机全国各地奖励政策汇总 (3月7日-3月13日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. 成都Uber优步司机奖励政策(2月17日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. springboot 读写excel

    添加两个坐标: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</a ...

  4. Ruby数据类型

    数字类型 书写整数时,可以根据需要在整数之间任意加入下划线而不会影响数字的值 a=123_45_78 puts a # => 12345678 to_i 截掉小数点之后的数字取整 内置Math模 ...

  5. 腾讯WeTest受邀参展2018谷歌开发者大会,Android 9专区免费开放

    2018谷歌开发者大会(Google Developer Days)于9月20日正式在上海拉开帷幕.在今年,围绕谷歌最新研发技术,来自机器学习.物联网.云服务等各领域精英参会并进行了案例分享. 201 ...

  6. [JSON].valueOf( keyPath )

    语法:[JSON].valueOf( keyPath ) 返回:[任意类型 | null] 说明:获取键名路径原值,它保留原始值的类型 示例: b = sysFile.binary("tes ...

  7. 【springmvc+mybatis项目实战】杰信商贸-2.数据库配置

    首先我们来了解项目的架构 我们分别使用了MySql和Oracle数据库,即是异构数据库.我们做到一个平台支持多个数据库.数据库建模我们使用Sybase公司的PowerDesigner(以后简称PD), ...

  8. LeetCode - 442. Find All Duplicates in an Array - 几种不同思路 - (C++)

    题目 题目链接 Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ...

  9. oraclize预言机资料

    oraclize预言机资料 智能合约如何可信的与外部世界交互: https://blog.csdn.net/sportshark/article/details/77477842 国外一篇讲得很详细的 ...

  10. HDU 2489 Minimal Ratio Tree(暴力+最小生成树)(2008 Asia Regional Beijing)

    Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated accord ...