Codeforces 980B(思维)
题面:
The city of Fishtopia can be imagined as a grid of 44 rows and an odd number of columns. It has two main villages; the first is located at the top-left cell (1,1)(1,1), people who stay there love fishing at the Tuna pond at the bottom-right cell (4,n)(4,n). The second village is located at (4,1)(4,1)and its people love the Salmon pond at (1,n)(1,n).
The mayor of Fishtopia wants to place kk 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?
The first line of input contain two integers, nn and kk (3≤n≤993≤n≤99, 0≤k≤2×(n−2)0≤k≤2×(n−2)), nn is odd, the width of the city, and the number of hotels to be placed, respectively.
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 44 lines that describe the city, each line should have nn characters, each of which is "#" if that cell has a hotel on it, or "." if not.
7 2
YES ....... .#..... .#..... .......
5 3
YES ..... .###. ..... .....
题面描述:
给你一个4*n的矩阵,A村庄在左上角的点(1,1),它要到达右下角C点(4,n),B村庄在左下角(4,1),它要达到右上角D(1,n)。先我们要建k个旅馆,要求所建的旅店的位置不能改变A到C的最短路径的数量以及B到D的最短路径。要求你输出满足条件的矩阵。
题目分析:
因为A到C和B到D的最短距离必定为他们之间的曼哈顿距离。而要满足所有最短路的数量都相同,则酒店所设立的位置必定是要将整个图关于x轴或者y轴对称。
对于这个题目,因为题目要求,我们不能在边界上建立酒店,因此我们只能在大小为2*(n-2)的矩阵中建立酒店。而题目所给的k数据范围是[0,(n-2)*2]的,因此不难发现对于这道题来说,只要合理安排酒店的位置,必定是可以将k个酒店都安排好的。
于是,我们进一步观察分析,可以知道,对于这个2*(n-2)的矩阵,要使得图关于x或者y轴对称,则当k为偶数的时候,只需要不断填充第2行第3行即可;而当k为奇数的时候,我们只需要在这个2*(n-2)的矩阵的中心开始加点,并从两边往中间加酒店即可。
代码:
#include <bits/stdc++.h>
using namespace std;
string str[5];
int main()
{
int n,k;
cin>>n>>k;
for(int i=0;i<4;i++){
for(int j=0;j<n;j++){
str[i]+='.';
}
}
if(k%2==0){
k/=2;
for(int i=1;i<=2;i++){
for(int j=1;j<=k;j++){
str[i][j]='#';
}
}
}
else{
str[1][n/2]='#';
if(k!=1){
int tmp=n/2;
if(k<=n-2){
for(int i=1;i<=(k-1)/2;i++){
str[1][i]='#';
str[1][n-1-i]='#';
}
}
else{
for(int i=1;i<=n-2;i++) str[1][i]='#';
k-=n-2;
for(int i=1;i<=k/2;i++){
str[2][i]='#';
str[2][n-1-i]='#';
}
}
}
}
puts("YES");
for(int i=0;i<4;i++){
cout<<str[i]<<endl;
}
}