POJ 1856 Sea Battle

题意:找出R*C的图中不想交的矩形个数,如果有一个相交矩形就输出Bad placement. 不然就输出能放多少船

不相交是说对于一个矩形(‘#’组成)周围一圈都是由‘.’包围,所以我们只用求出以某个点为起点的最大和最小的x,y的值,并且在搜索时‘#’的个数rec,如果rec=(maxx-minx+1)*(maxy-miny+1)成立,则这个矩形合法

#include <iostream>
#include<stdio.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 1010
int maxx,maxy,minx,miny;
using namespace std;
char map[N][N];
int r,c;
bool check(int x,int y)
{
    if(x>=0&&x<r&&y>=0&&y<c&&map[x][y]=='#')
    return true;
    return false;
}
int dfs(int x,int y)
{
    minx=min(minx,x);
    maxx=max(maxx,x);
    miny=min(miny,y);
    maxy=max(maxy,y);
    map[x][y]='.';

    int rec=1;
    for(int i=-1;i<=1;i++)
    for(int j=-1;j<=1;j++)
    {
        if(check(i+x,j+y))
        {
            rec+=dfs(i+x,j+y);
        }
    }
    return rec;
}
int main()
{

    while(~scanf("%d%d",&r,&c))
    {
        if(r==0&&c==0) break;

        for(int i=0;i<r;i++)
        scanf("%s",map[i]);

        int ans=0;

        for(int i=0;i<r;i++)
        for(int j=0;j<c;j++)
        {
            if(map[i][j]=='#')
            {
                maxx=minx=i;
                maxy=miny=j;
                int rec=dfs(i,j);
                if(rec==(maxx-minx+1)*(maxy-miny+1))
                      ans++;
                      else
                      {
                          ans=-1;//只要有一艘坏船就是bad
                          i=r;
                          break;
                      }
            }
        }
        if(ans==-1)
          printf("Bad placement.\n");
          else
          printf("There are %d ships.\n",ans);
    }
    return 0;
}

POJ 1856 Sea Battle

时间: 2024-08-04 01:37:38

POJ 1856 Sea Battle的相关文章

POJ 1856 Sea Battle(DFS)

题意  图中每个矩形'#'连通块代表一艘船  若一艘船与另一艘有边相邻或有角相邻  那么认为这两艘船相撞   若图中有船相撞  输出bad  否则输出图中有多少艘船 可以把图的周围全包上一圈'.'  遍历图中每个点  可知当图中存在一下四种结构中的一个时  必有船相撞   输出并退出循环  否则则dfs这个点  若图中不存在这些结构   就可以输出连通块数量即轮船数了 #include<cstdio> #include<cstring> using namespace std; c

POJ 1856 Sea Battle(BFS).

~~~~ 题意: 给你一个R*C的图,求其由图中连通'#"所组成的矩形的个数. 注意:If the ships were placed correctly (i.e., there are only rectangles that do not touch each other even with a corner), print the sentence "There are S ships." where S is the number of ships. Otherwi

POJ 1856 Sea Battle(dfs)

Description During the Summit, the armed forces will be highly active. The police will monitor Prague streets, the army will guard buildings, the Czech air space will be full of American F-16s. Moreover, the ships and battle cruisers will be sent to

POJ 题目2312 Battle City(BFS)

Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7208   Accepted: 2427 Description Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are d

CodeForces 738D Sea Battle

抽屉原理. 先统计最多有$sum$个船可以放,假设打了$sum-a$枪都没打中$a$个船中的任意一个,那么再打$1$枪必中. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include

poj1856Sea Battle(DFS)

题目链接: huangjing 思路: 这个题目当时想到是找联通快,但是不知道怎么判断这个联通快是不是标准的好船,后来看了别人的题解才知道可以用面积去判断...这个知道了就是简单的dfs找联通快了..注意是如果出现一艘破船则不用找了,直接输出就可以了... 题目: Sea Battle Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2809   Accepted: 996 Description During the S

codeforces Technocup 2017 - Elimination Round 2/Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) 题解

久违的下午场,打了一场状态不错一下rank12涨了207~~~ A. Interview with Oleg 题意: 给你一个长度不超过100的串,把其中ago开头后面不接或者接gogogo...的部分全部变成*** 思路: 水水,扫 /* *********************************************** Author :devil ************************************************ */ #include <cstdi

CF 729D 模拟,思维

CF 729 D. Sea Battle 题意:n个单位长的海域,有a只船,船长为b.已经开了k枪没有射中,求最少再开几枪可射中一只船. 题解:转变一下思维,求开了k枪后可放入多少只船.要求再开几枪可射中一只船,只要 -a+1即可. #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define FF(i,a,b) for (

Codeforces Round #541 (Div. 2) (A~F)

目录 Codeforces 1131 A.Sea Battle B.Draw! C.Birthday D.Gourmet choice(拓扑排序) E.String Multiplication(思路) F.Asya And Kittens(链表) G.Most Dangerous Shark Codeforces 1131 比赛链接 hack一个暴力失败了两次最后还是没成功身败名裂= = CF跑的也太快了吧... 不过倒也涨了不少. A.Sea Battle //想麻烦了,但是无所谓... #