P1331 海战:求助!!
  • 板块学术版
  • 楼主_Timeless_
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/7/19 17:06
  • 上次更新2023/10/27 19:30:53
查看原帖
P1331 海战:求助!!
556110
_Timeless_楼主2022/7/19 17:06
#include <iostream>

using namespace std;

int a[1005][1005], m, n, cnt = 0;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {-1, 1, 0, 0};

void dfs(int x, int y)
{
    a[x][y] = 0;
    for(int i = 0; i < 4; i++)
    {
        int tx = x + dx[i];
        int ty = y + dy[i];
        if(tx > 0 && tx <= m && ty > 0 && ty <=n && a[tx][ty])
        {
            //a[tx][ty] = 0;
            dfs(tx, ty);
        }
    }
}

bool d(int i,int j)
{
    int c = 0;
    if(a[i][j])  c++;
    if(a[i+1][j])  c++;
    if(a[i][j+1])  c++;
    if(a[i+1][j+1])  c++;
    if(c == 3)  return 0;
    return 1;
}

int main()
{
    cin >> m >> n;
    char ch;
    getchar();
    for(int i = 1; i <= m; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            ch = getchar();
            if(ch == '.')
            {
                a[i][j] = 0;
            }
            else
            {
                a[i][j] = 1;
            }
        }
        getchar();
    }
    for(int i = 1; i <= m; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(i < m && j < n && !d(i, j))
            {
                cout << "Bad placement.";
                return 0;
            }
        }
    }
    for(int i = 1; i <= m; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(a[i][j])
            {
//              a[i][j] = 0;
                dfs(i, j);
                cnt++;
            }
        }
    }
    cout << "There are " << cnt << " ships.";
    return 0;
 } 

各位大佬,各路神仙,帮忙一看呗~~

2022/7/19 17:06
加载中...