首页 > 代码库 > neerc

neerc

bfs

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
const int dx[]={-1,1,0,0},dy[]={0,0,-1,1};
queue<int>q;
int n,m,ans,x,y;
int used[1010][1010];
char board[1010][1010];
void bfs(int a,int b)
{
    q.push(a);q.push(b);
    used[a][b]=1;
    while(!q.empty())
    {
        int x=q.front();q.pop();
        int y=q.front();q.pop();
        for(int i=0;i<4;i++)
        {
            int xx=x+dx[i];
            int yy=y+dy[i];
            if(board[xx][yy]==-)
            {
                ans++;break;
            }
        }        
        for(int i=0;i<4;i++)
        {
            int xx=x+dx[i],yy=y+dy[i];
            if(!used[xx][yy]&&board[xx][yy]==+)
            {
                q.push(xx);q.push(yy);used[xx][yy]=1;
            }
        }
    }
}
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    cin>>n>>m;
    memset(used,1,sizeof(used));
    memset(board,1,sizeof(board));
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            cin>>board[i][j];
            used[i][j]=0;
            if(board[i][j]==+)
            {
                x=i;y=j;
            }
        }
    bfs(x,y);
    cout<<ans<<endl;
    fclose(stdin);
    fclose(stdout);
    return 0;
}

 

neerc