zoukankan      html  css  js  c++  java
  • POJ 1979 Red and Black

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    #define MAXN 21
    struct node
    {
        int x;
        int y;
    };
    int n,m,g[MAXN][MAXN];
    bool vis[MAXN][MAXN];
    int x[4]={1,-1,0,0};
    int y[4]={0,0,1,-1};
    int bfs(int sx,int sy)
    {
        int ans = 0;
        node temp;
        temp.x = sx;
        temp.y = sy;
        vis[sx][sy] = true;
        queue<node> q;
        q.push(temp);
        while(!q.empty())
        {
            temp = q.front();
            q.pop();
            //cout<<temp.x<<' '<<temp.y<<endl;
            ans++;
            for(int i=0;i<4;i++)
            {
                int tx = temp.x + x[i];
                int ty = temp.y + y[i];
                if((tx>=0&&ty>=0&&tx<m&&ty<n)&&!vis[tx][ty]&&g[tx][ty]=='.')
                {
                    vis[tx][ty] = true;
                    node nn;
                    nn.x = tx;
                    nn.y = ty;
                    q.push(nn);
                }
            }
        }
        return ans;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m))
        {
            getchar();
            if(n+m==0) break;
            int sx,sy;
            memset(g,0,sizeof(g));
            memset(vis,false,sizeof(vis));
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<n;j++)
                {
                    scanf("%c",&g[i][j]);
                    if(g[i][j]=='@')
                    {
                        sx = i;
                        sy = j;
                    }
                }
                getchar();
            }
            cout<<bfs(sx,sy)<<endl;
        }
        return 0;
    }
  • 相关阅读:
    5G名词术语
    什么是IPv6
    如何用SecureCRT 登录eNSP模拟器里的设备
    numpy-排序
    numpy-tile 数组复制
    经纬度计算距离与方位角
    numpy-添加操作大全
    高效编程之 concurrent.future
    linux命令 集合
    PostgreSQL-表空间
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6221247.html
Copyright © 2011-2022 走看看