bzoj4395【Usaco2015 Dec】Switching on the Lights

4395: [Usaco2015 dec]Switching on the Lights

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 33   Solved: 17
[ Submit][ Status][ Discuss]

Description

Farmer John has recently built an enormous barn consisting of an N×NN×N grid of rooms (2≤N≤100), numbered from (1,1)up to (N,N). Being somewhat afraid of the dark, Bessie the cow wants to turn on the lights in as many rooms as possible.

Bessie starts in room (1,1), the only room that is initially lit. In some rooms, she will find light switches that she can use to toggle the lights in other rooms; for example there might be a switch in room (1,1) that toggles the lights in room (1,2)(1,2). Bessie can only travel through lit rooms, and she can only move from a room (x,y)(x,y) to its four adjacent neighbors (x−1,y), (x+1,y), (x,y−1)(x,y−1) and (x,y+1) (or possibly fewer neighbors if this room is on the boundary of the grid).

Please determine the maximum number of rooms Bessie can illuminate.

有N*N个房间,组成了一张N*N的网格图,Bessie一开始位于左上角(1,1),并且只能上下左右行走。

一开始,只有(1,1)这个房间的灯是亮着的,Bessie只能在亮着灯的房间里活动。

有另外M条信息,每条信息包含四个数a,b,c,d,表示房间(a,b)里有房间(c,d)的灯的开关。

请计算出最多有多少个房间的灯可以被打开

Input

The first line of input contains integers NN and MM (1≤M≤20,000).

The next MM lines each describe a single light switch with four integers xx, yy, aa, bb, that a switch in room (x,y) can be used to toggle the lights in room (a,b). Multiple switches may exist in any room, and multiple switches may toggle the lights of any room.

Output

A single line giving the maximum number of rooms Bessie can illuminate.

Sample Input

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

Sample Output

5

HINT

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.

Source




暴力,每次bfs,直到找不到可以访问的房间。




#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define ll long long
#define pa pair<int,int>
#define maxn 105
#define maxm 20005
#define inf 1000000000
using namespace std;
queue<pa> q;
int n,m,cnt=0,a,b,c,d,ans=1,last=1;
int head[maxn][maxn];
bool vst[maxn][maxn],on[maxn][maxn];
const int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
struct edge_type
{
	int x,y,next;
}e[maxm];
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
inline void add_edge(int a,int b,int c,int d)
{
	e[++cnt]=(edge_type){c,d,head[a][b]};
	head[a][b]=cnt;
}
int main()
{
	n=read();m=read();
	F(i,1,m)
	{
		a=read();b=read();c=read();d=read();
		add_edge(a,b,c,d);
	}
	on[1][1]=true;
	while (1)
	{
		q.push(make_pair(1,1));
		memset(vst,false,sizeof(vst));
		vst[1][1]=true;
		while (!q.empty())
		{
			int x=q.front().first,y=q.front().second;
			q.pop();
			for(int i=head[x][y];i;i=e[i].next)
			{
				int tx=e[i].x,ty=e[i].y;
				if (!on[tx][ty]){on[tx][ty]=true;ans++;}
			}
			F(i,0,3)
			{
				int tx=x+dx[i],ty=y+dy[i];
				if (tx<=0||tx>n||ty<=0||ty>n) continue;
				if (on[tx][ty]&&!vst[tx][ty])
				{
					vst[tx][ty]=true;
					q.push(make_pair(tx,ty));
				}
			}
		}
		if (last==ans) break;
		last=ans;
	}
	printf("%d\n",ans);
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值