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. 如何设计一个良好的API接口?

    沟通创造价值,分享带来快乐.这里是程序员阅读时间,每天和你分享读书心得,欢迎您每天和我一起精进.今天和大家一起讨论的话题是如何设计一个良好的API接口? 作者:梁桂钊 解读:张飞洪 挑战 API是软件 ...

  2. C#/VB.NET 获取Excel中图片所在的行、列坐标位置

    本文以C#和vb.net代码示例展示如何来获取Excel工作表中图片的坐标位置.这里的坐标位置是指图片左上角顶点所在的单元格行和列位置,横坐标即顶点所在的第几列.纵坐标即顶点所在的第几行.下面是获取图 ...

  3. FreeRTOS --(14)队列管理之概述

    转载自 https://blog.csdn.net/zhoutaopower/article/details/107221175 在任何的 OS 中,都需要支持任务与任务,中断与任务之间的数据传输机制 ...

  4. 自学java的困难

    在自学的一些基础阶段,倒是没什么太大的问题,但是在想搞一个项目的时候,就显得手足无措了.因为,很多博主讲的的那些,都行需要一定的条件,比如前端框架,数据库的数据等等. 简单一点的SSM框架整合相对简单 ...

  5. Hadoop(一)Hadoop核心架构与安装

    Hadoop是什么 大白话,Hadoop是个存储数据,计算数据的分布式框架.核心组件是HDFS.MapReduce.Yarn. HDFS:分布式存储 MapReduce:分布式计算 Yarn:调度Ma ...

  6. 电脑UEFI启动是什么?

    UEFI 当EFI发展到1.1的时候,英特尔决定把EFI公之于众,于是后续的2.0吸引了众多公司加入,EFI也不再属于英特尔,而是属于了Unified EFI Form的国际组织,EFI在2.0后也遂 ...

  7. Spring注解开发_Spring容器创建概述

    浅尝Spring注解开发_Spring容器创建概述 浅尝Spring注解开发,基于Spring 4.3.12 概述Spring容器创建的过程,包括12个方法的执行 浅尝Spring注解开发_自定义注册 ...

  8. vue - Vue脚手架/消息订阅与发布

    今天的内容有意思了,朋友们继续对我们之前的案例完善,是这样的我们之前是不是靠props来完成父给子,子给父之间传数据,其实父给子最好的方法就是props但是自给父就不是了,并且今天学下来,不仅如此,组 ...

  9. 升级gradle后。需要修改jenkin 编译java版本从1.8 到11

    错误提示 * What went wrong: A problem occurred evaluating project ':App'. > Failed to apply plugin 'c ...

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

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