(sgu-344)Weed

47 篇文章 0 订阅

Problem Description

Andrew has visited his garden for the last time many years ago. Today’s property taxes are so high, so Andrew decided to sell his garden. The land was not cultivated for a long time and now it is probably a lot of weed on it. Andrew wants to remove everything from the ground before selling. Now he wants to estimate the amount of work.

The garden has the rectangular form and is divided into equal squares. Andrew’s memory is phenomenal. He remembers which squares were occupied by the weed. For the purpose of simplicity, Andrew thinks that each square is either fully occupied by the weed or completely free from it. Andrew likes botany and he knows that if some square is free from the weed but at least two of its adjacent squares are occupied by the weed (two squares are adjacent if they have common side), that square will be also occupied by the weed soon. Andrew is pretty sure that during last years weed occupied every square possible. Please help Andrew to estimate how many squares is occupied by the weed.

Input
The first line of the input contains integers N and M (1 ≤ N, M ≤ 1000). Next N lines contain M characters each. Character X denotes that the corresponding square is occupied by the weed. A period character (
.) denotes an empty square.

Output
Print one integer denoting the number of squares occupied by the weed after so many years.

Example(s)
sample input
sample output
3 3 X.. .X. .X.
6

sample input
sample output
3 4 X..X .X.. .X..
12

题意:给定N,M后面是一个N*M的矩阵,’X’代表杂草,’.’代表空地,若空地的至少两个相邻位置是’X’,那么这块空地将充满杂草,问最终会有多少杂草?

分析:明显的DFS
但是写出来了,始终AC不了,找了半天,试各种不同的数据,没发现问题,然后把输入的scanf()改成cin就过了。。。 被坑了半天、、、、

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1005;
int vis[N][N],dxy[4][2]= {0,1,1,0,0,-1,-1,0};
int m,n,ans;
char a[N][N];
void dfs(int x,int y)
{
    int cnt=0;
    if(a[x][y]=='X') return ;///剪枝
    for(int i=0; i<4; i++)
    {
        int nx=x+dxy[i][0],ny=y+dxy[i][1];
        if(nx>=0&&nx<n&&ny>=0&&ny<m&&a[nx][ny]=='X')
            cnt++;
    }
    if(cnt>=2)///周围有两块地是'X'
    {
        ans++;
        a[x][y]='X';
        for(int i=0; i<4; i++)
            dfs(x+dxy[i][0],y+dxy[i][1]);
    }
    else return ;
}
int main()
{
    //freopen("E:/in.txt","r",stdin);
    while(~scanf("%d%d",&n,&m))
    {
        ans=0;
        for(int i=0; i<n; i++)
        {
            cin>>a[i];///**不能用scanf()  !!!**
            for(int j=0; j<m; j++)
                if(a[i][j]=='X')
                    ans++;
        }
        //memset(vis,0,sizeof(vis));
        /*for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
                printf("%c",a[i][j]);
            printf("\n");
        }*/
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                if(a[i][j]=='.')
                    dfs(i,j);
        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值