P2845 [USACO15DEC]Switching on the Lights 开关灯
题目背景
来源: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);
输出格式:
一个数,最多可以点亮的房间数
输入输出样例
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
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 开关灯的更多相关文章
- Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯(bfs)
P2845 [USACO15DEC]Switching on the Lights 开关灯 题意 题目背景 来源:usaco-2015-dec \(Farm\ John\)最近新建了一批巨大的牛棚.这 ...
- 「Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯」
USACO的又一道搜索题 前置芝士 BFS(DFS)遍历:用来搜索.(因为BFS好写,本文以BFS为准还不是因为作者懒) 链式前向星,本题的数据比较水,所以邻接表也可以写,但是链式前向星它不香吗. 具 ...
- 搜索【洛谷P2845】 [USACO15DEC]Switching on the Lights 开关灯
P2845 [USACO15DEC]Switching on the Lights 开关灯 题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一 ...
- 洛谷P2828 Switching on the Lights(开关灯)
P2828 Switching on the Lights(开关灯) 题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一个N*N的矩形网络.( ...
- 洛谷P2845-Switching on the Lights 开关灯
Problem 洛谷P2845-Switching on the Lights 开关灯 Accept: 154 Submit: 499Time Limit: 1000 mSec Memor ...
- bzoj4395[Usaco2015 dec]Switching on the Lights*
bzoj4395[Usaco2015 dec]Switching on the Lights 题意: n*n个房间,奶牛初始在(1,1),且只能在亮的房间里活动.每当奶牛经过一个房间,就可以打开这个房 ...
- luogu P2828 Switching on the Lights(开关灯)
题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一个N*N的矩形网络.(1<n<100) 然而bessie十分怕黑,他想计算可以把 ...
- 洛谷 P2828 Switching on the Lights(开关灯)
传送门 题目大意:n*n的网格,每个网格是一个房间 都关着灯,只有(1,1)开着灯,且(x,y)有着(z,k)房间灯的开关. 问从(1,1)开始走最多点开几盏灯. 题解:搜索+骗分. 劳资的骗分天下无 ...
- 4395: [Usaco2015 dec]Switching on the Lights
每次到达一个点,或者点亮一个房间的灯的时候,检查一下它四周的点能否走. 一开始看错题了..要求的是最多能开多少房的灯. #include<cstdio> #include<iostr ...
随机推荐
- 成都Uber优步司机奖励政策(3月29日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- CC2541广播机制和代码分析(未完成)
1. 广播通道有3个,是固定的吗?设备为了节省功耗,可以忽略掉几个应答? 连接间隔可以是7.5ms到4s内的任意值,但必须是1.25ms的整数倍,从设备延迟,实际上是一个连接间隔的倍数,代表从设备在必 ...
- c++ singleton
http://www.yolinux.com/TUTORIALS/C++Singleton.html
- Java 语法基础
一 关键字 关键字: 其实就是某种语言赋予了特殊含义的单词 保留字: 其实就是还没有赋予特殊含义 但是准备日后要使用过的单词 二 标示符 标示符: 其实就是在程序中自定义的名词 比如类名, 变量名, ...
- 第5章 Linux网络编程基础
第5章 Linux网络编程基础 5.1 socket地址与API 一.理解字节序 主机字节序一般为小端字节序.网络字节序一般为大端字节序.当格式化的数据在两台使用了不同字节序的主机之间直接传递时,接收 ...
- unity发布自定义分辨率
如果你需要发布unity时想要使用自己设置的分辨率仅需要一下几个步骤: 打开Build Setting->PlayerSetting->Resolution and Presentatio ...
- 213. String Compression【LintCode java】
Description Implement a method to perform basic string compression using the counts of repeated char ...
- yarn logs -applicationId命令java版本简单实现
import java.io.DataInputStream; import java.io.EOFException; import java.io.FileNotFoundException; i ...
- STM32单片机是如何启动的?
STM32单片机是如何启动的? STM32中的内存 STM32中的内存包含两块主要区域:flash memory(只读).static ram memory(SRAM,读写).其中,flash mem ...
- qt qchart缩放后坐标轴间隔取整
使用qt的qchart显示数据曲线,坐标轴QValueAxis可以设置刻度间隔数量,但每个刻度的数值是根据坐标的极值除以间隔数量得到的,不一定是整数,导致曲线控件的显示刻度不适合观察. 如图: 纵坐标 ...