Codeforces Round #818 (Div. 2) B. Madoka and Underground Competitions

这篇博客介绍了一种解决网络格子问题的方法,即在给定一个'X'坐标后,如何构建最少数量的'X',使得任何k个连续的垂直或水平单元格中至少有一个'X'。作者通过深度优先搜索策略,从给定的'X'位置出发,向上和向下扩展,确保每k个单元格内存在'X'。代码示例展示了该算法的实现细节。
摘要由CSDN通过智能技术生成

Problem - B - Codeforces

 

 

 

 题意:给你一个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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值