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 ...
随机推荐
- tcpdump使用
1. tcpdump选项 它的命令格式为: tcpdump [ -DenNqvX ] [ -c count ] [ -F file ] [ -i interface ] [ -r file ][ -s ...
- javasript 字符串 数组操作
Javascript中经常涉及到对字符串和数组的处理,今天总结一下具体的用法 一 操作字符串 String对象有很多函数,可以以不同的方式访问和操作字符串,具体方法如下: charAt(index ...
- android 学习六 构建用户界面和使用控件
1.常用Android控件最终都会继承自View类 2.ViewGroup是一些布局类列表的基类,包括View和ViewGroup 3.构造界面的三种方法 a.完全使用代码(太灵活,而不好维护) ...
- 虚拟机克隆CentOs后网卡问题
1.直接修改 /etc/sysconfig/network-scripts/ifcfg-eth0 删掉UUID HWADDR配置静态地址 2.修改配置文件vi /etc/udev/rules.d/7 ...
- git配置github链接
1.百度git官网-下载最新版git 2.一路默认下一步安装 3.打开 git bash here 命令行 4.注册github账号(用自己的邮箱就可以,不会英文可以用谷歌翻译)注册成功后建立项目 5 ...
- TW实习日记:第18天
今天的bug没有那么多了,都是些小bug,一下就改好了.或者是接口那边数据返回的有问题,通知一下同事就ok了.主要今天是在赶功能进度,然而有一个功能模块需求里并没有写,实在是不知道要做成什么样子,真的 ...
- [JSON].remove( keyPath )
语法:[JSON].remove( keyPath ) 返回:无 说明:移除指定路径的键 示例: Set jsonObj = toJson("{div:{'#text-1': 'is tex ...
- CSS3自定义字体
原文摘自:https://www.cnblogs.com/moqiutao/archive/2015/12/23/5070463.html 总节: 1) 定义字体标准格式: @font-face { ...
- 复合词 (Compund Word,UVa 10391)
题目描述: 题目思路: 用map保存所有单词赋键值1,拆分单词,用map检查是否都为1,即为复合词 #include <iostream> #include <string> ...
- Spark mlib的本地向量
Spark mlib的本地向量有两种: DenseVctor :稠密向量 其创建方式 Vector.dense(数据) SparseVector :稀疏向量 其创建方式有两种: 方法一:Vector. ...