第六个点超空间了 不知道怎么办 大佬们救救呀
  • 板块P1331 海战
  • 楼主KOBEDAVIS
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/6/18 15:18
  • 上次更新2023/10/27 23:05:19
查看原帖
第六个点超空间了 不知道怎么办 大佬们救救呀
504560
KOBEDAVIS楼主2022/6/18 15:18
#include <bits/stdc++.h>

using namespace std;

const int N = 1010;
char mp[N][N];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int n, m;
int ant = 0;
struct Pos
{
    int x;
    int y;
};

bool cheak()
{
    int cnt = 0;
    for (int i = 0; i < n - 1; ++i)
    {
        for (int j = 0; j < m - 1; ++j)
        {
            if (mp[i][j] == '#')
                cnt++;
            if (mp[i + 1][j] == '#')
                cnt++;
            if (mp[i][j + 1] == '#')
                cnt++;
            if (mp[i + 1][j + 1] == '#')
                cnt++;

            if (cnt == 3) return true;
            else cnt=0;   
        }
    }

    return false;
}
void bfs(Pos p)
{
    queue<Pos> que;
    que.push(p);
    ++ant;
    mp[p.x][p.y] = '.';
    while (!que.empty())
    {
        auto pf = que.front();

        int sx = pf.x, sy = pf.y;
        mp[sx][sy] = '.';
        que.pop();
        for (int i = 0; i < 4; ++i)
        {
            int nx = sx + dx[i], ny = sy + dy[i];

            if (nx >= 0 && nx < n && ny >= 0 && ny < m && mp[nx][ny] == '#')
            {
                mp[nx][ny] == '.';
                que.push(Pos{nx, ny});
                mp[sx][sy] = '.';
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
            cin >> mp[i][j];

    if (cheak())
    {
        printf("Bad placement.");
    }
    else
    {
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < m; ++j)
            {
                if (mp[i][j] == '#')
                {
                    bfs(Pos{i, j});
                }
                // cout << ant << " ";
            }
            // cout << "\n";
        }

        printf("There are %d ships.", ant);
    }
    return 0;
}
2022/6/18 15:18
加载中...