zoukankan      html  css  js  c++  java
  • P2845 [USACO15DEC]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.

    Solution:

      广搜水题

      随便照思路模拟一下打个广搜就好了,然后注意一下入队的细节,问题是我死活交都是$41$老WA$6$个点,重写bfs好几遍死活WA,后面删掉代码重写才意识到数组开小了,WTF报错是WA没显示RE,第一份代码开大数组就A了,diss洛谷评测机啊!

    代码:

    #include<bits/stdc++.h>
    #define il inline
    #define ll long long
    #define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
    #define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
    using namespace std;
    const int N=105,M=200005,dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
    int n,m,ans,num[N][N],tot;
    int to[M],net[M],h[M],cnt;
    bool vis[N][N],ct[N][N];
    struct node{
        int x,y;
        node(int a=0,int b=0){x=a,y=b;}
    }mp[M];
    queue<node>q;
    
    il int gi(){
        int a=0;char x=getchar();
        while(x<'0'||x>'9')x=getchar();
        while(x>='0'&&x<='9')a=(a<<3)+(a<<1)+x-48,x=getchar();
        return a;
    }
    
    il int ID(int x,int y){return (x-1)*n+y;}
    
    il void add(int u,int v){to[++cnt]=v,net[cnt]=h[u],h[u]=cnt;}
    
    il void bfs(){
        ans=1,vis[1][1]=1,ct[1][1]=1;
        q.push(node(1,1));
        while(!q.empty()){
            node a=q.front();q.pop();
            for(int i=h[num[a.x][a.y]];i;i=net[i]){
                int x=mp[to[i]].x,y=mp[to[i]].y;
                if(!ct[x][y]){
                    ct[x][y]=1,ans++;
                    if(!vis[x][y]){
                        if(vis[x-1][y]||vis[x+1][y]||vis[x][y-1]||vis[x][y+1]) vis[x][y]=1,q.push(node(x,y));
                    }
                }
            }
            For(i,0,3) {
                int x=dx[i]+a.x,y=dy[i]+a.y;
                if(x<1||y<1||x>n||y>n)continue;
                if(ct[x][y]&&!vis[x][y]){
                    if(vis[x-1][y]||vis[x+1][y]||vis[x][y-1]||vis[x][y+1]) vis[x][y]=1,q.push(node(x,y));
                }
            }
        }
    }
    
    int main(){
        n=gi(),m=gi();
        For(i,1,n) For(j,1,n) num[i][j]=++tot,mp[tot].x=i,mp[tot].y=j;
        int a,b,c,d;
        while(m--){
            a=gi(),b=gi(),c=gi(),d=gi();
            add(num[a][b],num[c][d]);
        }
        bfs();
        cout<<ans;
        return 0;
    }
  • 相关阅读:
    商品销量与虚拟销量的计算方法
    EF获取当天的数据集合
    C# xml转化为类集合
    Linux下找不到动态链接库;
    STRTOK函数和STRTOK_R函数
    Ubuntu Linux 环境变量PATH设置
    关于Ubantu下使用cshell的问题解决
    realloc 使用详解(分析realloc invalid pointer、指针无效等错误)【转】
    ubuntu安装包查找及安装
    【转】MySql数据库--mysql_real_escape_string()函数
  • 原文地址:https://www.cnblogs.com/five20/p/9374288.html
Copyright © 2011-2022 走看看