2018/7/20 CF#476 B. Battleship

题目描述

Arkady is playing Battleship. The rules of this game aren't really important.

There is a field of n×nn×n cells. There should be exactly one kk-decker on the field, i. e. a ship that is kk cells long oriented either horizontally or vertically. However, Arkady doesn't know where it is located. For each cell Arkady knows if it is definitely empty or can contain a part of the ship.

Consider all possible locations of the ship. Find such a cell that belongs to the maximum possible number of different locations of the ship.

输入格式

The first line contains two integers nn and kk (1≤k≤n≤1001≤k≤n≤100) — the size of the field and the size of the ship.

The next nn lines contain the field. Each line contains nn characters, each of which is either '#' (denotes a definitely empty cell) or '.' (denotes a cell that can belong to the ship).

输出格式

Output two integers — the row and the column of a cell that belongs to the maximum possible number of different locations of the ship.

If there are multiple answers, output any of them. In particular, if no ship can be placed on the field, you can output any cell.

样例

input

4 3
#..#
#.#.
....
.###

output

3 2

input

10 4
#....##...
.#...#....
..#..#..#.
...#.#....
.#..##.#..
.....#...#
...#.##...
.#...#.#..
.....#..#.
...#.#...#

output

6 1

input

19 6
##..............###
#......#####.....##
.....#########.....
....###########....
...#############...
..###############..
.#################.
.#################.
.#################.
.#################.
#####....##....####
####............###
####............###
#####...####...####
.#####..####..#####
...###........###..
....###########....
.........##........
#.................#

output

1 8

Note

The picture below shows the three possible locations of the ship that contain the cell (3,2)(3,2) in the first sample.

题目大意

有一片海域,‘#’代表位置已被占据,‘.’则代表该地方没有障碍物(可以放船只)。给你海域大小n*n,船只长度k,以及这篇海的情况。要你输出一个位置,船只在占据这个位置的情况下,船只可以摆放的情况最多。

解题思路:暴力模拟

先将船只横放,走遍整片海域,如果发现放得下,就在船只占据的位置都+1,然后将船只竖着放,也走一遍。最后遍历整片海域,看看哪个地方的数最大,答案即为那个位置。

代码实现

#include <iostream>
#include<cstdio>
#include<cstring>

using namespace std;

bool field[105][105];
int ship[105][105];
int n, k;
int maxi = 0, maxj = 0;

void solve()
{
    memset(ship,0,sizeof(ship));
    int blank = 0;
    int i, j, l;
    for(i = 0; i < n; i++)
    {
        blank = 0;
        for( j = 0; j < n; j++)
        {
            if(field[i][j] == 0)
                blank++;
            if(field[i][j] == 1)
                blank = 0;
            if(blank >= k)
            {
                for(l = j-k+1;l<=j;l++ )
                    ship[i][l]++;
            }
        }
    }
    for(j = 0; j < n; j++)
    {
        blank = 0;
        for( i = 0; i < n; i++)
        {
            if(field[i][j] == 0)
                blank++;
            if(field[i][j] == 1)
                blank = 0;
            if(blank >= k)
            {
                for(l = i-k+1;l<=i;l++ )
                    ship[l][j]++;
            }
        }
    }

    int maxx = ship[0][0];
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < n; j++)
        {
            if(ship[i][j]>maxx)
            {
                maxx = ship[i][j];
                maxi = i;
                maxj = j;
            }
        }
    }
}

int main()
{
    scanf("%d%d", &n, &k);
    int i, j;
    char c;
    getchar();
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < n; j++)
        {
            scanf("%c", &c);
            if(c == '.')
                field[i][j] = 0;
            if(c == '#')
                field[i][j] = 1;
        }
        getchar();
    }
    solve();
    printf("%d %d\n", maxi+1, maxj+1);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值