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. mongodb 数据块的迁移流程介绍

    1. 基本概念 1.1 Chunk(数据块) 表示特定服务器上面,连续范围的分片键值所包含的一组数据,是一个逻辑概念. 例如,某数据块记录如下: { "_id" : "c ...

  2. Golang 高阶函数

    定义 高阶函数是接收函数作为参数或返回函数作为输出的函数. 高阶函数是对其他函数进行操作的函数,要么将它们作为参数,要么返回它们. 举例 函数作为参数 package main import &quo ...

  3. [题解] 树(tree)

    题目大意 ​ 给定一颗 \(N\) 个点的有根树,其中 \(1\) 是树根,除了 \(1\) 以外的其他点 \(u\) 有唯一的父亲 \(Father_u\).同时,给定 \(M\) 条路径,第 \( ...

  4. 跟我学Python图像处理丨获取图像属性、兴趣ROI区域及通道处理

    摘要:本篇文章主要讲解Python调用OpenCV获取图像属性,截取感兴趣ROI区域,处理图像通道. 本文分享自华为云社区<[Python图像处理] 三.获取图像属性.兴趣ROI区域及通道处理 ...

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

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

  6. 【hexo博客搭建】本地搭建hexo博客(上)

    前言 本篇文章会从本地(Windows 10)搭建-主题更换-部署阿里云详细步骤,如果在搭建过程中,遇到问题,可以通过博客页脚下的QQ联系我,或者在下面评论留言 一.本地搭建 1.安装前置 1.1安装 ...

  7. 第一个MVC程序

    配置版 添加web的支持! 确定导入了SpringMVC 的依赖! 配置web.xml , 注册DispatcherServlet <?xml version="1.0" e ...

  8. HIVE 数据分析

    题目要求: 具体操作: ①hive路径下建表:sale create table sale (day_id String, sale_nbr String, buy_nbr String, cnt S ...

  9. 以圆类 Circle 及立体图形类 Solid 为基础设计球类 Sphere

    学习内容:以圆类 Circle 及立体图形类 Solid 为基础设计球类 Sphere 代码示例: package 实验三; import java.util.Scanner; class Point ...

  10. 手把手教你使用Git管理你的软件代码

    什么是分布式版本控制系统?Git有哪些常用命令?什么是仓库?Git的操作区域包括哪些?Git有哪些常用对象(object)?git rebase和git merge的区别是什么?git reset,g ...