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行,直到放完为止。