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. 网页跟随系统 dark mode (暗黑模式) 的实现

    经过几十年的沉默, dark mode(暗黑模式) 又回到了我们面前,越来越多的 APP 有了暗黑主题,越来月多的操作系统原生添加了 "全局暗黑模式", 那么一个网站如何跟随系统的 ...

  2. Sliding Window - 题解【单调队列】

    题面: An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving fr ...

  3. 从0开始用webpack开发antd,react组件库npm包并发布

    一.初始化一个npm包 1.新建一个文件夹(名称随意,建议和报名一致),输入命令 :npm init -y 会自动生成一个包的说明文件 package.json如下(本文以scroll-antd-ta ...

  4. [AcWing 777] 字符串乘方

    点击查看代码 #include<iostream> using namespace std; string str; int main() { while (cin >> st ...

  5. Error:java: Can‘t generate mapping method with primitive return type.报错

    原因:Spring项目中使用了JPA以及Mybatis–mapper文件注解引错包导致编译错误 解决: 错误:import org.mapstruct.Mapper;正确路径:import org.a ...

  6. 五三想休息,今天还学习,图解二叉树的层序遍历BFS(广度优先)模板,附面试题题解

    壹 ❀ 引 我在从JS执行栈角度图解递归以及二叉树的前.中.后遍历的底层差异一文中,从一个最基本的数组遍历引出递归,在掌握递归的书写规则后,又从JS执行栈角度解释了二叉树三种深度优先(前序.中序后序) ...

  7. Docker容器网络-基础篇

    开源Linux 一个执着于技术的公众号 Docker的技术依赖于Linux内核的虚拟化技术的发展,Docker使用到的网络技术有Network Namespace.Veth设备对.Iptables/N ...

  8. vue - Vue组件化编程

    今天是对vue组件化的一个理解,最主要的单文件组件,然后就可以脚手架的学习了,本来昨晚就该上传的,但是用的那个上传博客园的Python脚本不行了,换了一个新的. 组件化让我越来越感觉到框架的力量了 一 ...

  9. [源码解析] TensorFlow 分布式之 ParameterServerStrategy V1

    [源码解析] TensorFlow 分布式之 ParameterServerStrategy V1 目录 [源码解析] TensorFlow 分布式之 ParameterServerStrategy ...

  10. Git使fork项目与源项目保持一致

        操作如下: 1.先clone自己的fork项目到本地工程目录 git clone git@gitlab.alibaba-inc.com:riqi/{project}.git 2.进入该项目目录 ...