E. Road to 1600(构造,打表)

63 篇文章 1 订阅
11 篇文章 0 订阅

在这里插入图片描述
题意:
构造方格1~n*n,皇后和车的走法如图,皇后和车会走当前能走的位置中没走过的值最小的来走,如果没有花费就会加1并且跳到整个图中没走过中的最小的方格来走,让你构造一个矩阵,使得车的花费比皇后少。
思路:
n<=2显然是无解的,n=3可以通过打表打出来,然后想想怎么拓展,我们可以通过走蛇形矩阵的方法,先蛇形递增的摆放食物诱导皇后和车同轨迹走,最后让车和皇后走到n,1或者1,n,(取决于n的奇偶性),然后皇后会跳进子问题n=3的陷阱,那么皇后就输了。
代码:

// Problem: E. Road to 1600
// Contest: Codeforces - Codeforces Round #632 (Div. 2)
// URL: https://codeforces.com/contest/1333/problem/E
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC target("avx")
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize("Ofast")
// created by myq 
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long ll;
#define x first
#define y second
typedef pair<int,int> pii;
const int N = 510;
int a[N][N];
const int mod=998244353;
inline int read()
{
	int res=0;
	int f=1;
	char c=getchar();
	while(c>'9' ||c<'0')
	{
		if(c=='-')	f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		res=(res<<3)+(res<<1)+c-'0';
		c=getchar(); 
	}
	return res;
 } 
 
int main() 
{ 
	int n;
	cin>>n;
	if(n<=2)	puts("-1");
	else if(n==3)
	{
		int eps=0;
		a[1][1]=1+eps;
		a[1][2]=7+eps;
		a[1][3]=9+eps;
		a[2][1]=3+eps;
		a[2][2]=2+eps;
		a[2][3]=5+eps;
		a[3][1]=4+eps;
		a[3][2]=8+eps;
		a[3][3]=6+eps;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
				printf("%d ",a[i][j]);
			cout<<endl;
		}
	}
	else
	{
		int sx=4;
		int sy=1;
		int len=4;
		int now=2;
		int eps=1;
		a[4][1]=1;
		while(true)
		{
			while(sy+1<=len)
			{
				a[sx][++sy]=now++;
				eps++;
				
			}
			while(sx-1>=1)
			{
				a[--sx][sy]=now++;
				eps++;
			}
			sy++;
			len++;
			if(eps==n*n-9)
			break;
			eps++;
			a[sx][sy]=now++;
			while(sx+1<=len)
			{
				a[++sx][sy]=now++;
				eps++;
			}
			while(sy-1>=1)
			{
				a[sx][--sy]=now++;
				eps++;
			}
			if(eps==n*n-9)
			break;
			len++;
			sx++;
			eps++;
			a[sx][sy]=now++;
		}
		a[1][1]=1+eps;
		a[1][2]=7+eps;
		a[1][3]=9+eps;
		a[2][1]=3+eps;
		a[2][2]=2+eps;
		a[2][3]=5+eps;
		a[3][1]=4+eps;
		a[3][2]=8+eps;
		a[3][3]=6+eps;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
				printf("%d ",a[i][j]);
			cout<<endl;
		}
		
	}
	return 0;
	
}
/**
* In every life we have some trouble
* When you worry you make it double
* Don't worry,be happy.
**/



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值