UVA 177 PaperFolding 折纸痕 (分形,递归)
著名的折纸问题:给你一张很大的纸,对折以后再对折,再对折……每次对折都是从右往左折,因此在折了很多次以后,原先的大纸会变成一个窄窄的纸条。现在把这个纸条沿着折纸的痕迹打开,每次都只打开“一半”,即把每个痕迹做成一个直角,那么从纸的一端沿着和纸面平行的方向看过去,会看到一条美妙的曲线。
就是一个分形,规定一下,纸的朝向,然后不难发现规律。
实现方面,可以迭代也可递归。
在图形输出方面,存下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 折纸痕 (分形,递归)的更多相关文章
- 折纸问题java实现
/** * 折纸问题 这段代码写的太low了 本人水平有限 哎... 全是字符串了 * @param n * @return * @date 2016-10-7 * @author shaobn */ ...
- CSS3实现文字折纸效果
CSS3实现文字折纸效果 效果图: 代码如下,复制即可使用: <!DOCTYPE html> <html> <head> <title></tit ...
- 嵊州D5T2 折纸 folding
折纸 folding [问题描述] 在非常紧张的 NOIP 考试中,有人喜欢啃指甲,有人喜欢转铅笔,有人喜欢撕 纸条,……而小 x 喜欢迷折纸. 现有一个 W * H 的矩形纸张,监考老师想知道,小 ...
- 折纸(folding)
问题 C: 折纸 时间限制: 1 Sec 内存限制: 128 MB[提交] [状态] 题目描述 现有一个W*H的矩形纸张,求至少要折多少次才能使矩形纸张变成w*h的矩形纸张.注意,每次的折痕都要平行 ...
- CSS3写折纸
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 【BZOJ】1074: [SCOI2007]折纸origami
http://www.lydsy.com/JudgeOnline/problem.php?id=1074 题意:一开始有一个左上角是(0,100),右下角是(100,0)的纸片,现在可以沿有向直线折n ...
- 1074: [SCOI2007]折纸origami - BZOJ
Description 桌上有一张边界平行于坐标轴的正方形纸片,左下角的坐标为(0,0),右上角的坐标为(100,100).接下来执行n条折纸命令.每条命令用两个不同点P1(x1,y1)和P2(x2, ...
- 1074: [SCOI2007]折纸origami
Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 372 Solved: 229[Submit][Status][Discuss] Descriptio ...
- 紫书 习题8-5 UVa 177 (找规律)
参考了https://blog.csdn.net/weizhuwyzc000/article/details/47038989 我一开始看了很久, 拿纸折了很久, 还是折不出题目那样..一脸懵逼 后来 ...
随机推荐
- java之二叉树--未完待续
参考http://how2j.cn/k/collection/collection-tree/476.html#nowhere 二叉树概念 二叉树由各种节点组成二叉树特点:每个节点都可以有左子节点,右 ...
- centos 安装webbench 用于web压力测试
1.WebBench安装: yum install ctags(先安装依赖库) mkdir /usr/local/man (创建一个目录不然会报错) wget http://home.tiscali. ...
- Unity中场景异步加载
引入命名空间 using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Collections; using Syst ...
- MVC+Linq+EF笔记
using (DataContext ctx = new DataContext()) { /*foreach (var item in ctx.employees) { ViewData[" ...
- Jinkins定时任务设置
设置的地方在构建触发器中 Build after other projects are built:在其他项目构建完成后再进行构建. Build periodically:周期进行构建 Build w ...
- OVS调试介绍
基本概念 基本命令 ovs-vsctl # ovs-vsctl show 96a55a7e-f49c-4dbe-b359-bafdff2ccad7 Manager "ptcp:6640:92 ...
- TensorFlow中tf.ConfigProto()配置Sesion运算方式
博主个人网站:https://chenzhen.online tf.configProto用于在创建Session的时候配置Session的运算方式,即使用GPU运算或CPU运算: 1. tf.Con ...
- ue4 htcvivi简单配置
1 主角视口:相机与控制器挂载 Chaperone设置,主角bp上加上SteamVRChaperone用于提示用户可用区域边界 MotionController1里面选项设置为Right右手,然后下边 ...
- c# json字符串转数组
JArray jo = (JArray)JsonConvert.DeserializeObject("这里是json字符串");
- api接口测试工具和接口文档管理工具
api接口测试工具和接口文档管理工具 1.postman(https://www.getpostman.com) Postman 是一个很强大的 API调试.Http请求的工具.她可是允许用户发送任何 ...