luogu P2828 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.
bfs爆搜,搜索每一个可扩展的点
#include<vector>
#include<cstdio>
#include<queue>
using namespace std;
const int N = ;
int n,m;
struct node{
int x;int y;
}cur,nxt;
queue<node>que;
int ans;
bool vis[N][N],vv[N][N];
int fs[]={,,-,,};
bool can[N][N];
int main()
{
int a,b,c,d;
scanf("%d%d",&n,&m);
vector<struct node> vec[n+][n+];
for(int i=;i<=m;i++)
{
scanf("%d%d%d%d",&a,&b,&nxt.x,&nxt.y);
vec[a][b].push_back(nxt);
}
cur.x=;cur.y=;
que.push(cur);
vis[][]=;
can[][]=;
while(!que.empty())
{
cur=que.front();
que.pop();
int x=cur.x,y=cur.y;
for(int i=;i<vec[x][y].size();i++)
{
if(vis[vec[x][y][i].x][vec[x][y][i].y]&&!can[vec[x][y][i].x][vec[x][y][i].y])
nxt.x=vec[x][y][i].x,nxt.y=vec[x][y][i].y,que.push(nxt);
can[vec[x][y][i].x][vec[x][y][i].y]=;
} for(int j=;j<;j++)
{
int x1=x;int y1=y;
x1+=fs[j];y1+=fs[j+];
if(x1<||x1>n||y1<||y1>n)continue;
if(!vis[x1][y1]&&can[x1][y1])
nxt.x=x1,nxt.y=y1,que.push(nxt);
vis[x1][y1]=;
}
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)if(can[i][j])ans++;
printf("%d\n",ans);
return ;
}
luogu P2828 Switching on the Lights(开关灯)的更多相关文章
- 洛谷P2828 Switching on the Lights(开关灯)
P2828 Switching on the Lights(开关灯) 题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一个N*N的矩形网络.( ...
- 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 开关灯
P2845 [USACO15DEC]Switching on the Lights 开关灯 题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一 ...
- 洛谷 P2828 Switching on the Lights(开关灯)
传送门 题目大意:n*n的网格,每个网格是一个房间 都关着灯,只有(1,1)开着灯,且(x,y)有着(z,k)房间灯的开关. 问从(1,1)开始走最多点开几盏灯. 题解:搜索+骗分. 劳资的骗分天下无 ...
- 「Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯」
USACO的又一道搜索题 前置芝士 BFS(DFS)遍历:用来搜索.(因为BFS好写,本文以BFS为准还不是因为作者懒) 链式前向星,本题的数据比较水,所以邻接表也可以写,但是链式前向星它不香吗. 具 ...
- P2845 [USACO15DEC]Switching on the Lights 开关灯
题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一个N*N的矩形网络.(1<n<100) 然而bessie十分怕黑,他想计算可以把 ...
- 洛谷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),且只能在亮的房间里活动.每当奶牛经过一个房间,就可以打开这个房 ...
- 4395: [Usaco2015 dec]Switching on the Lights
每次到达一个点,或者点亮一个房间的灯的时候,检查一下它四周的点能否走. 一开始看错题了..要求的是最多能开多少房的灯. #include<cstdio> #include<iostr ...
随机推荐
- Android 环境变量设置
需要设置以下全局的环境变量 ANDROID_HOME: C:\Users\bellesun\AppData\Local\Android\sdk JAVA_HOME: C:\Program Files ...
- JS 如何获取radio或者checkbox选中后的值
废话不多说,直接上代码: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...
- (原)Unreal源码搬山-动画篇 自定义动画节点(一)
@author:黑袍小道 太忙了,来更新下,嘿嘿 前言: 本文是接着上文 Unreal搬山之动画模块_Unreal动画流程和框架,进行简单入门如何自定义动画图标的AnimNode. 正文: 一.Ani ...
- leetcode 214. 最短回文串 解题报告
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: "aaa ...
- poj2388 更水
Who's in the Middle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34974 Accepted: 2 ...
- Oracle 数据库导出时 EXP-00008;ORA-00904
问题是客户端和服务器端版本问题,我本地是11g,而服务器端是10g. 规则1:低版本的exp/imp可以连接到高版本(或同版本)的数据库服务器,但高版本的exp/imp不能连接到低版本的数据库服务器. ...
- 当发送ICMP包的时候不一定能收得到(arp已经应答了)【复现不了了】
arp已经应答了,然后再返回ICMP应答的时候竟然不被回复. 其实这里想想也很容易想清楚: 虽然arp给了回复,但是真正到ICMP报文到的时候,我理解报文到的时候,我理解还是要进行与本地网络兑换的,本 ...
- windows系统设备管理器显示全部硬件
下面的小命令能让隐藏的未卸载掉的硬件设备彻底现身:开始-运行-CMD C:\> C:\>start devmgmt.msc 之后再在Windows 的设备管理器中,单击菜单“显示”-“显示 ...
- JAVA输出最大值和最小值
public class MaxMin{ public static void main(String[] args) { int[] arr = new int[6]; Scanner input ...
- zepto方法
1.$.inArray $.inArray v1.0+ $.inArray(element, array, [fromIndex]) ⇒ number 搜索数组中指定值并返回它的索引(如果没有找到则返 ...