zoukankan      html  css  js  c++  java
  • 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
    ",ans);
        return 0;
    }
  • 相关阅读:
    嵌套查询别名必须性示例。HAVING用法
    分组查询限制。HAVING可写在GROUP BY前。
    合并查询结果集UNION(去重), UNION ALL(不去重),INTERSECT(交集),MINUS(差集,第一个结果集减去第二个结果集,第一个结果集中不在第二个结果集中的记录行),[NOT] EXIST
    18-matlab知识点复习一
    53-java中的queue
    52-2018 蓝桥杯省赛 B 组模拟赛(一)java
    51-迷宫(一)- java版dfs和bfs
    50-2018 蓝桥杯省赛 B 组模拟赛(五)
    49-2015年第6届蓝桥杯Java B组
    46-web页面登入前和登入后控制
  • 原文地址:https://www.cnblogs.com/sssy/p/7087029.html
Copyright © 2011-2022 走看看