E 地、颜色、魔法

7 篇文章 0 订阅
3 篇文章 0 订阅

题目戳我

本题开字符串数组肯定开不出二维的,所以考虑开一个vector数组存,1表示.,0表示#,然后就是dfs了,因为是其他点到边界,然后如果我们枚举其他点到边界的话绝对要超时,所以要转变思想。从边界枚举出去,然后如果这个点可达那么他的ve[x][y]++;这样时间复杂度就降下来了,但是还是不够的,那么现在就要减枝叶了,如果ve[x][y]>1的话说明已经有边界可达了,那么就直接return ,如果该点是#也就是说ve[x][y]==0,那么也return ;

在这里插入代码片#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<deque>
#include<map>
#include<stdlib.h>
#include<set>
#include<iomanip>
#include<stack>
#define ll long long
#define ms(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x & -x
#define fi first
#define ull unsigned long long
#define se second
#define endl "\n"
#define bug cout<<"----acac----"<<endl
#define IOS ios::sync_with_stdio(false), cin.tie(0),cout.tie(0)
using namespace std;
const int maxn =1e6+10;
const int maxm = 1.5e5+50;
const double eps = 1e-18;
const double inf = 0x3f3f3f3f;
const double  lnf  = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const  double pi=3.141592653589;
vector<int>ve[maxn];
char s[maxn];
 int n,m;
int dir[][2]={{-1,0},{1,0},{0,-1},{0,1}};
bool check(int i,int j)
{
    if(i==0||j==0||i==n-1||j==m-1)return true;
    else return false;
}
void dfs(int x,int y)
{
    if(ve[x][y]==0||ve[x][y]>1)return ;
    ve[x][y]++;
    for(int i=0;i<4;i++)
    {
        int dx=x+dir[i][0];
        int dy=y+dir[i][1];
        if(dx<0||dy<0||dx>=n||dy>=m)continue;
        dfs(dx,dy);
    }

}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
    { //bug;
       scanf("%s",s+1);
       for(int j=1;j<=m;j++)
       {

           if(s[j]=='.')
           {
               ve[i].push_back(1);
           }

           else
           {
               ve[i].push_back(0);
           }
       }
    }
       for(int i=0;i<n;i++)
       {
           for(int j=0;j<m;j++)
           {
               if(check(i,j)&&ve[i][j])dfs(i,j);
           }
       }//-----bug;
       int ans=0;
       for(int i=0;i<n;i++)
       {
           for(int j=0;j<m;j++)
           {
               if(ve[i][j]>1)ans++;
           }
       }
       printf("%d\n",n*m-ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值