NC201605 Bits
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的更多相关文章
- 【牛客小白月赛21】NC201605 Bits
[牛客小白月赛21]NC201605 Bits 题目链接 题目描述 Nancy喜欢做游戏! 汉诺塔是一个神奇的游戏,神奇在哪里呢? 给出3根柱子,最开始时n个盘子按照大小被置于最左的柱子. 如果盘子数 ...
- [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 ...
- [LeetCode] Reverse Bits 翻转位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【leetcode】Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
- Leetcode-190 Reverse Bits
#190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...
- CodeForces 485C Bits[贪心 二进制]
C. Bits time limit per test1 second memory limit per test256 megabytes inputstandard input outputsta ...
- uva12545 Bits Equalizer
uva12545 Bits Equalizer You are given two non-empty strings S and T of equal lengths. S contains the ...
- LeetCode Counting Bits
原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...
- Number of 1 Bits(Difficulty: Easy)
题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
随机推荐
- 【GPLT】 2018年天梯赛全国总决赛 L2-2 小字辈(c++)
题目: 这一题并不是很难,属于常规的图论遍历题,这里我是用的bfs(dfs应该也可以,但明显bfs简单一些). 本人写的时候写了很多没必要头文件,自己可以根据内容删去,必要的我会写上注释 如有错误,请 ...
- [源码解析] TensorFlow 分布式之 MirroredStrategy 分发计算
[源码解析] TensorFlow 分布式之 MirroredStrategy 分发计算 目录 [源码解析] TensorFlow 分布式之 MirroredStrategy 分发计算 0x1. 运行 ...
- 深入理解mmap--内核代码分析及驱动demo示例
mmap是一个很常用的系统调用,无论是分配内存.读写大文件.链接动态库文件,还是多进程间共享内存,都可以看到其身影.本文首先介绍了进程地址空间和mmap,然后分析了内核代码以了解其实现,最后通过一个简 ...
- Golang 高阶函数
定义 高阶函数是接收函数作为参数或返回函数作为输出的函数. 高阶函数是对其他函数进行操作的函数,要么将它们作为参数,要么返回它们. 举例 函数作为参数 package main import &quo ...
- 详解Docker中Image、Container与 Volume 的迁移
开源Linux 长按二维码加关注~ 上一篇:Linux Used内存到底哪里去了? 已经部署的容器化服务,也不是不需要维护的.而且,由于生产环境往往有这样那样的严格要求,往往需要些非常规操作.Imag ...
- 踩到一个关于分布式锁的非比寻常的BUG!
你好呀,我是歪歪. 提到分布式锁,大家一般都会想到 Redis. 想到 Redis,一部分同学会说到 Redisson. 那么说到 Redisson,就不得不掰扯掰扯一下它的"看门狗&quo ...
- 微信新菜单类型 article_id 设置教程
前不久, Senparc.Weixin SDK 跟随微信更新的步伐,上线了新的素材管理接口,其中也涉及到了 article_id 类型的自定义菜单接口. 本文将演示如何使用新的菜单类型. 官方文档传送 ...
- Dapr 不是服务网格,只是我长的和他很像
概述 我们快速看一遍官方文档:https://docs.dapr.io/concepts/service-mesh/#how-dapr-and-service-meshes-compare ,看看 D ...
- Web安全学习笔记 SQL注入上
Web安全学习笔记 SQL注入上 繁枝插云欣 --ICML8 SQL注入分类 SQL注入检测 一.注入分类 1.简介 SQL注入是一种代码注入技术用于攻击数据驱动的应用程序在应用程序中,如果没有做恰当 ...
- strlen获取字符数组为什么是255
为什么是255呢? strlen函数的规则是,读取到0则判断字符串结束. char为1字节,只有8位. 所以...... -1就是 1111 1111, -2就是 1111 1110, 直到-128: ...