hiho1257 Snake Carpet
题目链接: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的更多相关文章
- UVALive 7269 Snake Carpet (构造)
题目:传送门. 题意:构造出一个矩阵,使得矩阵含有n条蛇,每条蛇的长度是1到n,并且奇数长度的蛇有奇数个拐弯,偶数长度 的蛇有偶数个拐弯. 奇数和偶数分开构造,奇数可以是: 1357 3357 555 ...
- 构造 hihocoder 1257 Snake Carpet (15北京I)
题目传送门 题意:贪吃蛇,要求长度奇数的蛇转弯次数为正奇数,长度偶数转弯次数为正偶数,且组成矩形.(北大出的题咋都和矩形相关!!!) 分析:构造找规律,想到就简单了.可以构造 宽:(n + 1) / ...
- UVaLive 7269 Snake Carpet (找规律,模拟)
题意:给定一个数字n,表示有n条蛇,然后蛇的长度是 i ,如果 i 是奇数,那么它只能拐奇数个弯,如果是偶数只能拐偶数个,1, 2除外,然后把这 n 条蛇, 放到一个w*h的矩阵里,要求正好放满,让你 ...
- 【hihocoder 1257 Snake Carpet】构造
2015北京区域赛现场赛第4题. 题面:http://media.hihocoder.com/contests/icpcbeijing2015/problems.pdf OJ链接:http://hih ...
- hihoCoder 1257 Snake Carpet(很简单的构造方法)
2015 ACM / ICPC 北京现场赛 I 题 构造 注意一个小坑,每条蛇的输出是要从头到尾输出的. 还要注意的是,不能开数组去模拟构造过程,然后输出,那样会TLE的. #include < ...
- UVALive - 7269 I - Snake Carpet
思路: 多画画就发现从五的时候可以这么填: 六的时候这么填: 七的时候这么填: 看出规律了吗? 没看出的话再画画把. #include <bits/stdc++.h> using name ...
- [LeetCode] Design Snake Game 设计贪吃蛇游戏
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- Leetcode: Design Snake Game
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- 2101 Problem A Snake Filled
题目描述 “What a boring world!”Julyed felt so bored that she began to write numbers on the coordinate pa ...
随机推荐
- Leetcode 153. Find Minimum in Rotated Sorted Array -- 二分查找的变种
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- Kickstart Practice Round 2017---A
Problem The Constitution of a certain country states that the leader is the person with the name con ...
- Achievements
看了Suma,觉得懂了85%以上. 两个月可以学这么多.方法是不懂的就学就行了. 最近学了:字符串,网络流,线段树,斯特林反演,多项式与生成函数,一些数论等.
- 我们为什么要使用List和Set(List,Set详解)
1.集合概述 类图 集合和数组的区别? 集合基本方法 集合特有的遍历方式? public static void main(String[] args) { //创建集合对象 Collection c ...
- linux之常见错误
在日常开发中,尤其是在Linux中进行操作的时候,经常会碰到各种各样的错误.记录一下,熟能生巧,慢慢参透linux的奥秘 1) 在安装ssl证书的时候,发生certbot命令无法使用的情况 解决方案: ...
- 手机移动端input date placehoder不显示
要解决这个问题,我们可以伪造一个placehoder,通过css跟js来解决这个问题. 为什么要用js的原因是因为当你选择了时间之后,placehoder的文字没有清除掉,所以我们就需要把这个伪造的p ...
- js-cookie和session
###1.cookie 含义: 存储在访问者的计算机中的变量,即存储在客户端 创建一个cookie /* getCookie方法判断document.cookie对象中是否存有cookie,若有则判断 ...
- MySQL dump文件导入
1 打开cmd 输入要导入的数据库,用户名,密码,dump文件路径 mysql -u employees <E:\employees_db\load_departments.dump
- [转帖]国产紫光SSD不再只是实验室展品 开始批量出货
国产紫光SSD不再只是实验室展品 开始批量出货 https://www.cnbeta.com/articles/tech/825865.htm 没听说有做HDD的 现做了SSD 弯道超车吗 可以实现全 ...
- Latex常用软件
Linux texMaker sudo apt-get install texlive-full sudo apt-get install texmaker