题目背景

来源: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);

输出格式:

一个数,最多可以点亮的房间数

输入输出样例

输入样例#1:

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
输出样例#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(开关灯)的更多相关文章

  1. 洛谷P2828 Switching on the Lights(开关灯)

    P2828 Switching on the Lights(开关灯) 题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一个N*N的矩形网络.( ...

  2. Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯(bfs)

    P2845 [USACO15DEC]Switching on the Lights 开关灯 题意 题目背景 来源:usaco-2015-dec \(Farm\ John\)最近新建了一批巨大的牛棚.这 ...

  3. 搜索【洛谷P2845】 [USACO15DEC]Switching on the Lights 开关灯

    P2845 [USACO15DEC]Switching on the Lights 开关灯 题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一 ...

  4. 洛谷 P2828 Switching on the Lights(开关灯)

    传送门 题目大意:n*n的网格,每个网格是一个房间 都关着灯,只有(1,1)开着灯,且(x,y)有着(z,k)房间灯的开关. 问从(1,1)开始走最多点开几盏灯. 题解:搜索+骗分. 劳资的骗分天下无 ...

  5. 「Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯」

    USACO的又一道搜索题 前置芝士 BFS(DFS)遍历:用来搜索.(因为BFS好写,本文以BFS为准还不是因为作者懒) 链式前向星,本题的数据比较水,所以邻接表也可以写,但是链式前向星它不香吗. 具 ...

  6. P2845 [USACO15DEC]Switching on the Lights 开关灯

    题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一个N*N的矩形网络.(1<n<100) 然而bessie十分怕黑,他想计算可以把 ...

  7. 洛谷P2845-Switching on the Lights 开关灯

    Problem 洛谷P2845-Switching on the Lights 开关灯 Accept: 154    Submit: 499Time Limit: 1000 mSec    Memor ...

  8. bzoj4395[Usaco2015 dec]Switching on the Lights*

    bzoj4395[Usaco2015 dec]Switching on the Lights 题意: n*n个房间,奶牛初始在(1,1),且只能在亮的房间里活动.每当奶牛经过一个房间,就可以打开这个房 ...

  9. 4395: [Usaco2015 dec]Switching on the Lights

    每次到达一个点,或者点亮一个房间的灯的时候,检查一下它四周的点能否走. 一开始看错题了..要求的是最多能开多少房的灯. #include<cstdio> #include<iostr ...

随机推荐

  1. time模块与random模块,六位含字母随机验证码

    # time模块# import time# time.time()#计算这一时刻时间戳 *******# time.sleep(1)#让cpu休眠一定时间 *******# time.clock() ...

  2. java IO小结

    package 字符与字节转换; import java.io.*; public class char_byte { public static void main(String[] args) { ...

  3. 牛客网暑期ACM多校训练营(第一场):J-Different Integers(分开区间不同数+树状数组)

    链接:J-Different Integers 题意:给出序列a1, a2, ..., an和区间(l1, r1), (l2, r2), ..., (lq, rq),对每个区间求集合{a1, a2, ...

  4. lo口环路问题分析

    流程如下,collecter抓取网卡lo和wlan0数据,其中lo口无数据,wlan0是笔记本上网网口,然后按自定义协议把数据包通过lo口发给后端dispatch进行分发! 这种模式下,抓包程序每经过 ...

  5. ssh 免交互登录 ,远程执行命令脚本。

    ##免交互SSH登录auto_login_ssh () {    expect -c "set timeout -1;                spawn -noecho ssh -o ...

  6. 【Android】实验3 颜色、字符串资源的使用【提交截止时间:2016.4.1】

    实验4 颜色.字符串资源的使用 [目的] 掌握Android中颜色和字符串资源的使用方法. 理解Android中尺寸和布局资源的使用方法. [要求] 在工程中为Activity.View使用颜色资源: ...

  7. python os操作

    大家先看一下Python os模块中的部分函数 python 路径相关的函数 os.listdir(dirname):列出dirname下的目录和文件 os.getcwd():获得当前工作目录 os. ...

  8. 【bzoj1565】[NOI2009]植物大战僵尸 拓扑排序+最大权闭合图

    原文地址:http://www.cnblogs.com/GXZlegend/p/6808268.html 题目描述 输入 输出 仅包含一个整数,表示可以获得的最大能源收入.注意,你也可以选择不进行任何 ...

  9. 利用VS2013 XSLT对 XML进行转换

    1.打开VS2013 2.文件-->新建-->文件-->XML文件 3.文件-->新建-->文件-->XSLT文件 4.CTRL+SHIFT+S 保存2个文件位置 ...

  10. poj1679 次最小生成树 kruskal(暴力枚举)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...