hdu 6113 度度熊的01世界(结构体的赋值问题)

题目大意:

输入n*m的字符串矩形,判断里面的图形是1还是0,还是什么都不是

注意:结构体中放赋值函数,结构体仍旧能定义的写法

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<cmath>
#include<cstring>
#include<queue>
//#include<bits/stdc++.h>
using namespace std;
struct node
{
    int x,y;
    node(int a=0,int b=0):x(a),y(b) {}  //这里point
};
int n,m,one,zero;
int dr[4][2]= {{1,0},{0,1},{-1,0},{0,-1} };
char ch[105][105];
bool vis[105][105];
bool ok(int x,int y)
{
    if (x>=0 && x<=n && y>=0 && y<=m) return 1;
    return 0;
}
void bfs(int sx,int sy)
{
    queue<node> Q;
    Q.push(node(sx,sy));
    vis[sx][sy]=1;
    while(!Q.empty())
    {
        node u=Q.front();
        node v;
        Q.pop();
        for(int i=0; i<4; i++)
        {
            v.x=u.x+dr[i][0];
            v.y=u.y+dr[i][1];
            if (!ok(v.x,v.y)) continue;
            if (vis[v.x][v.y]) continue;
            if (ch[u.x][u.y]==ch[v.x][v.y])
            {
                Q.push(v);
                vis[v.x][v.y]=1;
            }
        }
    }
    return;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
                cin>>ch[i][j];
        for(int j=0; j<=m+1; j++)
        {
            ch[0][j]='0';
            ch[n+1][j]='0';
        }
        for(int i=0; i<=n+1; i++)
        {
            ch[i][0]='0';
            ch[i][m+1]='0';
        }
        n++;
        m++;
        memset(vis,0,sizeof(vis));

        zero=0;
        one=0;
        for(int i=0; i<=n; i++)
            for(int j=0; j<=m; j++)
            {
                if (vis[i][j]==1) continue;
                if (ch[i][j]=='0')
                {
                    zero++;
                    bfs(i,j);
                }
                else
                {
                    one++;
                    bfs(i,j);
                }

            }
        // printf("%d %d
",one,zero);
        if (one==1 && zero==2) printf("0
");
        else if (one==1 && zero==1) printf("1
");
        else printf("-1
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/stepping/p/7363611.html