B. Marlin(对称,设置障碍)

本文探讨了一个有趣的问题:如何在特定的城市网格中放置旅馆,确保两个村庄到达各自目的地的最短路径数量相等。通过巧妙的设计算法,实现了对于任意旅馆数量的有效布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Marlin
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
The city of Fishtopia can be imagined as a grid of
4
rows and an odd number of columns. It has two main villages; the first is located at the top-left cell
(
1
,
1
)
, people who stay there love fishing at the Tuna pond at the bottom-right cell
(
4
,
n
)
. The second village is located at
(
4
,
1
)
and its people love the Salmon pond at
(
1
,
n
)
.

The mayor of Fishtopia wants to place
k
hotels in the city, each one occupying one cell. To allow people to enter the city from anywhere, hotels should not be placed on the border cells.

A person can move from one cell to another if those cells are not occupied by hotels and share a side.

Can you help the mayor place the hotels in a way such that there are equal number of shortest paths from each village to its preferred pond?

Input
The first line of input contain two integers,
n
and
k
(
3

n

99
,
0

k

2
×
(
n

2
)
),
n
is odd, the width of the city, and the number of hotels to be placed, respectively.

Output
Print “YES”, if it is possible to place all the hotels in a way that satisfies the problem statement, otherwise print “NO”.

If it is possible, print an extra
4
lines that describe the city, each line should have
n
characters, each of which is “#” if that cell has a hotel on it, or “.” if not.

Examples
inputCopy
7 2
outputCopy
YES
…….
.#…..
.#…..
…….
inputCopy
5 3
outputCopy
YES
…..
.###.
…..
…..

#include <stdio.h>
#include <stdlib.h>
char a[6][100];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    int i,j;
    for(i=1; i<=4; i++)
    {
        for(j=1; j<=n; j++)
        {
            a[i][j]='.';
        }
    }
    if(k%2==0)
    {
        for(i=2; i<=n-1; i++)
        {
           if(k>=2)
           {
               a[2][i]='#';
               a[3][i]='#';
               k-=2;
           }
        }
    }
    else
    {
        a[2][n/2+1]='#';
        k--;
        for(i=n/2;i>=2;i--)
        {
            if(k>=2)
            {
                a[2][i]='#';
                a[2][n-i+1]='#';
                k-=2;
            }
        }
        for(i=2;i<=n/2;i++)
        {
            if(k>=2)
            {
               a[3][i]='#';
               a[3][n-i+1]='#';
               k-=2;
            }
        }
    }
    printf("YES\n");
    for(i=1;i<=4;i++)
    {
        for(j=1;j<=n;j++)
        {
            printf("%c",a[i][j]);
        }
        printf("\n");
    }
    return 0;
}

题意:给定4行矩阵,列数是奇数,在最左上角的人家要去最右下角吃鱼,最右上角的数要去最左下角吃鱼,现在在矩阵里面放旅馆,边上不能放,让我们求旅馆的放法,使得左下角到右上角,右下角到左上角的最短路径数一样多。

思路:我们就在纸上把左上角与右下角,左下右上连起来,主要要的是对称,所以如果宾馆数是偶数的话,那就竖着放,在2,3行上放,从左往右,肯定会把旅馆都放完的,不用考虑旅馆会不会有剩余,因为旅馆数数<=2*(n-2)的;当K是奇数的时候,再用刚才那种方法是不行了,因为最后肯定会剩一个,所以就先从中间放一个,然后往两边放,第2行放完了就放第3行,直到放完为止。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值