著名的折纸问题:给你一张很大的纸,对折以后再对折,再对折……每次对折都是从右往左折,因此在折了很多次以后,原先的大纸会变成一个窄窄的纸条。现在把这个纸条沿着折纸的痕迹打开,每次都只打开“一半”,即把每个痕迹做成一个直角,那么从纸的一端沿着和纸面平行的方向看过去,会看到一条美妙的曲线。

就是一个分形,规定一下,纸的朝向,然后不难发现规律。

实现方面,可以迭代也可递归。

在图形输出方面,存下x和y坐标,然后下标排个序,或者用map。

//Rey 2015.8.3
#include<bits/stdc++.h>
using namespace std; // r -> rl -> open -> ru
// -> rlrl -> open -> r ud l -> open -> ru lu
// -> rlrlrlrl -> open -> r ud lr ud l -> open -> ru ludr dl -> rulu ldlu
// -> rlrlrlrlrlrlrlrl -> r ud lr ud lr ud lr ud l -> ru const int maxn = ;
const int maxL = (<<)+;
int dir[maxL];
int Rotate[maxL];
int r[maxL];
int x[maxL],y[maxL];
// up right down left
// 0 1 2 3
int dx[] = {-,,, };
int dy[] = { ,,,-};
int mx[] = { ,,, };
int my[] = { ,,,-};
char decode[] = {'|','_','|','_'}; bool cmp(int a,int b) { return x[a] < x[b] || ( x[a] == x[b] && y[a] < y[b] ); } void solve(int n)
{
int N = (<<n);
// fold
for(int i = , tmp[] = {,}; i < N; i++) dir[i] = tmp[i&];
memset(Rotate,,sizeof(int)*N);
//open
//mark
for(int i = ; i <= n; i++) {
int seg = <<i;
for(int j = <<(i-); j < N; j += seg<<){
for(int k = ; k < seg && j+k < N; k++)
Rotate[j+k]++;
}
} //rotate and calculate position
int minx = ,miny = ;
x[] = y[] = ;
for(int i = ; i < N; i++){
int &u = dir[i] , u2 = dir[i-];
u = (u + Rotate[i])%;
x[i] = x[i-] + dx[u2] + mx[u] ;
y[i] = y[i-] + dy[u2] + my[u] ;
minx = min(minx,x[i]);
miny = min(miny,y[i]);
}
//normalize
for(int i = ; i < N; i++){
x[i] -= minx;
y[i] -= miny;
} for(int i = ; i < N; i++) r[i] = i;
sort(r,r+N,cmp); int prex = ,prey = -;
for(int i = ; i < N; i++){
int id = r[i];
if(x[id] != prex) putchar('\n'),prey = -;
for(int j = prey+; j < y[id]; j++) putwchar(' ');
putchar(decode[dir[id]]);
prey = y[id]; prex = x[id];
}
printf("\n^\n");
} int main()
{
// freopen("out.txt","w",stdout);
int n;
while(scanf("%d",&n),n){
solve(n);
}
return ;
}

UVA 177 PaperFolding 折纸痕 (分形,递归)的更多相关文章

  1. 折纸问题java实现

    /** * 折纸问题 这段代码写的太low了 本人水平有限 哎... 全是字符串了 * @param n * @return * @date 2016-10-7 * @author shaobn */ ...

  2. CSS3实现文字折纸效果

    CSS3实现文字折纸效果 效果图: 代码如下,复制即可使用: <!DOCTYPE html> <html> <head> <title></tit ...

  3. 嵊州D5T2 折纸 folding

    折纸 folding [问题描述] 在非常紧张的 NOIP 考试中,有人喜欢啃指甲,有人喜欢转铅笔,有人喜欢撕 纸条,……而小 x 喜欢迷折纸. 现有一个 W * H 的矩形纸张,监考老师想知道,小 ...

  4. 折纸(folding)

    问题 C: 折纸 时间限制: 1 Sec  内存限制: 128 MB[提交] [状态] 题目描述 现有一个W*H的矩形纸张,求至少要折多少次才能使矩形纸张变成w*h的矩形纸张.注意,每次的折痕都要平行 ...

  5. CSS3写折纸

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  6. 【BZOJ】1074: [SCOI2007]折纸origami

    http://www.lydsy.com/JudgeOnline/problem.php?id=1074 题意:一开始有一个左上角是(0,100),右下角是(100,0)的纸片,现在可以沿有向直线折n ...

  7. 1074: [SCOI2007]折纸origami - BZOJ

    Description 桌上有一张边界平行于坐标轴的正方形纸片,左下角的坐标为(0,0),右上角的坐标为(100,100).接下来执行n条折纸命令.每条命令用两个不同点P1(x1,y1)和P2(x2, ...

  8. 1074: [SCOI2007]折纸origami

    Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 372  Solved: 229[Submit][Status][Discuss] Descriptio ...

  9. 紫书 习题8-5 UVa 177 (找规律)

    参考了https://blog.csdn.net/weizhuwyzc000/article/details/47038989 我一开始看了很久, 拿纸折了很久, 还是折不出题目那样..一脸懵逼 后来 ...

随机推荐

  1. ASP.NEt ajax 弹出窗口在页面无法关闭

    <meta http-equiv="X-UA-Compatible" content="IE=9" />     今天又有客戶投訴公司的網頁有問題. ...

  2. UIButton和UINavigationItem设置图片和文字位置

    1.UIButton设置文字位置 有些时候我们想让UIButton的title居左对齐,我们设置 btn.textLabel.textAlignment = UITextAlignmentLeft 是 ...

  3. Open-source Tutorial - NLog

    1. Installing NLog 使用 NuGet 程序包管理器安装 NLog.如何使用 NuGet? 遇到问题:我的项目是 .Net Framework 4.0 平台的,虽然 NLog 说明中是 ...

  4. std::function"函数"对象包装器

    语义: 类模板std::function是可调用对象的包装器,可以包装除了类成员之外的所有可调用对象.包括,普通函数,函数指针,lambda,仿函数.通过指定的模板参数,它可以用统一的方式保存,并延迟 ...

  5. httpd.conf------Apache主站点配置

    Apache主站点配置 Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.conf文件中修改. Apache主站点基本配置:ServerRoot “/mnt/sof ...

  6. Codeforces617E【莫队算法+前缀异或】

    题意: 给出一系列数,对每个查询区间,计算有多少个子区间异或为k. 思路: 可以先预处理异或前缀,一个区间[L,R]的异或值=sum[R]^sum[L-1]; 如果当前区间是[a,b],加一个右端点b ...

  7. DRF教程8-过滤

    在写后端api时,经常需要使用各种过滤条件,可以使用Q对查询集进行过滤,这里介绍一个新玩意儿 以下是基础文档 https://django-filter.readthedocs.io/en/maste ...

  8. 学习flask的网址

    学习flask的网址: http://www.bjhee.com

  9. NET Core+MySql+Nginx

    NET Core+MySql+Nginx 容器化部署 .NET Core容器化@Docker.NET Core容器化之多容器应用部署@Docker-Compose.NET Core+MySql+Ngi ...

  10. 线段树 & 题目

    首先说下我写的线段树吧. 我是按照线段树[完全版]那个人的写法来写的,因为网上大多数题解都是按照他的写法来写. 确实比较飘逸,所以就借用了. 节点大小是maxn是4倍,准确来说是大于maxn的2^x次 ...