题目链接:http://hihocoder.com/problemset/problem/1257

题目大意:有n条蛇 编号为1-n 每条蛇的长度跟编号相等 奇数编号的蛇必须拐奇数次(除了第一条)偶数编号的蛇必须拐偶数次(除了第二条)问能不能在这种约束条件下面用蛇构造成一个矩形

思路:构造题 肯定是有一种固定的构造方式,通过推可以发现矩形的长可以成为(n+1)/2 宽是n+((n&1)==0)

偶数很好做 因为直接在后面加就行了 奇数的时候可以发现通过前一个偶数和前一个奇数来向外面加一层

#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
int type[];
void pre(){
type[]=;
type[]=;
for(int n=;n<=;n++){
type[n]=type[n-]+;
}
}
bool judge(int n){
if(n<=){
if(n==){
printf("1 1\n");
return true;
}
if(n==){
printf("1 1\n1 2 1 3\n");
return true;
}
if(n==){
printf("2 1\n1 1 1 2\n1 3 2 3 2 2\n");
return true;
}
}
return false;
}
void print(int n,int row,int col){
if(n&){
for(int i=;i<(n+)/;i++){
printf("%d %d ",row+i,col);
}
for(int i=;i<=n/;i++){
printf("%d %d%c",row+(n+)/-,col-i,i==n/?'\n':' ');
}
}
else {
int tmp=n/;
for(int i=;i<tmp;i++){
printf("%d %d ",row,col-i);
}
for(int i=tmp-;i>=;i--){
printf("%d %d%c",row+,col-i,i==?'\n':' ');
}
}
}
void solve(int n,int row,int col){
if(judge(n)) return;
if(n&){
solve(n-,row,col-);
print(n-,row,col-);
print(n-,row+(n-)/,col-(n+)/);
print(n,row,col);
return;
}
else {
solve(n-,row,col-);
for(int i=;i<n/;i++){
printf("%d %d ",row+i,col);
}
for(int i=n/-;i>=;i--){
printf("%d %d%c",row+i,col-,i==?'\n':' ');
}
return ;
}
return ;
}
int main(){
int n;
pre();
while(scanf("%d",&n)!=EOF){
printf("%d %d\n",(n+)/,type[n]);
solve(n,,type[n]);
}
return ;
}
/*
133
223 13344
22344 22135
44335
44555
偶数:
*****nn
*****nn
*****nn
奇数:
* * * * n-2 n
* * * * n-2 n
* * * * n-2 n
n-1n-1n-1n-2n-2n
n-1n-1n-1n n n
*/

hiho1257 Snake Carpet的更多相关文章

  1. UVALive 7269 Snake Carpet (构造)

    题目:传送门. 题意:构造出一个矩阵,使得矩阵含有n条蛇,每条蛇的长度是1到n,并且奇数长度的蛇有奇数个拐弯,偶数长度 的蛇有偶数个拐弯. 奇数和偶数分开构造,奇数可以是: 1357 3357 555 ...

  2. 构造 hihocoder 1257 Snake Carpet (15北京I)

    题目传送门 题意:贪吃蛇,要求长度奇数的蛇转弯次数为正奇数,长度偶数转弯次数为正偶数,且组成矩形.(北大出的题咋都和矩形相关!!!) 分析:构造找规律,想到就简单了.可以构造 宽:(n + 1) / ...

  3. UVaLive 7269 Snake Carpet (找规律,模拟)

    题意:给定一个数字n,表示有n条蛇,然后蛇的长度是 i ,如果 i 是奇数,那么它只能拐奇数个弯,如果是偶数只能拐偶数个,1, 2除外,然后把这 n 条蛇, 放到一个w*h的矩阵里,要求正好放满,让你 ...

  4. 【hihocoder 1257 Snake Carpet】构造

    2015北京区域赛现场赛第4题. 题面:http://media.hihocoder.com/contests/icpcbeijing2015/problems.pdf OJ链接:http://hih ...

  5. hihoCoder 1257 Snake Carpet(很简单的构造方法)

    2015 ACM / ICPC 北京现场赛 I 题 构造 注意一个小坑,每条蛇的输出是要从头到尾输出的. 还要注意的是,不能开数组去模拟构造过程,然后输出,那样会TLE的. #include < ...

  6. UVALive - 7269 I - Snake Carpet

    思路: 多画画就发现从五的时候可以这么填: 六的时候这么填: 七的时候这么填: 看出规律了吗? 没看出的话再画画把. #include <bits/stdc++.h> using name ...

  7. [LeetCode] Design Snake Game 设计贪吃蛇游戏

    Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...

  8. Leetcode: Design Snake Game

    Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...

  9. 2101 Problem A Snake Filled

    题目描述 “What a boring world!”Julyed felt so bored that she began to write numbers on the coordinate pa ...

随机推荐

  1. Final Destination II -- 矩阵快速幂模板题

    求f[n]=f[n-1]+f[n-2]+f[n-3] 我们知道 f[n] f[n-1] f[n-2]         f[n-1]  f[n-2]  f[n-3]         1    1    ...

  2. Linux—vim常用命令

    vim常用命令: 1. 键入i进入编辑模式2. esc进入命令模式3. a,进入编辑模式3. b,光标移动到单词前,end,光标移动到行尾4. home光标移动到行首5. cc,删除当前行,并进入编辑 ...

  3. poj2104 主席树裸题

    空间大小:n*lgn 复杂度:建树n*lgn  查询lgn #include <cstdio> #include <iostream> #include <algorit ...

  4. hibernate 的sum(filed)引发的NullPointException错误解决过程

    背景: 在用hql语句进行sum查询时遭遇NPE问题: StringBuilder builder = new StringBuilder("select SUM(actualWorking ...

  5. pycharm设置pytest运行程序

  6. eclipse中不能保存汉字的解决方法

    首先分清是打开jsp页面的问题还是java文件的问题?    对于java文件,只要在你的项目上点击右键选择“Propertise”(属性)然后点击“Info”标签将里面的Text file enco ...

  7. PHPer未来路在何方...

    PHP 从诞生到现在已经有20多年历史,从Web时代兴起到移动互联网退潮,互联网领域各种编程语言和技术层出不穷, Node.js . GO . Python 不断地在挑战 PHP 的地位.这些技术的推 ...

  8. JDBC+Servlet+JSP的学生案例增删改查

    数据库信息传输到页面实现. 先进行学生信息页面展示: 接口IStudentDao public interface IStudentDao { /** * 保存操作 * @param stu 学生对象 ...

  9. linux下编译tex,bib成pdf文件

    参考linux下编译bib.tex生成pdf文件 为了编译出出正确的pdf文件,需要执行4条命令完成整个编译过程. 编译命令及输出 $ pdflatex bb.tex #目录下会生成bb.aux.bb ...

  10. K3CLOUD常用数据表

    一.数据库查询常用表 --查询数据表select * from ( select convert(varchar(4000),t1.FKERNELXML.query('//TableName')) a ...