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 guard the banks of
the Vltava river. Unfortunately, in the case of any incident, the Czech Admiralty have only a few captains able to control over the large sea battle. Therefore, it was decided to educate new admirals. As an excellent preparation, the game of "Sea Battle" was
chosen to help with their study program.

In this well-known game, a predefined number of ships of predefined shapes are placed on the square board in such a way that they cannot contact one another even with their corners. In this task, we will consider rectangular shaped ships only. The unknown number
of rectangular ships of unknown sizes are placed on a rectangular board. All the ships are full rectangles built of hash characters. Write a program that counts the total number of ships present in the field.

Input

The input consists of more scenarios. The description of each scenario begins with two integer numbers R and C separated with a single space, 1 <= R,C <= 1000. These numbers give the number of rows and columns in the game field.

After these two numbers, there are R lines, each of them containing C characters. Each character is either hash ("#") or dot ("."). Hashes denote ships, dots water.

Then, the next scenario description begins. At the end of the input, there will be a line containing two zeros instead of the field size.

Output

Output a single line for every scenario. 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.

Otherwise, print the sentence "Bad placement.".

Sample Input

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

Sample Output

Bad placement.
There are 5 ships.

Source

CTU Open 2002

#

题意:就是#必须是矩阵,即为长方形或正方形,还有每个#矩形的角不能共着,如第一个图中的##  因为他们有一个点共着了,而题目问的是只要有供着的,就输出

Bad placement.

否则输出矩形个数

比赛时没有好的思路,纠结好久,后来看别人代码,碉堡了,每次看到#,就dfs,记录搜到的最大x,y,最小x,y,和#的个数  temp  ,if(temp==(maxx-minx+1)*(maxy-miny+1)),那么就是满足条件的,否者不满足输出

Bad placement.

不说了,上代码了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 1005

char a[N][N];

int n,m;
int minx,maxx,miny,maxy;

int temp;

void dfs(int x,int y)
{
    a[x][y]='.';
    temp++;

    int i,j;
    minx=min(minx,x);
    maxx=max(maxx,x);

    miny=min(miny,y);
    maxy=max(maxy,y);

    for(i=-1;i<=1;i++)
        for(j=-1;j<=1;j++)
    {
        int xx=x+i;
        int yy=y+j;
        if(xx>=0&&xx<n&&yy>=0&&yy<m&&a[xx][yy]=='#')
            dfs(xx,yy);
    }
}
int main()
{
    int i,j;
    while(scanf("%d%d",&n,&m),n+m)
    {
        for(i=0;i<n;i++)
            scanf("%s",a[i]);

        int ans=0,flag=0;

        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
            if(a[i][j]=='#')
            {
                minx=maxx=i;
                miny=maxy=j;
                temp=0;
                dfs(i,j);

                if(temp==(maxx-minx+1)*(maxy-miny+1))
                   ans++;
                   else
                {
                    flag=1;
                    i=n;
                    j=m;
                }
            }

            if(flag)
                printf("Bad placement.\n");
            else
                printf("There are %d ships.\n",ans);
    }
    return 0;
}

POJ 1856 Sea Battle(dfs)

时间: 2024-10-27 03:15:59

POJ 1856 Sea Battle(dfs)的相关文章

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

题意:找出R*C的图中不想交的矩形个数,如果有一个相交矩形就输出Bad placement. 不然就输出能放多少船 不相交是说对于一个矩形('#'组成)周围一圈都是由'.'包围,所以我们只用求出以某个点为起点的最大和最小的x,y的值,并且在搜索时'#'的个数rec,如果rec=(maxx-minx+1)*(maxy-miny+1)成立,则这个矩形合法 #include <iostream> #include<stdio.h> #include<string> #incl

poj1856Sea Battle(DFS)

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

POJ 1699 Best Sequence(DFS)

題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做的 1 //1699 2 #include <iostream> 3 #include <stdio.h> 4 #include <string.h> 5 #include <string> 6 7 using namespace std; 8 9 string

POJ 3009-Curling 2.0(DFS)

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12158   Accepted: 5125 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is

POJ 2251 Dungeon Master(dfs)

Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot m

[ACM] POJ 3740 Easy Finding (DFS)

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16202   Accepted: 4349 Description Given a M×N matrix A. Aij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), could you find some rows that let every cloumn contains and only contains one 1. Input There a

POJ 1321-棋盘问题(DFS)

棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23338   Accepted: 11567 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. Input 输入含有多组测试数据. 每组数据的第一行是两个正整数,n k,用一个空格隔开,表示