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;
    }
  • 相关阅读:
    对Spring的简单理解
    对Hibernate的简单认识
    对Struts的简单理解
    浅谈实体类
    xdebug配置
    hosts文件修改完无效的解决办法
    CentOS6.4 中文输入法
    python加密解密
    windows运行命令大全
    vm虚拟机centos文件共享目录设置
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6221247.html
Copyright © 2011-2022 走看看