搜索【洛谷P2845】 [USACO15DEC]Switching on the Lights 开关灯
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:
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
const int wx=117;
inline int read(){
int sum=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0'; ch=getchar();}
return sum*f;
}
int f[wx][wx],mpx[200017],mpy[200017];
int head[200017],vis[wx][wx],had[wx][wx];
int dx[]={0,1,0,-1,0};
int dy[]={0,0,1,0,-1};
int n,m,num,ans=1,tot;
struct e{
int nxt,to;
}edge[200017];
void add(int from,int to){
edge[++num].nxt=head[from];
edge[num].to=to;
head[from]=num;
}
queue<int > q;
void bfs(){
vis[1][1]=1; had[1][1]=1; q.push(1); q.push(1);
while(q.size()){
int ux=q.front(); q.pop();
int uy=q.front(); q.pop();
for(int i=head[f[ux][uy]];i;i=edge[i].nxt){
int v=edge[i].to;
int ex=mpx[v]; int ey=mpy[v];
if(!had[ex][ey]){
had[ex][ey]=1;ans++;
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);
}
}
for(int i=1;i<=4;i++){
int ex=ux+dx[i]; int ey=uy+dy[i];
if(ex<1||ey<1||ex>n||ey>n||vis[ex][ey]||!had[ex][ey])continue;
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);
}
}
printf("%d\n",ans);
}
int main(){
n=read(); m=read();
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
f[i][j]=++tot; mpx[tot]=i; mpy[tot]=j;
}
}
for(int i=1;i<=m;i++){
int x,y,z,c;
x=read(); y=read(); z=read(); c=read();
add(f[x][y],f[z][c]);
}
bfs();
return 0;
}
搜索【洛谷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\)最近新建了一批巨大的牛棚.这 ...
- P2845 [USACO15DEC]Switching on the Lights 开关灯
题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一个N*N的矩形网络.(1<n<100) 然而bessie十分怕黑,他想计算可以把 ...
- 「Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯」
USACO的又一道搜索题 前置芝士 BFS(DFS)遍历:用来搜索.(因为BFS好写,本文以BFS为准还不是因为作者懒) 链式前向星,本题的数据比较水,所以邻接表也可以写,但是链式前向星它不香吗. 具 ...
- 洛谷 P3128 [USACO15DEC]最大流Max Flow-树上差分(点权/点覆盖)(模板题)
因为徐州现场赛的G是树上差分+组合数学,但是比赛的时候没有写出来(自闭),背锅. 会差分数组但是不会树上差分,然后就学了一下. 看了一些东西之后,对树上差分写一点个人的理解: 首先要知道在树上,两点之 ...
- 状压搜索 洛谷T47092 作业
TYM 有 nn 本作业,编号为 1,\dots,n1,…,n. 由于 \mathrm{TYM}TYM 很喜欢偷懒,而且不喜欢消耗脑细胞,所以他选择跳着完成这 nn 本作业.此外,如果将做作业的顺序转 ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...
- 洛谷 P3130 [USACO15DEC]计数haybalesCounting Haybales
P3130 [USACO15DEC]计数haybalesCounting Haybales 题目描述 Farmer John is trying to hire contractors to help ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
随机推荐
- 创建github怎样管理
创建版本库 第一步: 创建一个版本库非常简单,首先,选择一个合适的地方,创建一个空目录 $mkdir learngit $cd learngit $pwd mkdir learngit 创建一个名叫“ ...
- 2015.1.3 DataGridView中嵌入其它控件
1.按正常方法绑定待嵌入列的值,先赋值为空也行. 2.添加combbox到datagrivdview中 dvaw.Controls.Add(cb_dir); 3.添加DataGridView Mous ...
- 11-24网页基础--Js框架及学习思路
第一部分 基本语法: 1.数据类型(字符串.小数.整数.布尔.时间日期)var s="3.14" var n=parsefloat(s) s+=5;var s="abc3 ...
- leetcode637
层次遍历,计算每一层的节点值,然后求平均值. class Solution { public: vector<double> averageOfLevels(TreeNode* root) ...
- LAMP 3.2 mysql登陆
mysql 服务启动时,不仅会监听 IP:Port,还会监听一个 socket,我们安装的 mysql 是监听在/tmp/mysql.sock.如果 php 是在本地,那么 php 和 mysql 通 ...
- [转]SQL 模糊查询
在进行数据库查询时,有完整查询和模糊查询之分. 一般模糊查询语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: 1,% :表 ...
- 安装nodemon热启动
1.安装: cnpm i nodemon -g 2.执行 nodemon .\launch.js .\config_preview\ .\launch.js 为我要启动的脚本文件 .\config_p ...
- String/StringBuilder 类 判断QQ号码
1.1. 训练描述:[方法.String类] 一.需求说明:请用户输入一个“QQ号码”,我们来判断这个QQ号码是否正确. 要求:使用方法来完成判断功能. 1.2. 操作步骤描述 建立MainApp类 ...
- php学习笔记-php中的比较运算符
其中比较难懂的是==和=== ==是只比较两个变量的值,不仅仅是用于比较两个数是否相等,还可以比较int和string,不过会先转化string为int类型再比较,值相等则返回true,值不相等则返回 ...
- netty中的PlatformDependent
通过类名就知道这是一个平台有关的类,通过对该类的学习可以帮助我们实现一个跨平台的应用.但是有些方法放的实现不是很好,比如:isWindows0.通过File的separator就可以判断出来.没必要那 ...