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);

输出格式:

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

输入输出样例

输入样例#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.

/*
一个特别需要注意的细节:一个房间可以走多次!比较蠢的,我就另外写了一个宽搜来check这种情况
*/
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#define maxn 110
using namespace std;
int n,m,e[][]={{,},{,-},{-,},{,}};
bool vis[maxn][maxn],bri[maxn][maxn],hav[maxn][maxn],v[maxn][maxn];
struct node{
int x,y;
}cur,nxt,map[maxn][maxn][];
node make_node(int x,int y){
node res;res.x=x;res.y=y;
return res;
}
int main(){
freopen("Cola.txt","r",stdin);
scanf("%d%d",&n,&m);
int a,b,c,d;
for(int i=;i<=m;i++){
scanf("%d%d%d%d",&a,&b,&c,&d);
map[a][b][++map[a][b][].x].x=c;map[a][b][map[a][b][].x].y=d;
hav[a][b]=;
}
queue<node>q;
cur=make_node(,);vis[][]=;bri[][]=;
q.push(cur);
while(!q.empty()){
while(!q.empty()){
cur=q.front();q.pop();
if(hav[cur.x][cur.y]){
for(int i=;i<=map[cur.x][cur.y][].x;i++){
int tx=map[cur.x][cur.y][i].x,ty=map[cur.x][cur.y][i].y;
if(bri[tx][ty])continue;
bri[tx][ty]=;
}
}
for(int i=;i<;i++){
int xx=cur.x+e[i][],yy=cur.y+e[i][];
if(xx<||xx>n||yy<||yy>n)continue;
if(vis[xx][yy]||bri[xx][yy]==)continue;
q.push(make_node(xx,yy));
vis[xx][yy]=;
}
}
queue<node>que;
while(!que.empty())que.pop();
memset(v,,sizeof(v));
que.push(make_node(,));v[][]=;
while(!que.empty()){
cur=que.front();que.pop();
for(int i=;i<;i++){
int xx=e[i][]+cur.x,yy=e[i][]+cur.y;
if(xx>n||xx<||yy>n||yy<)continue;
if(!v[xx][yy]&&bri[xx][yy]){
v[xx][yy]=;
que.push(make_node(xx,yy));
if(!vis[xx][yy])
q.push(make_node(xx,yy)),vis[xx][yy]=;
}
}
}
}
int cnt=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(bri[i][j])cnt++;
printf("%d",cnt);
}

最新文章

  1. 你真的会玩SQL吗?Case也疯狂
  2. 利用CSS3D效果制作简易旋转木马效果
  3. 自动生成build.xml文件
  4. Sql Server Analysis Service 转换为UnknownMember的正确设置 (转载)
  5. 向SqlServer数据库插入数据
  6. 【SpringMVC】SpringMVC系列14之SpringMVC国际化
  7. Computer Science Theory for the Information Age-4: 一些机器学习算法的简介
  8. SharePoint2010 Form验证配置流程
  9. 2.3 DHC REST
  10. CentOS 7.x上gitlab搭建教程(https可用,邮件可用)
  11. pyqt4桌面软件各种画布组合结构实例
  12. 微软Telnet的回显功能开启
  13. Android的AdapterViewFlipper和Stackview-android学习之旅(三十)
  14. YUV420格式解析
  15. #Java学习之路——基础阶段二(第四篇)
  16. hbase读的性能优化
  17. 20175313 张黎仙《Java程序设计》第九周学习总结
  18. [leetcode.com]算法题目 - Pow(x, n)
  19. koa2 入门(1)koa-generator 脚手架和 mongoose 使用
  20. 转载: 几个主流的Java连接池整理

热门文章

  1. python3与Redis连接操作
  2. python-socket1
  3. 【leetcode刷题笔记】Recover Binary Search Tree
  4. redis学习--String数据类型。
  5. Agc019_F Yes or No
  6. java将白色背景图片转换成透明图片
  7. 【caffe】卷积层代码解析
  8. mysql绿色安装
  9. nefu阶乘定理
  10. virtual judge(专题一 简单搜索 E)