牛客练习赛31a

思路:从边界出发,对能走到的所有点进行计数,剩下的走不到的就是可以变颜色的了。

需要注意的是范围  题目上说了  m*n<1e6   那么有可能是 1*1e6    1e6*1  但是二维数组是没法开1e6*1e6

因此用vector来创建动态二维数组。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn=1e6+7;
struct node {
    int x1,y1;//当前位置坐标 
}q[maxn];
int t=1,w=1;
int n,m;
int bfs(vector<vector<char> > &a)
{
    while(t!=w){
        if(q[t].x1-1>=0&&a[q[t].x1-1][q[t].y1]=='.')//如果向上可以走的话 
		{//上
            q[w].x1=q[t].x1-1;
			q[w].y1=q[t].y1;
			w++;
			a[q[t].x1-1][q[t].y1]='#';
        }
        if(q[t].y1-1>=0&&a[q[t].x1][q[t].y1-1]=='.')
		{//左
            q[w].x1=q[t].x1;
			q[w].y1=q[t].y1-1;
			w++;
			a[q[t].x1][q[t].y1-1]='#';
        }
        if(q[t].x1+1<n&&a[q[t].x1+1][q[t].y1]=='.')
		{//下
            q[w].x1=q[t].x1+1;
			q[w].y1=q[t].y1;
			w++;
			a[q[t].x1+1][q[t].y1]='#';
        }
        if(q[t].y1+1<m&&a[q[t].x1][q[t].y1+1]=='.')
		{//右
            q[w].x1=q[t].x1;
			q[w].y1=q[t].y1+1;
			w++;
			a[q[t].x1][q[t].y1+1]='#';
        }
        t++;
    }
    return w-1;
}
char ch;
int main(){
    scanf("%d%d",&n,&m);
    vector<vector<char> >a(n+3);
    for(int i=0;i<n;i++){
        getchar();
        for(int j=0;j<m;j++){
            scanf("%c",&ch);
            a[i].push_back(ch);
        }
    }
    //把边界上的'.'加进队列去做bfs
    int sum=0;
    for(int i=0;i<m;i++)
	{
        if(a[0][i]=='.')
		{
			q[w].x1=0;
			q[w].y1=i;
			a[q[w].x1][q[w].y1]='#';
			w++;
		}
    }
    for(int i=0;i<m;i++)
	{//下
        if(a[n-1][i]=='.')
		{
			q[w].x1=n-1;
			q[w].y1=i;
			a[q[w].x1][q[w].y1]='#';
			w++;
		}
    }
    for(int i=0;i<n;i++)
	{//左
        if(a[i][0]=='.')
		{
			q[w].x1=i;
			q[w].y1=0;
			a[q[w].x1][q[w].y1]='#';
			w++;
		}
    }
    for(int i=0;i<n;i++){//右
        if(a[i][m-1]=='.')
		{
			q[w].x1=i;
			q[w].y1=m-1;
			a[q[w].x1][q[w].y1]='#';
			w++;
		}
    }
    printf("%d\n",n*m-bfs(a));
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值