If a large sheet of paper is folded in half, then in half again, etc, with all the folds parallel, then opened up flat, there are a series of parallel creases, some pointing up and some down, dividing the paper
into fractions of the original length. If the paper is only opened ``half-way'' up, so every crease forms a 90 degree angle, then (viewed end-on) it forms a ``dragon curve''. For example, if four successive folds are made, then the following curve is seen
(note that it does not cross itself, but two corners touch):

Write a program to draw the curve which appears after N folds. The exact specification of the curve is as follows:

  • The paper starts flat, with the ``start edge'' on the left, looking at it from above.
  • The right half is folded over so it lies on top of the left half, then the right half of the new double sheet is folded on top of the left, to form a 4-thick sheet, and so on, for N folds.
  • Then every fold is opened from a 180 degree bend to a 90 degree bend.
  • Finally the bottom edge of the paper is viewed end-on to see the dragon curve.

From this view, the only unchanged part of the original paper is the piece containing the ``start edge'', and this piece will be horizontal, with the ``start edge'' on the left. This uniquely defines the curve.
In the above picture, the ``start edge'' is the left end of the rightmost bottom horizontal piece (marked `s'). Horizontal pieces are to be displayed with the underscore character ``_'', and vertical pieces with the ``|'' character.

Input

Input will consist of a series of lines, each with a single number N (  ).
The end of the input will be marked by a line containing a zero.

Output

Output will consist of a series of dragon curves, one for each value of N in the input. Your picture must be shifted as far left, and as high as possible. Note that for large N, the picture will
be greater than 80 characters wide, so it will look messy on the screen. The pattern for each different number of folds is terminated by a line containing a single `^'.

Sample input

2
4
1
0

Sample output

|_
 _|
^
   _   _
  |_|_| |_
   _|    _|
|_|
^
_|
^

经典题目吧,感觉是练习递归的,可是有非递归的方式做,果断直接迭代了。

观察每次展开新部分和旧部分,新的尾和旧的头相对应,依次往中间走对应起来,得到对应关系:

上变成左

下变成右

左变成下

右变成上

画图的部分用map存储。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>

using namespace std;

const int maxn = 1 << 15;
int A[maxn];

// 0,1,2,3分别是上下左右
int trans[] = { 2, 3, 1, 0 };

map<int, set< pair<int, int> > > P;

void build(int n)
{
	int m = 1;
	A[0] = 3;
	for (int i = 1; i <= n; i++) {
		for (int j = m - 1, k = m; j >= 0; j--, k++) { // 算出下一次展开的位置
			A[k] = trans[A[j]];
		}
		m <<= 1;
	}
	int x = -1, y = 0, px = 0, py = 0;
	P.clear();
	// 把每个短线的位置计算出来,并放入P中
	for (int i = 0; i < m; i++) {
		if (A[i] == 0) {
			x = px << 1;
			y = py;
			P[y].insert(make_pair(x, 0));
			py++;
		}
		else if (A[i] == 1) {
			x = px << 1;
			y = py - 1;
			P[y].insert(make_pair(x, 1));
			py--;
		}
		else if (A[i] == 2) {
			x = (px << 1) - 1;
			y = py;
			P[y].insert(make_pair(x, 2));
			px--;
		}
		else {
			x = (px << 1) + 1;
			y = py;
			P[y].insert(make_pair(x, 3));
			px++;
		}
	}
}

void draw()
{
	// 设置无穷大和无穷小,具体为什么在这篇题解前一篇博文有说明
	int mxy = -0x3f3f3f3f, mnx = 0x3f3f3f3f;
	for (map<int, set< pair<int, int> > >::iterator it = P.begin();
		it != P.end(); it++) {
		mxy = max(mxy, it->first);
		for (set< pair<int, int> >::iterator jt = it->second.begin();
			jt != it->second.end(); jt++) {
			mnx = min(mnx, jt->first);
		}
	}
	// 从最上面一行画起,所以需要反向遍历
	for (map<int, set< pair<int, int> > >::reverse_iterator it = P.rbegin();
		it != P.rend(); it++) {
		int i = mnx;
		for (set<pair<int, int> >::iterator jt = it->second.begin();
			jt != it->second.end(); jt++) {
			while (i < jt->first) {
				cout << ' ';
				i++;
			}
			i++;
			if (jt->second == 0 || jt->second == 1) {
				cout << '|';
			}
			else {
				cout << '_';
			}
		}
		cout << endl;
	}
	cout << '^' << endl;
}

int main()
{
	ios::sync_with_stdio(false);
	int n;
	while (cin >> n && n) {
		build(n);
		draw();
	}

	return 0;
}

Uva - 177 - Paper Folding的更多相关文章

  1. uva 177:Paper Folding(模拟 Grade D)

    题目链接 题意:一张纸,每次从右往左对折.折好以后打开,让每个折痕都自然的呈90度.输出形状. 思路:模拟折……每次折想象成把一张纸分成了正面在下的一张和反面在上的一张.维护左边和方向,然后输出.细节 ...

  2. Paper Folding UVA - 177 模拟+思路+找规律

    题目:题目链接 思路:1到4是很容易写出来的,我们先考虑这四种情况的绘制顺序 1:ru 2:rulu 3:rululdlu 4:rululdluldrdldlu 不难发现,相较于前一行,每一次增加一倍 ...

  3. 【uva 177】Paper Folding(算法效率--模拟)

    P.S.模拟真の难打,我花了近乎三小时!o(≧口≦)o 模拟题真的要思路清晰!分块调试. 题意:著名的折纸问题:给你一张很大的纸,对折以后再对折,再对折--每次对折都是从右往左折,因此在折了很多次以后 ...

  4. UVA 177 PaperFolding 折纸痕 (分形,递归)

    著名的折纸问题:给你一张很大的纸,对折以后再对折,再对折……每次对折都是从右往左折,因此在折了很多次以后,原先的大纸会变成一个窄窄的纸条.现在把这个纸条沿着折纸的痕迹打开,每次都只打开“一半”,即把每 ...

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

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

  6. 【Uva 1630】Folding

    [Link]: [Description] 你能对字符串进行压缩的操作; 即把连续出现的相同的子串改成它出现的次数+这个最基本的字符串的形式; 问你这个字符串最短能被压缩得多短; [Solution] ...

  7. github上所有大于800 star OC框架

    https://github.com/XCGit/awesome-objc-frameworks#awesome-objc-frameworks awesome-objc-frameworks ID ...

  8. 一位学长的ACM总结(感触颇深)

    发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...

  9. GitHub前50名的Objective-C动画相关库

    GitHub的Objective-C的动画UI库其实是最多的一部分,GitHub有相当一部分的动画大牛,如Jonathan George,Nick Lockwood,Kevin,Roman Efimo ...

随机推荐

  1. 一些重要的计算机网络协议(IP、TCP、UDP、HTTP)

    一.计算机网络的发展历程 1.计算机网络发展 与其说计算机改变了世界,倒不如说是计算机网络改变了世界.彼时彼刻,你我都因网络而有了交集,岂非一种缘分? 计算机与网络发展大致经历如下过程:

  2. python学习之路基础篇(第七篇)

    一.模块 configparser configparser用于处理特定格式的文件,其本质是利用open来对文件进行操作 [section1] # 节点 k1 = v1 # 值 k2:v2 # 值 [ ...

  3. 原生Js写轮播图代码

    html css js 在知道jQuery如何实现轮播效果的基础上,用js写代码 如图:标记这里的地方 理解一下 用到的知识: 1.HTML DOM 的appendChild() 和 removeCh ...

  4. docker环境 mysql读写分离 mycat maxscale

    #mysql读写分离测试 环境centos 7.4 ,docker 17.12 ,docker-compose mysql 5.7 主从 mycat 1.6 读写分离 maxscale 2.2.4 读 ...

  5. JAVA处理Blob大对象

    Blob对象是SQL Blob的Java语言映射.SQL Blob是一个内置类型,它可以将一个二进制大对象保存在数据库中.接口ResultSet.CallableStatement和PreparedS ...

  6. 为什么《Dive into Python》不值得推荐

    2010 年 5 月 5 日更新:我翻译了一篇<<Dive Into Python>非死不可>作为对本文观点的进一步支持和对评论的回复,请见:http://blog.csdn. ...

  7. 自定义view实现阻尼效果的加载动画

    效果: > 需要知识: 1. 二次贝塞尔曲线 2. 动画知识 3. 基础自定义view知识 先来解释下什么叫阻尼运动 阻尼振动是指,由于振动系统受到摩擦和介质阻力或其他能耗而使振幅随时间逐渐衰减 ...

  8. Bootstrap3 排版-引用

    在你的文档中引用其他来源的内容. 默认样式的引用 将任何 HTML 元素包裹在 <blockquote> 中即可表现为引用样式.对于直接引用,我们建议用 <p> 标签. Lor ...

  9. Support Annotation Library使用详解

    概述 Support Annotation Library是在Android Support Library19.1版本开始引入的一个全新的函数包,它包含了诸多有用的元注解.用来帮助开发者在编译期间发 ...

  10. 用Python递归解决阿拉伯数字转为中文财务数字格式的问题(2)--打开思路的一种方法

    几天前自己写了个将阿拉伯数字转为中文财务数字的程序.用的递归,不幸的是它是树形递归. 虽然实际过程中不太可能出现金额数字大到让Python递归栈溢出,但是始终是一块心病,这玩意终究在理论上是受限制的. ...