请问此方法为何只能得54分,非常感谢
  • 板块P1331 海战
  • 楼主qq2415985142
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/10/16 20:30
  • 上次更新2023/11/2 13:23:40
查看原帖
请问此方法为何只能得54分,非常感谢
822097
qq2415985142楼主2023/10/16 20:30

想不出为什么,可能因为有某些情况没有想到,谢谢

//p1331
#include<bits/stdc++.h>
using namespace std;
int r,c;				//行与列
char maps[1010][1010];	//存放字符
int ans;				//计数
void dfs(int x,int y)	//深搜此坐标下的八个方位
{
    maps[x][y] = '.';	//为避免后续重复,赋值为海域
    for(int i=-1;i<=1;i++)
        for(int j=-1;j<=1;j++)//循环八个方位
        {
            int dx=x+i;
            int dy=y+j;
            if(dx>=0 && dx<r && dy>=0 && dy<c && maps[dx][dy]=='#')//判断每个方位下是否还能继续深搜
            {
                dfs(dx,dy);
            }
        }   
}
int main()
{
    cin>>r>>c;
    for(int i=0;i<r;i++)
        for(int j=0;j<c;j++)
        {
            cin>>maps[i][j];
        }
    for(int i=0;i<r;i++)
        for(int j=0;j<c;j++)//遍历每个点
        {
            if(maps[i][j]=='#')//如果是船则深搜
            {
                dfs(i,j);//如果深搜结束则船附近所有相连接的#已替换
                ans++;
            }
        }
    if(ans==0)
        cout<<"Bad placement.";
    else
        printf("There are %d ships.",ans);
    return 0;
}

谢谢

2023/10/16 20:30
加载中...