暑期个人赛--第一场--D

时间限制 1000 ms  内存限制 65536 KB

题目描述

给定一个 NM 的矩阵,求问里面有多少个由'#'组成的矩形,"There are 5 ships.",若是里面有一个不是矩形的联通块,则输出"So Sad"

输入格式

1n,m1000

有多组数据,EOF结束。

输出格式

每行对应一个answer

输入样例

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

输出样例

There are 5 ships.
So Sad

赛中提交:T


赛后ac:Y


题目大意:在矩阵里面找出独立的由#组成的区域个数


思路:

裸的广搜


赛中为何不过:

真心不懂- -赛后直接复制赛中提交的再提交一次一样的就过了

代码如下


#include "iostream"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "string"
#include "vector"
#include "list"
#include "map"
#include "queue"
#include "stack"
#include "bitset"
#include "algorithm"
#include "numeric"
#include "functional"
 
using namespace std;
typedef pair<int ,int > P;
 
int n,m;
int u,d,l,r,counta,countaa;
char c[1005][1005];
int vis[1005][1005];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
 
void bfs(int bx,int by)
{
    P p,tempt;
    queue<P> que;
    tempt.first=bx,tempt.second=by;
    que.push(tempt);
    vis[bx][by]=1;
    while(que.size()){
        int x,y;
        counta+=1;
        tempt=que.front();
        que.pop();
        x=tempt.first,y=tempt.second;
        //printf("\n\nque.size()=%d  取%d %d\n",que.size(),x,y);
        l=min(y,l);
        r=max(y,r);
        u=max(x,u);
        d=min(x,d);
        //printf("上%d下%d左%d右%d\n",u,d,l,r);
 
        for(int i=0;i<4;i+=1){
            int nx=x+dx[i],ny=y+dy[i];
            if(nx>=0&&ny>=0&&nx<n&&ny<m&&c[nx][ny]!='.'&&vis[nx][ny]==0){
                tempt.first=nx,tempt.second=ny;
                que.push(tempt);
                vis[nx][ny]=1;
            }
        }
    }
}
 
int main()
{
    while(scanf("%d %d",&n,&m)!=EOF){
        getchar();
        int F=0;
        countaa=0;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i+=1){
            for(int j=0;j<m;j+=1){
                scanf("%c",&c[i][j]);
            }
            getchar();
        }
 
        for(int i=0;i<n;i+=1){
            bool flag=0;
            for(int j=0;j<m;j+=1){
                //    printf("(%d,%d)",i+1,j+1);
                if(!vis[i][j]&&c[i][j]=='#'){
                    u=d=i;
                    r=l=j;
                    counta=0;
                    bfs(i,j);
                    if(counta!=(u-d+1)*(r-l+1)){
                        F=1;
                        //printf("hhhhh(u%d-d%d+1)*(r%d-l%d+1)=%d  counta=%d\n",u,d,r,l,(u-d+1)*(r-l+1),counta);
                        break;
                    }
                    else{
                        countaa+=1;
                    }
                }
                //printf("dfasfd%d %d\n",i+1,j+1);
            }
            if(F){
                //    printf("\n\ncome and eat shit\n\n");
                break;
            }
        }
        if(F){
            printf("So Sad\n");
        }
        else{
            printf("There are %d ships.\n",countaa);
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值