灌水法,有问题!
  • 板块P1331 海战
  • 楼主only_matthew
  • 当前回复10
  • 已保存回复10
  • 发布时间2021/9/8 22:02
  • 上次更新2023/11/4 07:15:21
查看原帖
灌水法,有问题!
421080
only_matthew楼主2021/9/8 22:02
#include <cstdio>
const int MAXN = 1000 + 5;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int n, m, ans;
char a[MAXN][MAXN];
void dfs(int x, int y)
{
    a[x][y] = '.';
    for (int i = 0; i < 4; i++)
    {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx >= 0 && nx < n && ny >= 0 && ny < m && a[nx][ny] == '#')
            dfs(nx, ny);
    }
}
int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            scanf("%c", &a[i][j]);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (a[i][j] == '#')
            {
                ans++;
                dfs(i, j);
            }
    if (n)
        printf("%s %d %s", "There are ",ans," ships");
    else
        printf("%s", "Bad placement.");
}
2021/9/8 22:02
加载中...