

题意:给你一个n*n的网络格子,然后给你一个坐标点,这个点的位置是'X',问你怎么构造尽可能少的'X',使任何k个连续的垂直或水平单元格中,必须至少有一个包含字符“X”。
思路:因为每个数据都会给一个为'X'的点,那么我们就要用到他,并且尽可能用最少的点来满足条件,那么我们就以题目中给的点,然后往上下两边构造一个点出来,然后构造出来的每个点都进行每k次的遍历在当前行中建点,注意在构造的时候可能会超范围,但是题目数据原因可以超,只要满足n*n内的点满足条件就好了。
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long
using namespace std;
const int N=1000000+100;
int n ,m,c,r;
int k ;
ll s[N];
int Map[1010][1010];
bool pan(int x,int y)
{
if(x>=1&&y>=1)return 1;
return 0;
}
void dfs(int x,int y )
{
int xx=x,yy=y;
while(yy+k<=n)
{
yy+=k;
if(pan(xx,yy))
Map[xx][yy]=1;
}
xx=x,yy=y;
while(yy-k>=1)
{
yy-=k;
if(pan(xx,yy))
Map[xx][yy]=1;
}
}
int main()
{
int t;
sc_int(t);
while(t--)
{
memset(Map,0,sizeof Map);
cin>>n>>k>>r>>c;
int x=r,y=c;
int dx=-1,dy=1;//往上走
while(x>=1)//走到第一个点
{
x+=dx,y+=dy;
Map[x][y]=1;
dfs(x,y);
}
dx=1,dy=-1;
while(x<=n)//走到第一个点
{
x+=dx,y+=dy;
if(pan(x,y))
Map[x][y]=1;
dfs(x,y);
}
for(int i =1;i<=n;i++)
{
for(int j =1;j<=n;j++)
if(Map[i][j])cout<<"X";
else cout<<".";
cout<<endl;
}
}
return 0;
}
这篇博客介绍了一种解决网络格子问题的方法,即在给定一个'X'坐标后,如何构建最少数量的'X',使得任何k个连续的垂直或水平单元格中至少有一个'X'。作者通过深度优先搜索策略,从给定的'X'位置出发,向上和向下扩展,确保每k个单元格内存在'X'。代码示例展示了该算法的实现细节。
1323

被折叠的 条评论
为什么被折叠?



