NC201605 Bits

题目

题目描述

Nancy喜欢做游戏!

汉诺塔是一个神奇的游戏,神奇在哪里呢?

给出 \(3\) 根柱子,最开始时 \(n\) 个盘子按照大小被置于最左的柱子。

如果盘子数为偶数,则需要将她们全部移动到最右侧的柱子上,否则将她们移动到中间的柱子上。

那么,Nancy该怎样移动呢?请你输出汉诺塔游戏的过程叭!

输入描述

共一行:一个整数 \(n\) ,表示最开始 \(n\) 个盘子(编号为 \(1\) 到 \(n\) )的放置方法。

数据满足:\(2 \leq n \leq 11\)。

输出描述

共 \(2^n\) 组:每组 \(n+2\) 行,每行 \(3 \times (2n+1)+4\) 个字符,用.表示空白区域,用|表示柱子区域,用*表示盘子。组与组之间请输出 \(3 \times (2n+1)+4\) 个-。

具体输出方式请参看样例进行理解。

示例1

输入

2

输出

...................
...|.....|.....|...
..***....|.....|...
.*****...|.....|...
-------------------
...................
...|.....|.....|...
...|.....|.....|...
.*****..***....|...
-------------------
...................
...|.....|.....|...
...|.....|.....|...
...|....***..*****.
-------------------
...................
...|.....|.....|...
...|.....|....***..
...|.....|...*****.

题解

思路

知识点:递归。

难点在输出。用 \(0,1,2\) 表示三个塔,用向量模拟塔的圆盘情况,采用后进先出的的操作。再用一个函数输出当前状态即可。

时间复杂度 \(O(2^n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>

using namespace std;

int N, cnt;
vector<int> h[3]; void write() {
for (int i = 1;i <= 3 * (2 * N + 1) + 4;i++) cout << '.';
cout << '\n';
for (int i = 1;i <= 3 * (2 * N + 1) + 4;i++) {
if (i == N + 2 || i == 3 * N + 4 || i == 5 * N + 6) cout << '|';
else cout << '.';
}
cout << '\n';
for (int i = N - 1;i >= 0;i--) {
for (int j = 1;j <= 3 * (2 * N + 1) + 4;j++) {
if (i < h[0].size() && h[0][i] && N + 2 - h[0][i] <= j && j <= N + 2 + h[0][i] ||
i < h[1].size() && h[1][i] && 3 * N + 4 - h[1][i] <= j && j <= 3 * N + 4 + h[1][i] ||
i < h[2].size() && h[2][i] && 5 * N + 6 - h[2][i] <= j && j <= 5 * N + 6 + h[2][i]
) cout << '*';
else if (j == N + 2 || j == 3 * N + 4 || j == 5 * N + 6) cout << '|';
else cout << '.';
}
cout << '\n';
}
} void hanoi(int n, int A, int B, int C) {
if (n == 1) {
for (int i = 1;i <= 3 * (2 * N + 1) + 4;i++) cout << '-';
cout << '\n';
cnt++;
h[C].push_back(h[A].back());
h[A].pop_back();
write();
return;
}
hanoi(n - 1, A, C, B);
cnt++;
h[C].push_back(h[A].back());
h[A].pop_back();
for (int i = 1;i <= 3 * (2 * N + 1) + 4;i++) cout << '-';
cout << '\n';
write();
hanoi(n - 1, B, A, C);
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> N;
for (int i = N;i >= 1;i--) h[0].push_back(i);
write();
if (N & 1) hanoi(N, 0, 2, 1);
else hanoi(N, 0, 1, 2);
return 0;
}

NC201605 Bits的更多相关文章

  1. 【牛客小白月赛21】NC201605 Bits

    [牛客小白月赛21]NC201605 Bits 题目链接 题目描述 Nancy喜欢做游戏! 汉诺塔是一个神奇的游戏,神奇在哪里呢? 给出3根柱子,最开始时n个盘子按照大小被置于最左的柱子. 如果盘子数 ...

  2. [LeetCode] Number of 1 Bits 位1的个数

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  3. [LeetCode] Reverse Bits 翻转位

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  4. 【leetcode】Number of 1 Bits

    题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...

  5. Leetcode-190 Reverse Bits

    #190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...

  6. CodeForces 485C Bits[贪心 二进制]

    C. Bits time limit per test1 second memory limit per test256 megabytes inputstandard input outputsta ...

  7. uva12545 Bits Equalizer

    uva12545 Bits Equalizer You are given two non-empty strings S and T of equal lengths. S contains the ...

  8. LeetCode Counting Bits

    原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...

  9. Number of 1 Bits(Difficulty: Easy)

    题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

随机推荐

  1. Vue2响应式原理

    vue2响应式原理 vue的特性:数据驱动视图和双向数据绑定.vue官方文档也提供了响应式原理的解释: 深入响应式原理 Object.defineProperty() Object.definePro ...

  2. R 数据可视化: PCA 主成分分析图

    简介 主成分分析(Principal Component Analysis,PCA)是一种无监督的数据降维方法,通过主成分分析可以尽可能保留下具备区分性的低维数据特征.主成分分析图能帮助我们直观地感受 ...

  3. javase集合 温故而知新

    复习javase集合 1.为什么要有集合? 数组长度需要在初始化时确定大小,数据结构单一.因此集合出现了 2.数组和集合的区别 区别一:数组既可以存储基本数据类型,又可以存储引用类型,集合只能存储引用 ...

  4. 超详细的Linux 用户与用户组知识

    一个执着于技术的公众号 1.用户和组的概念 Linux系统对用户与组的管理是通过ID号来实现的.我们在登录系统时,输入用户名与对应密码,操作系统会将用户名转化为ID号后再判断该账号是否存在,并对比密码 ...

  5. Nginx中FastCGI参数的优化配置实例

    在配置完成Nginx+FastCGI之后,为了保证Nginx下PHP环境的高速稳定运行,需要添加一些FastCGI优化指令.下面给出一个优化实例,将下面代码添加到Nginx主配置文件中的HTTP层级. ...

  6. go 语言开发2 简易数据库和web代码示例

    数据库开发示例 package dao import ( "github.com/go-xorm/xorm" "fmt" ) type UserInfo str ...

  7. 微信H5页面唤醒APP并传参跳转uniapp

    主要实现是利用微信内置浏览器支持的<wx-open-launch-app>开放标签可以让你的H5网页拉起APP   在链接https://developers.weixin.qq.com/ ...

  8. 150. Evaluate Reverse Polish Notation - LeetCode

    Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...

  9. linux篇-修改mysql数据库密码

    总是忘记,每次都要查文档,背背背 方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = passw ...

  10. OpenHarmony3.1 Release版本特性解析——硬件资源池化架构介绍

    李刚 OpenHarmony 分布式硬件管理 SIG 成员 华为技术有限公司分布式硬件专家 OpenHarmony 作为面向全场景.全连接.全智能时代的分布式操作系统,通过将各类不同终端设备的能力进行 ...