1 条题解

  • 0
    @ 2024-12-24 9:59:30

    C++ :

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    char g[1010][1010];
    int r,c;
    bool vis[1010][1010];
    int up,down,left,right;
    int block,ans;
    bool judge(int x,int y){
    	if(x<0||x>=r||y<0||y>=c) return false;
    	if(g[x][y]=='.'||vis[x][y]==true) return false;
    	return true;
    }
    int ex[4][2]={1,0,-1,0,0,1,0,-1};
    void dfs(int x,int y){
    	vis[x][y]=true;
    	block++;
    	if(x<up) up=x;
    	if(x>down) down=x;
    	if(y<left) left=y;
    	if(y>right) right=y;
    	// printf("%d*%d\n",x,y);
    	for(int i=0;i<4;i++){
    		int nowx=x+ex[i][0];
    		int nowy=y+ex[i][1];
    		if(judge(nowx,nowy)){
    			dfs(nowx,nowy);
    		}
    	}
    }
    int main(){
    	scanf("%d %d",&r,&c);
    	getchar();
    	for(int i=0;i<r;i++){
    		for(int j=0;j<c;j++){
    			scanf("%c",&g[i][j]);
    		}
    		getchar();
    	}
    	ans=0;
    	for(int i=0;i<r;i++){
    		for(int j=0;j<c;j++){
    			if(judge(i,j)==true){
    				block=0;up=2000;down=-10;left=2000;right=-10;
    				dfs(i,j);
    				ans++;
    				int temp=(down-up+1)*(right-left+1);
    				if(temp!=block){
    					printf("Bad placement.\n");
    					return 0;
    				}
    			}
    		}
    	}
    	printf("There are %d ships.\n",ans);
    	return 0;
    }
    
    • 1

    信息

    ID
    1666
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者