博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
zoj 3823 Excavator Contest 构造
阅读量:6649 次
发布时间:2019-06-25

本文共 4304 字,大约阅读时间需要 14 分钟。

Excavator Contest

Time Limit: 1 Sec  

Memory Limit: 256 MB

题目连接

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3823

Description

Bluefly University is famous of excavator technology. Lots of students take part in many excavator-related courses. After the students finish their courses, they will compete in a contest called International Collegiate Excavator Contest (ICEC).

 

An excavator

 

This year's ICEC will be held at Marjar University. This is an individual competition that each contestant will start the match one by one.

The task of the contest is to drive an excavator passing a square field. The judge partitioned the field into N × N equal-sized square chunks. Each chunk should be visited exactly one time. The contestant will drive the excavator, starting from and ending at the center of two different boundary chunks.

In order to show off their superb excavator operating skills, the contestants need to drive the excavator with as many as possible turnings. Since the excavator is a kind of large and heavy vehicle, it can only make a turn to left or right at the center of any chunk.

Bob is a student from Marjar University. He wants to win the contest. To fulfill this dream, he needs to drive the excavator with at least N× (N - 1) - 1 turnings. It seems to be a difficult task, so he turns to you for help. Please write a program to find a feasible route for him.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

There is only one integer N (2 <= N <= 512).

Output

For each test case, output a matrix with N × N integers indicating the route to pass all the chunks. Bob will drive the excavator passing the chunks in the order of 1, 2, .., N2.

If there are multiple solutions, any one will be acceptable.

Sample Input

243
 

Sample Output

2 1 16 153 4 13 146 5 12 117 8 9 101 2 38 7 49 6 5

HINT

 

题意

给你一个n*n个格子,然后让你跑,使得起点和终点都必须在边界上

而且使得转弯次数至少是n*(n-1)-1次

题解:

分奇数偶数构造

大概偶数特别容易想

奇数比较麻烦,奇数在我的构造方式下,要转圈圈

反正就非常迷就是了……

代码:

//qscqesze#pragma comment(linker, "/STACK:1024000000,1024000000")#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef long long ll;using namespace std;//freopen("D.in","r",stdin);//freopen("D.out","w",stdout);#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)#define maxn 100006#define mod 1000000007#define eps 1e-9#define e exp(1.0)#define PI acos(-1)const double EP = 1E-10 ;int Num;//const int inf=0x7fffffff;const ll inf=999999999;inline ll read(){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}//*************************************************************************************int A[600][600];int B[600][600];int main(){ int t;scanf("%d",&t); while(t--) { int n;scanf("%d",&n); if(n%2==0) { int nowx= 1,nowy = 1; int now = 1; while(now<=n*n) { A[nowy][nowx]=now; if(nowy<=n-2) { if(nowx%4==1) { if(nowy%2==1) nowx=nowx+1; else nowy=nowy+1; } else if(nowx%4==2) { if(nowy%2==1) nowy=nowy+1; else nowx=nowx-1; } else if(nowx%4==3) { if(nowy%2==1) nowx=nowx+1; else nowy=nowy-1; } else { if(nowy==1) nowx=nowx+1; else if(nowy%2==1) nowy=nowy-1; else nowx=nowx-1; } } else { if(nowx%4==1) { if(nowy==n-1) nowy=nowy+1; else nowx=nowx+1; } else if(nowx%4==2) { if(nowy==n-1) nowx=nowx+1; else nowy=nowy-1; } else if(nowx%4==3) { if(nowy==n-1) nowy=nowy+1; else nowx=nowx+1; } else { nowy=nowy-1; } } now++; } } else { A[1][1]=1; int nowx = 1; int nowy = 1; int now = 2; for(int i=3;i<=n;i+=2) { for(int ii=1;ii<=i-2;ii++) { for(int jj=1;jj<=i-2;jj++) { B[ii][jj]=A[ii][jj]; } } for(int ii=1;ii<=i-2;ii++) { for(int jj=1;jj<=i-2;jj++) { A[ii][jj]=(i-2)*(i-2)+1-B[i-1-jj][ii]; } } nowx = 1,nowy = i-1; while(now<=i*i) { A[nowx][nowy]=now; if(nowx<=i-2) { if(nowx%2==1) { if(nowy==i-1) nowy=nowy+1; else nowx=nowx+1; } else { if(nowy==i) nowy=nowy-1; else nowx=nowx+1; } } else { if(nowx==i-1) { if(nowy%2==0) nowy=nowy-1; else nowx=nowx+1; } else { if(nowy%2==0) nowx=nowx-1; else nowy=nowy-1; } } now++; } } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { printf("%d ",A[i][j]); } printf("\n"); } }}

 

转载地址:http://uvuto.baihongyu.com/

你可能感兴趣的文章
[OSGI Felix ] Intellij Idea 15 中开发 Maven osgi 项目(Apache felix环境)
查看>>
hdu 1879 继续通畅工程(最小生成树)
查看>>
Storm Topology的并发度
查看>>
hibernate回滚事务
查看>>
我的友情链接
查看>>
禁用USB
查看>>
Linux文本处理---文件属性查找
查看>>
umount 时出现的 "Device is busy"问题
查看>>
mac装windows遇到的问题总结
查看>>
OPENDNS的地址不能用,劫持DNS
查看>>
云舒网络:容器系列二:容器的视角-设计交付和架构
查看>>
Ubuntu中安装mysql-python失败的解决方法
查看>>
Linux文件系统和目录的作用
查看>>
springmvc+mybatis+spring 整合 bootstrap
查看>>
aix 文件大小相关查询
查看>>
cgroup
查看>>
hadoop
查看>>
keepalived
查看>>
《Linux菜鸟入门2》系统定时延时任务
查看>>
局域网共享故障的分析与排除
查看>>