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

 

输出格式:

 

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

 

输入输出样例

输入样例#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 = 900;
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[5]={1,0,-1,0,1};
bool can[N][N];
int main()
{
    int a,b,c,d;
    scanf("%d%d",&n,&m);
    vector<struct node> vec[n+1][n+1];
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d%d",&a,&b,&nxt.x,&nxt.y);
        vec[a][b].push_back(nxt);
    }
    cur.x=1;cur.y=1;
    que.push(cur);
    vis[1][1]=1;
    can[1][1]=1;
    while(!que.empty())
    {
        cur=que.front();
        que.pop();
        int x=cur.x,y=cur.y;
        for(int i=0;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]=1;
        }
        
        for(int j=0;j<4;j++)
        {
            int x1=x;int y1=y;
            x1+=fs[j];y1+=fs[j+1];
            if(x1<1||x1>n||y1<1||y1>n)continue;
            if(!vis[x1][y1]&&can[x1][y1])
            nxt.x=x1,nxt.y=y1,que.push(nxt);
            vis[x1][y1]=1;
        }
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)if(can[i][j])ans++;
    printf("%d\n",ans);
    return 0;
}
复制代码

 

posted @   zzzzx  阅读(326)  评论(0编辑  收藏  举报
编辑推荐:
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
阅读排行:
· 本地部署 DeepSeek:小白也能轻松搞定!
· 基于DeepSeek R1 满血版大模型的个人知识库,回答都源自对你专属文件的深度学习。
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!
点击右上角即可分享
微信分享提示