zoukankan      html  css  js  c++  java
  • [SinGuLaRiTy] 2017 百度之星程序设计大赛 初赛A

    【SinGuLaRiTy-1036】 Copyright (c) SinGuLaRiTy 2017. All Rights Reserved.

    小C的倍数问题

    Time Limit: 2000/1000 MS (Java/Others)   Memory Limit: 32768/32768 K (Java/Others)

    Problem Description

    根据小学数学的知识,我们知道一个正整数x是3的倍数的条件是x每一位加起来的和是3的倍数。反之,如果一个数每一位加起来是3的倍数,则这个数肯定是3的倍数。

    现在给定进制P,求有多少个B满足P进制下,一个正整数是B的倍数的充分必要条件是每一位加起来的和是B的倍数。

    Input

    第一行一个正整数T表示数据组数(1<=T<=20)。

    接下来T行,每行一个正整数P(2 < P < 1e9),表示一组询问。

    Output

    对于每组数据输出一行,每一行一个数表示答案。

    Sample Input

    1
    10

    Sample Output

    3

    Code

    数学(这道题不算是"数论"吧......)

    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    
    using namespace std;
    
    int T;
    
    int main()
    {
        cin>>T;
        for(int i=1;i<=T;i++)
        {
            int n,cnt=0;
            scanf("%d",&n);
            n=n-1;
            for(int i=1;i*i<=n;i++)
            {
                if(n%i==0)
                    cnt+=2;
                if(i*i==n)
                    cnt--;
            }
            printf("%d
    ",cnt);
        }
        return 0;
    }

    数据分割

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

    Problem Description

    小w来到百度之星的赛场上,准备开始实现一个程序自动分析系统。
    这个程序接受一些形如x_i = x_j 或x_​i​​≠x_​j​​ 的相等/不等约束条件作为输入,判定是否可以通过给每个 w 赋适当的值,来满足这些条件。
    输入包含多组数据。 然而粗心的小w不幸地把每组数据之间的分隔符删掉了。 他只知道每组数据都是不可满足的,且若把每组数据的最后一个约束条件去掉,则该组数据是可满足的。
    请帮助他恢复这些分隔符。

    Input

    第1行:一个数字L,表示后面输入的总行数。
    之后L行,每行包含三个整数,i,,j,e描述一个相等/不等的约束条件,若e=1,则该约束条件为x_i = x_j若e=0,则该约束条件为x​_i​​!=x_​j​​ 。
    i,j,L≤100000
    Lx​i​​,x​j​​≤L

    Output

    输出共T+1行。
    第一行一个整数T,表示数据组数。
    接下来T行的第i行,一个整数,表示第i组数据中的约束条件个数。

    Sample Input

    6
    2 2 1
    2 2 1
    1 1 1
    3 1 1
    1 3 1
    1 3 0

    Sample Output

    1
    6

    Code

    并查集

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<set>
    
    #define maxn 200011
    
    using namespace std;
    
    set<int> s[maxn];
    
    int T,n=200001;
    int cnt,vis[maxn],sta[maxn],top;
    
    struct Node
    {
        int fa[maxn];
        Node()
        {
            for(int i=1;i<=n;i++)
                fa[i]=i;
        }
        int find(int x)
        {
            return x==fa[x]?x:(fa[x]=find(fa[x]));
        }
        void Union(int &x,int &y)
        {
            x=find(x);
            y=find(y);
            if(x!=y)
                fa[x]=y;
        }
    }node;
    
    int x,y,id;
    int ans[maxn];
    int lans=0,Case;
    
    void clear()
    {
        while (top)
        {
            int now=sta[top--];
            node.fa[now]=now;
            s[now].clear();
        }
        ans[cnt++]=Case;
    }
    
    int main()
    {
        scanf("%d",&T);
        memset(vis,0,sizeof(vis));
        top=0;
        cnt=1;
        for (Case=1;Case<=T;Case++)
        {
            scanf("%d%d%d",&x,&y,&id);
            if(vis[x]!=cnt)
                vis[x]=cnt,sta[++top]=x;
            if(vis[y]!=cnt)
                vis[y]=cnt,sta[++top]=y;
            x=node.find(x);
            y=node.find(y);
            if(s[x].size()>s[y].size())
            {
                int t=x;
                x=y;
                y=t;
            }
            if(id)
            {
                if(s[x].find(y)!=s[x].end())
                {
                    clear();
                    continue;
                }
                node.Union(x,y);
                for(set<int>::iterator i=s[x].begin();i!=s[x].end();i++)
                {
                    int now=*i;
                    now=node.find(now);
                    s[now].erase(x);
                    s[y].insert(now);
                    s[now].insert(y);
                }
                s[x].clear();
            }
            else
            {
                if(x==y)
                {
                    clear();
                    continue;
                }
                s[x].insert(y);
                s[y].insert(x);
            }
        }
        printf("%d
    ",cnt-1);
        for(int i=1;i<cnt;i++)
            printf("%d
    ",ans[i]-ans[i-1]);
        return 0;
    }

    路径交

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)

    Problem Description

    给定一棵n个点的树,以及m条路径,每次询问第L条到第R条路径的交集部分的长度(如果一条边同时出现在2条路径上,那么它属于路径的交集)。

    Input

    第一行一个数n(n<=500,000)接下来n-1行,每行三个数x,y,z,表示一条从x到y并且长度为z的边第n+1行一个数m(m<=500,000)接下来m行,每行两个数u,v,表示一条从u到v的路径接下来一行一个数Q,表示询问次数(Q<=500,000)接下来Q行,每行两个数L和R

    Output

    Q行,每行一个数表示答案。

    Sample Input

    4
    1 2 5
    2 3 2
    1 4 3
    2
    1 2
    3 4
    1
    1 2

    Sample Output

    5

    Code

    线段树区间合并+LCA

    #include<cstdlib>
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    
    #define lson x<<1
    #define rson x<<1|1
    
    using namespace std;
    
    const int maxn=500010;
    typedef long long ll;
    
    struct path
    {
        int a,b,c;
        path(){}
        path(int A,int B,int C)
        {
            a=A,b=B,c=C;
        }
    }p[maxn],s[maxn<<2];
    
    int n,m,q,cnt;
    int mn[20][maxn<<1],Log[maxn<<1],pos[maxn],dep[maxn],fa[maxn];
    int head[maxn],nxt[maxn<<1],val[maxn<<1],to[maxn<<1];
    int cs[10];
    
    ll len[maxn];
    
    int MN(int a,int b)
    {
        return dep[a]<dep[b]?a:b;
    }
    
    int lca(int a,int b)
    {
        int x=pos[a],y=pos[b];
        if(x>y)
            swap(x,y);
        int k=Log[y-x+1];
        return MN(mn[k][x],mn[k][y-(1<<k)+1]);
    }
    
    bool cmp(int a,int b)
    {
        return dep[a]<dep[b];
    }
    
    path mix(path x,path y)
    {
        if(!x.c||!y.c)
            return path(0,0,0);
        cs[1]=lca(x.a,y.a);
        cs[2]=lca(x.a,y.b);
        cs[3]=lca(x.b,y.a);
        cs[4]=lca(x.b,y.b);
        sort(cs+1,cs+5,cmp);
        int md=max(dep[x.c],dep[y.c]),nd=min(dep[x.c],dep[y.c]);
        if(dep[cs[1]]<nd||dep[cs[3]]<md)
            return path(0,0,0);
        else
            return path(cs[3],cs[4],lca(cs[3],cs[4]));
    }
    
    inline int read()
    {
        int ret=0,f=1;
        char gc=getchar();
        while(gc<'0'||gc>'9'){if(gc=='-')f=-f;   gc=getchar();}
        while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
        return ret*f;
    }
    
    void add(int a,int b,int c)
    {
        to[cnt]=b;
        val[cnt]=c;
        nxt[cnt]=head[a];
        head[a]=cnt++;
    }
    
    void dfs(int x)
    {
        pos[x]=++pos[0];
        mn[0][pos[0]]=x;
        for(int i=head[x];i!=-1;i=nxt[i])
        {
            if(to[i]!=fa[x])
            {
                fa[to[i]]=x,dep[to[i]]=dep[x]+1,len[to[i]]=len[x]+val[i],dfs(to[i]);
                mn[0][++pos[0]]=x;
            }
        }
    }
    
    void build(int l,int r,int x)
    {
        if(l==r)
        {
            s[x]=p[l];
            return ;
        }
        int mid=l+r>>1;
        build(l,mid,lson),build(mid+1,r,rson);
        s[x]=mix(s[lson],s[rson]);
    }
    
    path query(int l,int r,int x,int a,int b)
    {
        if(a<=l&&r<=b)
            return s[x];
        int mid=l+r>>1;
        if(b<=mid)
            return query(l,mid,lson,a,b);
        if(a>mid)
            return query(mid+1,r,rson,a,b);
        return mix(query(l,mid,lson,a,b),query(mid+1,r,rson,a,b));
    }
    
    int main()
    {
        n=read();
        int i,j,a,b,c;
        memset(head,-1,sizeof(head));
        for(i=1;i<n;i++)
        {
            a=read(),b=read(),c=read();
            add(a,b,c),add(b,a,c);
        }
        dep[1]=1;
        dfs(1);
        for(i=2;i<=2*n-1;i++)
            Log[i]=Log[i>>1]+1;
        for(j=1;(1<<j)<=2*n-1;j++)
            for(i=1;i+(1<<j)-1<=2*n-1;i++)
                mn[j][i]=MN(mn[j-1][i],mn[j-1][i+(1<<j-1)]);
        m=read();
        for(i=1;i<=m;i++)
        {
            p[i].a=read(),p[i].b=read();
            p[i].c=lca(p[i].a,p[i].b);
        }
        build(1,m,1);
        q=read();
        for(i=1;i<=q;i++)
        {
            a=read(),b=read();
            path ans=query(1,m,1,a,b);
            printf("%I64d
    ",len[ans.a]+len[ans.b]-2*len[ans.c]);
        }
        return 0;
    }

    迷宫出逃

    Time Limit: 2000/1000 MS (Java/Others)   Memory Limit: 32768/32768 K (Java/Others)

    Problem Description

    小明又一次陷入了大魔王的迷宫,在无人机的帮忙下,小明获得了整个迷宫的草图。
    不同于一般的迷宫,魔王在迷宫里安置了机关,一旦触碰,那么四个方向所在的格子,将翻转其可达性(原先可通过的格子不可通过,反之亦然,机关可以反复触发)。为了防止小明很容易地出逃,魔王在临走前把钥匙丢在了迷宫某处,只有拿到钥匙,小明才能开门在出口处离开迷宫。
    万般无奈之下,小明想借助聪明的你,帮忙计算是否有机会离开这个迷宫,最少需要多少时间。(每一单位时间只能向四邻方向走一步)

    Input

    第一行为 T,表示输入数据组数。
    下面 T 组数据,对于每组数据:
    第一行是两个数字 n, m(2 < n * m <= 64),表示迷宫的长与宽。
    接下来 n 行,每行 m 个字符,‘.’表示空地可以通过,‘x’表示陷阱,‘*’表示机关,‘S’代表起点,‘E’代表出口,‘K’表示钥匙(保证存在且只有一个)。

    Output

    对第 i 组数据,输出
    Case #i:
    然后输出一行,仅包含一个整数,表示最少多少步能够拿到钥匙并走出迷魂阵,如果不能则打出-1。

    Sample Input

    5
    5 7
    ...*x..
    ...x...
    xEx....
    *x...K.
    .x*...S
    5 7
    K..*x..
    ...x...
    xEx....
    *x.....
    .x*...S
    5 7
    ..K*x..
    ..*x*..
    xEx....
    *x.....
    .x*...S
    5 7
    ..K*x..
    .*xx*..
    *E*....
    xx.....
    .x*...S
    4 4
    S*..
    **..
    ...E
    ...K
    View Sample Input

    Sample Output

    Case #1:
    11
    Case #2:
    13
    Case #3:
    13
    Case #4:
    11
    Case #5:
    -1

    Code

    状态压缩+BFS

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    #include<vector>
    
    typedef unsigned long long LL;
    
    using namespace std;
    
    int tx[]={-1,1,0,0};
    int ty[]={0,0,-1,1};
    int ii=0;
    
    struct datatype
    {
        int bs;
        LL zt1;
        LL zt2;
        int x;
        int y;
        datatype()
        {
            bs=0;
            zt1=0;
            x=0;
            y=0;
            zt2=0;
        }
    };
    queue<datatype> team;
    
    int maps[70][70];
    
    struct mapdata
    {
        int x,y;
        LL zt1;
        int zt2;
        LL count()
        {
            LL ans=zt1%10000;
            ans=(ans+zt2)%10000;
            ans=(ans+x)%10000;
            ans=(ans+y)%10000;
            return ans;
        }
        bool operator<(const mapdata &b)const
        {
            if(this->x!=b.x)
                return this->x<b.x;
            if(this->y!=b.y)
                return this->y<b.y;
            if(this->zt1!=b.zt1)
                return this->zt1<b.zt1;
            return this->zt2<b.zt2;
        }
        bool operator==(const mapdata &b)const
        {
            if(this->x!=b.x)
                return this->x == b.x;
            if(this->y!=b.y)
                return this->y == b.y;
            if(this->zt1!=b.zt1)
                return this->zt1==b.zt1;
            return this->zt2==b.zt2;
        }
    };
    
    char s[70][100];
    
    vector<mapdata> has[10010];
    
    void deal()
    {
        memset(s,0,sizeof(s));
        memset(maps,0,sizeof(maps));
        int n,m;
        while(!team.empty())
            team.pop();
        datatype pt;
        cin>>n>>m;
        for(int i=0;i<=10000;i++)
            has[i].clear();
        for(int i=1;i<=n;i++)
            scanf("%s",s[i]+1);
        int qdx,qdy;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(s[i][j]=='.') maps[i][j]=1;
                if(s[i][j]=='x') maps[i][j]=0;
                if(s[i][j]=='E') maps[i][j]=-666;
                if(s[i][j]=='*') maps[i][j]=-10;
                if(s[i][j]=='K') maps[i][j]=-66;
                if(s[i][j]=='S')
                {
                    maps[i][j]=1,qdx=i,qdy=j;
                    pt.bs=0,pt.zt1=0,pt.zt2=0,pt.x=qdx,pt.y=qdy;
                    team.push(pt);
                }
            }
        }
        datatype pd;
        int ans=-1;
        while(!team.empty())
        {
            pt=team.front();
            team.pop();
            if(maps[pt.x][pt.y]==-666)
            {
                if(pt.zt2==1)
                {
                    ans=pt.bs;
                    break;
                }
            }
            for(int i=0;i<4;i++)
            {
                pd.bs=pt.bs+1;
                pd.zt1=pt.zt1;
                pd.zt2=pt.zt2;
                pd.x=pt.x+tx[i];
                pd.y=pt.y+ty[i];
                if(pd.x<=0||pd.y<=0||pd.x>n||pd.y>m)
                    continue;
                LL cj=(LL)(m)*(pd.x-1)+pd.y;
                LL zt=((pd.zt1>>(LL)(cj-1))&(LL)1);
                if(zt==0&&maps[pd.x][pd.y]==0)
                    continue;
                if(zt==1)
                {
                    if(maps[pd.x][pd.y] != 0)
                        continue;
                }
                LL ws1,ws2,ws3,ws4;
                ws1=0;ws2=0;ws3=0;ws4=0;
                ws1=(pd.x-2)*(m)+pd.y;
                ws2=(pd.x)*m+(pd.y);
                ws3=(pd.x-1)*m+(pd.y-1);
                ws4=(pd.x-1)*m+(pd.y+1);
                if(maps[pd.x][pd.y]==-10)
                {
                    if(pd.x-1>0)
                    {
                        if((pd.zt1>>(ws1-(LL)1))&(LL)1)
                            pd.zt1=pd.zt1-((LL)1<<(ws1-(LL)1));
                        else
                            pd.zt1=pd.zt1+((LL)1<<(ws1-(LL)1));
                    }
                    if(pd.x+1<=n)
                    {
                        if((pd.zt1>>(ws2-(LL)1))&(LL)1)
                            pd.zt1=pd.zt1-((LL)1<<(ws2-(LL)1));
                        else
                            pd.zt1=pd.zt1+((LL)1<<(ws2-(LL)1));
                    }
                    if(pd.y-1>0)
                    {
                        if((pd.zt1>>(ws3-(LL)1))&(LL)1)
                            pd.zt1=pd.zt1-((LL)1<<(ws3-(LL)1));
                        else
                            pd.zt1=pd.zt1+((LL)1<<(ws3-(LL)1));
                    }
                    if(pd.y+1<=m)
                    {
                        if((pd.zt1>>(ws4-(LL)1))&(LL)1)
                            pd.zt1=pd.zt1-((LL)1<<(ws4-(LL)1));
                        else
                            pd.zt1=pd.zt1+((LL)1<<(ws4-(LL)1));
                    }
                }
                if(maps[pd.x][pd.y]==-66)
                {
                    pd.zt2=1;
                }
                mapdata h;
                h.x=pd.x;h.y=pd.y;h.zt1=pd.zt1;h.zt2=pd.zt2;
                int zzt=h.count();
                int vvv=0;
                for(int k=0;k<has[zzt].size();k++)
                {
                    if(has[zzt][k]==h)
                    {
                        vvv=1;
                        break;
                    }
                }
                if(vvv)
                    continue;
                has[zzt].push_back(h);
                team.push(pd);
            }
        }
        printf("Case #%d:
    ",ii);
        cout<<ans<<endl;
    }
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            ii++;
            deal();
        }
        return 0;
    }

    今夕何夕

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

    Problem Description

    今天是2017年8月6日,农历闰六月十五。
    小度独自凭栏,望着一轮圆月,发出了“今夕何夕,见此良人”的寂寞感慨。
    为了排遣郁结,它决定思考一个数学问题:接下来最近的哪一年里的同一个日子,和今天的星期数一样?比如今天是8月6日,星期日。下一个也是星期日的8月6日发生在2023年。
    小贴士:在公历中,能被4整除但不能被100整除,或能被400整除的年份即为闰年。

    Input

    第一行为T,表示输入数据组数。
    每组数据包含一个日期,格式为YYYY-MM-DD。
    1 ≤ T ≤ 10000
    YYYY ≥ 2017
    日期一定是个合法的日期

    Output

    对每组数据输出答案年份,题目保证答案不会超过四位数。

    Sample Input

    3
    2017-08-06
    2017-08-07
    2018-01-01

    Sample Output

    2023
    2023
    2024

    Code

    暴力枚举

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    
    using namespace std;
    
    typedef long long ll;
    
    bool isrun(int year)
    {
        if (year%4==0&&year%100!=0||year%400==0)
            return true;
        return false;
    }
    
    int a[10000][13][32];
    int month[]={29,31,28,31,30,31,30,31,31,30,31,30,31};
    
    int main()
    {
        memset(a,-1,sizeof(a));
        int p=0;
        for(int year=2017;year<=9999;year++)
        {
            bool run=isrun(year);
            for(int m=1;m<=12;m++)
            {
                if(run&&m==2)
                {
                    for(int d=1;d<=month[0];d++)
                    {
                        a[year][m][d]=p;
                        p++;
                        p%=7;
                    }
                    continue;
                }
                for(int d=1;d<=month[m];d++)
                {
                    a[year][m][d]=p;
                    p++;
                    p%=7;
                }
            }
        }
        int t;
        scanf("%d",&t);
        while (t--)
        {
            int y,m,d;
            scanf("%d-%d-%d",&y,&m,&d);
            for(int i=y+1;i<=9999;i++)
            {
                if(a[i][m][d]==a[y][m][d])
                {
                    printf("%d
    ",i);
                    break;
                }
            }
        }
        return 0;
    }

    度度熊的01世界

    Time Limit: 2000/1000 MS (Java/Others)   Memory Limit: 32768/32768 K (Java/Others)

    Problem Description

    度度熊是一个喜欢计算机的孩子,在计算机的世界中,所有事物实际上都只由0和1组成。
    现在给你一个n*m的图像,你需要分辨他究竟是0,还是1,或者两者均不是。
    图像0的定义:存在1字符且1字符只能是由一个连通块组成,存在且仅存在一个由0字符组成的连通块完全被1所包围。
    图像1的定义:存在1字符且1字符只能是由一个连通块组成,不存在任何0字符组成的连通块被1所完全包围。
    连通的含义是,只要连续两个方块有公共边,就看做是连通。
    完全包围的意思是,该连通块不与边界相接触。

    Input

    本题包含若干组测试数据。 每组测试数据包含: 第一行两个整数n,m表示图像的长与宽。 接下来n行m列将会是只有01组成的字符画。
    满足1<=n,m<=100

    Output

    如果这个图是1的话,输出1;如果是0的话,输出0,都不是输出-1。

    Sample Input

    32 32
    00000000000000000000000000000000
    00000000000111111110000000000000
    00000000001111111111100000000000
    00000000001111111111110000000000
    00000000011111111111111000000000
    00000000011111100011111000000000
    00000000111110000001111000000000
    00000000111110000001111100000000
    00000000111110000000111110000000
    00000001111110000000111110000000
    00000001111110000000011111000000
    00000001111110000000001111000000
    00000001111110000000001111100000
    00000001111100000000001111000000
    00000001111000000000001111000000
    00000001111000000000001111000000
    00000001111000000000000111000000
    00000000111100000000000111000000
    00000000111100000000000111000000
    00000000111100000000000111000000
    00000001111000000000011110000000
    00000001111000000000011110000000
    00000000111000000000011110000000
    00000000111110000011111110000000
    00000000111110001111111100000000
    00000000111111111111111000000000
    00000000011111111111111000000000
    00000000111111111111100000000000
    00000000011111111111000000000000
    00000000001111111000000000000000
    00000000001111100000000000000000
    00000000000000000000000000000000
    32 32
    00000000000000000000000000000000
    00000000000000001111110000000000
    00000000000000001111111000000000
    00000000000000011111111000000000
    00000000000000111111111000000000
    00000000000000011111111000000000
    00000000000000011111111000000000
    00000000000000111111110000000000
    00000000000000111111100000000000
    00000000000001111111100000000000
    00000000000001111111110000000000
    00000000000001111111110000000000
    00000000000001111111100000000000
    00000000000011111110000000000000
    00000000011111111110000000000000
    00000001111111111111000000000000
    00000011111111111111000000000000
    00000011111111111111000000000000
    00000011111111111110000000000000
    00000000001111111111000000000000
    00000000000000111111000000000000
    00000000000001111111000000000000
    00000000000111111110000000000000
    00000000000011111111000000000000
    00000000000011111111000000000000
    00000000000011111111100000000000
    00000000000011111111100000000000
    00000000000000111111110000000000
    00000000000000001111111111000000
    00000000000000001111111111000000
    00000000000000000111111111000000
    00000000000000000000000000000000
    3 3
    101
    101
    011
    View Sample Input

    Sample Output

    0
    1
    -1

    Code

    BFS判连通包含数量

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    
    #define ms(x,y) memset(x,y,sizeof(x))
    
    using namespace std;
    
    typedef long long ll;
    const int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
    
    struct node
    {
        int x, y;
        node(int p,int q)
        {
            x=p;
            y=q;
        }
    };
    
    int n,m;
    char a[110][110];
    bool book[110][110];
    
    int bfs(int x,int y,char c)
    {
        int bao=1;
        queue<node> que;
        que.push(node(x,y));
        book[x][y]=1;
        while(que.size())
        {
            node p=que.front();
            que.pop();
            for (int i=0;i<4;i++)
            {
                int px,py;
                px=p.x+dx[i];
                py=p.y+dy[i];
                if(px>=0&&px<n&&py>=0&&py<m)
                {
                    if(!book[px][py]&&a[px][py]==c)
                    {
                        book[px][py]=1;
                        que.push(node(px,py));
                    }
                }
                else
                    bao=0;
            }
        }
        return bao;
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            ms(book,0);
            ms(a,0);
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    scanf(" %c",&a[i][j]);
                }
            }
            int num0=0,num1=0;
            int bao1=0,bao0=0;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    if(!book[i][j])
                    {
                        if(a[i][j]=='0')
                        {
                            num0++;
                            bao1+=bfs(i,j,'0');
                        }
                        else
                        {
                            num1++;
                            bao0+=bfs(i,j,'1');
                        }
                    }
                }
            }
            if(num1==1&&bao1==0)
            {
                printf("1
    ");
            }
            else if(num1==1&&bao1==1)
            {
                printf("0
    ");
            }
            else
                printf("-1
    ");
        }
        return 0;
    }

    Time: 2017-08-12

  • 相关阅读:
    指针
    day07
    day06
    oracle instr
    动态解析dll及使用类
    客户端调用接口
    Java中调用WebService
    Vs2015智能提示英文
    oracle中varchar(32)转nvarchar(32)
    C#创建XML节点
  • 原文地址:https://www.cnblogs.com/SinGuLaRiTy2001/p/7350254.html
Copyright © 2011-2022 走看看