P1331Help
  • 板块P1331 海战
  • 楼主Merron
  • 当前回复9
  • 已保存回复9
  • 发布时间2020/7/24 18:07
  • 上次更新2023/11/6 22:23:48
查看原帖
P1331Help
338370
Merron楼主2020/7/24 18:07

RT

为什么第二个点WA

Input:

6 8
.....#.#
##.....#
##.....#
.......#
#......#
#..#...#

Correct Output:

There are 5 ships.

My OutPut:

There are 10 ships.

其他点都没问题。

代码如下:


#include <bits/stdc++.h>
using namespace std ;

int n, m ;
char sea[1010][1010] ;
bool vis[1010][1010] ;
int ans ;
int dx[4] = {0, 0, 1, -1} ;
int dy[4] = {1, -1, 0, 0} ;

struct node
{
    int x ;
    int y ;
} ;

inline bool check(int x, int y){return (x > 0 && x <= n && y > 0 && y <= n ) ;}


bool OK (int x, int y)
{
    int sum = 0 ;
    if (sea[x][y] == '#') sum ++ ;
    if (sea[x + 1][y] == '#') sum ++ ;
    if (sea[x][y + 1] == '#') sum ++ ;
    if (sea[x + 1][y + 1] == '#') sum ++ ;
    if (sum == 3) return 1 ;
    return 0 ;
}

void bfs (int x, int y)
{
    //sea[x][y] = '*' ;
    vis[x][y] = 1 ;
    queue<node> q ;
    node t ;
    t.x = x ;
    t.y = y ;
    q.push(t) ;

    while(!q.empty())
    {
        int X = q.front().x ;
        int Y = q.front().y ;
        q.pop() ;
        for (int i = 0 ;i < 4 ;i ++)
        {
            int nx = X + dx[i] ;
            int ny = Y + dy[i] ;
            if (!vis[nx][ny] && sea[nx][ny] == '#' && check(nx, ny))
            {
                vis[nx][ny] = 1 ;
                sea[nx][ny] = '.' ;
                node T ;
                T.x = nx ;
                T.y = ny ;
                q.push(T) ;
            }
        }
    }
}

int main ()
{
    cin >> n >> m ;
    for (int i = 1 ;i <= n ;i ++)
    {
        for (int j = 1 ;j <= m ;j ++)
        {
            cin >> sea[i][j] ;
        }
    }

    for (int i = 1 ;i <= n ;i ++)
    {
        for (int j = 1 ;j <= m ;j ++)
        {
            if (i < n && j < m && OK(i, j))
            {
                cout << "Bad placement." << endl ;
                return 0 ;
            }
        }
    }
    
    for (int i = 1; i <= n ;i ++)
    {
        for (int j = 1; j <= m ;j ++)
        {
            if(sea[i][j] == '#' && vis[i][j] == 0)
            {
                ans ++ ;
                bfs (i, j) ;
            }
        }
    }
    cout << "There are " << ans << " ships." << endl ;
    return 0 ;
}

求大神指点!

2020/7/24 18:07
加载中...