CFB. Madoka and Underground Competitions
题目:
Madoka decided to participate in an underground sports programming competition. And there was exactly one task in it:
A square table of size n×nn×n, where nn is a multiple of kk, is called good if only the characters '.' and 'X' are written in it, as well as in any subtable of size 1×k1×k or k×1k×1, there is at least one character 'X'. In other words, among any kk consecutive vertical or horizontal cells, there must be at least one containing the character 'X'.
Output any good table that has the minimum possible number of characters 'X', and also the symbol 'X' is written in the cell (r,c)(r,c). Rows are numbered from 11 to nn from top to bottom, columns are numbered from 11 to nn from left to right.
The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases. Description of the test cases follows.
The first and the only line of each test case contains four integers nn, kk, rr, cc (1≤n≤500,1≤k≤n,1≤r,c≤n1≤n≤500,1≤k≤n,1≤r,c≤n) — the size of the table, the integer kk and the coordinates of the cell, which must contain the character 'X'. It is guaranteed that nn is a multiple of kk.
It is guaranteed that the sum of nn over all test cases does not exceed 500500.
For each test case, output nn lines, each consisting of nn characters '.' and 'X', — the desired table. If there are several answers, then you can output anyone.
题目大意就是给定一个二维字符数组,元素只有'x'和'.',给定一个c,r,表示s[c][r]是'X',给定一个k,要求无论哪个位置,用1*k或k*1的小方块去套,里面都至少包含一个'X'
解法:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#define x first
#define y second
using namespace std;
const int N=510;
char s[N][N];
int n,k,r,c;
typedef pair<int,int> PII;
int t;
vector<PII>p;
int main(){
cin>>t;
while(t--){
p.clear();
cin>>n>>k>>r>>c;
memset(s,'.',sizeof s);
s[r][c]='X';
p.push_back({r,c});//记录最开始的每个'X'的坐标
for(int i=r+1,j=c+1;j<=n;i++,j++){//向左移动
if(i>n) i=1;
s[i][j]='X';
p.push_back({i,j});
}
for(int i=r-1,j=c-1;j>0;i--,j--){//向右移动
if(i<1) i=n;
s[i][j]='X';
p.push_back({i,j});
}
for(int i=0;i<p.size();i++){//对于每一行的X,向左右两边搜寻
for(int j=p[i].y;j<=n;j+=k) s[p[i].x][j]='X';
for(int j=p[i].y;j>0;j-=k) s[p[i].x][j]='X';
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<s[i][j];
}
cout<<endl;
}
}
}
//贪心的思想,即X是的数字最小,我们由给定的起点开始,错位分别左移和右移,因为是n行n列,做到每一行都有一个X
//因为X的位置最优,即任意相邻的两个X相聚不能超过k,