HDU 1241 油田

这道题明明很简单但不知道为什么运行结果一直错,但提交却是对的!代码真是神奇,不过我猜测可能是提上给出的数据错了,可能提上给的数据m和n后多给了一个空格或回车,但题的数据没有

#include<stdio.h>
#include<string.h>
#define N 110
int m, n;
char maps[N][N];
int dir[8][2]={{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};

void DFS(int x, int y);

int main()
{
    while(scanf("%d%d", &m, &n), m!=0)
    {
        int ans=0;
        for(int i=0; i<m; i++)
        {
            getchar();
            for(int j=0; j<n; j++)
            {
                scanf("%c", &maps[i][j]);
            }
        }

        for(int i=0; i<m; i++)
            for(int j=0; j<n; j++)
            {
                if(maps[i][j]=='@')
                {
                    DFS(i, j);
                    ans++;
                }
            }

        printf("%d
", ans);
    }
}

void DFS(int x, int y)
{
    maps[x][y]='*';
    for(int i=0; i<8; i++)
    {
        int nx=x+dir[i][0];
        int ny=y+dir[i][1];

        if(maps[nx][ny]=='@'&&nx>=0&&nx<m&&ny>=0&&ny<n)
            DFS(nx, ny);

    }
}

  

原文地址:https://www.cnblogs.com/wazqWAZQ1/p/4782874.html