搜索【洛谷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 ...
随机推荐
- python web框架 Django基本操作
django 操作总结! django框架安装: cmd安装: pip3 install django pycharm安装: 在python变量下 搜索 django 安装 创建django项目: c ...
- redis学习一 大体概述
redis 命令查找:http://doc.redisfans.com/ 1,redis 技术简介以及疑问 redis是一个开源的,内存存储的数据结构服务器.可以用做数据库,高速缓存和消息队 ...
- java 多线程系列---JUC原子类(二)之AtomicLong原子类
概要 AtomicInteger, AtomicLong和AtomicBoolean这3个基本类型的原子类的原理和用法相似.本章以AtomicLong对基本类型的原子类进行介绍. AtomicLong ...
- Sandbox简介和路径获取
一.简介 iOS的沙盒机制,每个应用只能访问自己应用目录下的文件.iOS应用产生的内容,如文件.缓存内容等都必须存储在自己的沙盒内.默认情况下,每个沙盒含有3个文件夹:Documents, Libra ...
- LNMP 1.5 php-fpm配置文件
php-fpm配置文件: /usr/local/php/etc/php-fpm.conf :php-fpm服务的配置文件 /usr/local/php/etc/php.ini :ph ...
- Tornado之抽屉实战(1)--分析与架构
项目模拟地址:http://dig.chouti.com/ 知识点应用: AJAX 用于偷偷发请求 原生ajax jQuery ajax($.ajax) iframe伪造 上传文件 传统Form ...
- 1-3 分布式系统的瓶颈以及zk的相关特性
- SQL查询语句 [2]
一.快捷查询 快捷查询方式是一种多字段查询的简化写法,在多个字段之间用'|'隔开表示OR,用'&'隔开表示 AND. 1.不同字段相同查询条件 在 Home/controller/UserC ...
- Action层, Service层 和 Dao层的功能区分
Action/Service/DAO简介: Action是管理业务(Service)调度和管理跳转的. Service是管理具体的功能的. Action只负责管理,而Service负责实施. DAO ...
- 指静脉屏幕说明usart hmi
1. 1.usart HMI 下载最新版软件 2.如上图可以设置横屏 3.txt的最大字符默认是10 要改大一些 4.t0.txt="aaa" 这是串口通讯命令但前面还有开始码