Codefores 507C Guess Your Way Out!(递归)
1 second
256 megabytes
standard input
standard output
Amr bought a new video game "Guess Your Way Out!". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node.
Let's index all the leaf nodes from the left to the right from 1 to 2h. The exit is located at some node n where 1 ≤ n ≤ 2h, the player doesn't know where the exit is so he has to guess his way out!
Amr follows simple algorithm to choose the path. Let's consider infinite command string "LRLRLRLRL..." (consisting of alternating characters 'L' and 'R'). Amr sequentially executes the characters of the string using following rules:
- Character 'L' means "go to the left child of the current node";
- Character 'R' means "go to the right child of the current node";
- If the destination node is already visited, Amr skips current command, otherwise he moves to the destination node;
- If Amr skipped two consecutive commands, he goes back to the parent of the current node before executing next command;
- If he reached a leaf node that is not the exit, he returns to the parent of the current node;
- If he reaches an exit, the game is finished.
Now Amr wonders, if he follows this algorithm, how many nodes he is going to visit before reaching the exit?
Input consists of two integers h, n (1 ≤ h ≤ 50, 1 ≤ n ≤ 2h).
Output a single integer representing the number of nodes (excluding the exit node) Amr is going to visit before reaching the exit by following this algorithm.
1 2
2
2 3
5
3 6
10
10 1024
2046
A perfect binary tree of height h is a binary tree consisting of h + 1 levels. Level 0 consists of a single node called root, level h consists of 2h nodes called leaves. Each node that is not a leaf has exactly two children, left and right one.
Following picture illustrates the sample test number 3. Nodes are labeled according to the order of visit.
题意是按照“LRLRLRLR....”这个指令在数上走 。。
然后给了几个返回的条件, 让你算一下经过了多少个结点。
其实就是一个根据指令递归判点在左右子树还是右子树的情况。
做法就是分成4种情况
出口在左子树,指令是L
出口在左子树,指令是R
出口在右子树,指令是L
出口在右子树,指令是R
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n , h , ok ;
LL cal( LL dep , LL pos , int tag ) {
if( ok ) return ;
if( dep == ) { ok = ; return ; }
LL tmp ;
if( !tag ) {
if( pos >(1LL<<(dep-))) tmp = (1LL<<dep)+cal(dep-,pos-(1LL<<(dep-)),) ;
else tmp = 1LL + cal(dep-,pos,);
}
else {
if( pos > (1LL<<(dep-)) ) tmp = 1LL+ cal( dep-,pos-(1LL<<(dep-)),) ;
else tmp = (1LL<<dep) + cal(dep-,pos,);
}
return tmp ;
}
int main()
{
while( cin >> h >> n ) {
ok = ;
cout << cal(h,n,) << endl ;
}
}
Codefores 507C Guess Your Way Out!(递归)的更多相关文章
- .NET 基础 一步步 一幕幕[面向对象之方法、方法的重载、方法的重写、方法的递归]
方法.方法的重载.方法的重写.方法的递归 方法: 将一堆代码进行重用的一种机制. 语法: [访问修饰符] 返回类型 <方法名>(参数列表){ 方法主体: } 返回值类型:如果不需要写返回值 ...
- 算法笔记_013:汉诺塔问题(Java递归法和非递归法)
目录 1 问题描述 2 解决方案 2.1 递归法 2.2 非递归法 1 问题描述 Simulate the movement of the Towers of Hanoi Puzzle; Bonus ...
- Android 算法 关于递归和二分法的小算法
// 1. 实现一个函数,在一个有序整型数组中二分查找出指定的值,找到则返回该值的位置,找不到返回 -1. package demo; public class Mytest { public st ...
- 二叉树的递归实现(java)
这里演示的二叉树为3层. 递归实现,先构造出一个root节点,先判断左子节点是否为空,为空则构造左子节点,否则进入下一步判断右子节点是否为空,为空则构造右子节点. 利用层数控制迭代次数. 依次递归第二 ...
- 递归实现n(经典的8皇后问题)皇后的问题
问题描述:八皇后问题是一个以国际象棋为背景的问题:如何能够在8×8的国际象棋棋盘上放置八个皇后, 使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行.纵行或斜线上 ...
- C语言用分别用递归和循环求数字的阶乘的方法
以下代码均为 自己 实现,嘻嘻! 参考文章:http://blog.csdn.net/talk_8/article/details/46289683 循环法 int CalFactorial(int ...
- C#递归解决汉诺塔问题(Hanoi)
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace MyExamp ...
- Java之递归求和的两张方法
方法一: package com.smbea.demo; public class Student { private int sum = 0; /** * 递归求和 * @param num */ ...
- C#语言基础——递归
递归 一.概念conception: 函数体内调用本函数自身,直到符合某一条件不再继续调用. 二.应满足条件factor: (1)有反复执行的过程(调用自身): (2)有跳出反复执行过程的条件(函数出 ...
随机推荐
- query_module - 向内核查询和模块有关的各个位
总览 #include <linux/module.h> int query_module(const char *name, int which,void *buf, size_t bu ...
- 02.Linux-CentOS系统NFS挂载时拒绝访问挂载问题
问题: 在挂载nfs时报拒绝访问挂载:mount -t nfs 192.163.1.10:/home/opneuser/upload /home/openuser/upload/ 报错信息:Mount ...
- 246-基于TI DSP TMS320C6678、Altera FPGA的CPCI处理卡
基于TI DSP TMS320C6678.Altera FPGA的CPCI处理卡 1.板卡概述 本板卡由我公司自主研发,基于CPCI架构,符合CPCI2.0标准,采用两片TI DSP TMS320C ...
- 《YC创业营:硅谷顶级创业孵化器如何改变世界》:YC2011批量天使投资记录 三星推荐
这个YC创业营是一个硅谷的天使投资基金,每年两次批量投资创业公司.本书说的是2011年YC批量选择了64个创业团队,让他们集中到硅谷办公3个月,给他们创业指导,帮他们找A轮投资. YC创始人偏爱25岁 ...
- Java的GC机制及算法
GC的阶段 对每个对象而言,垃圾回收分为两个阶段:finalization和reclamation. finalization: 指运行这个对象的finalize的方法. reclamation: ...
- springboot中使用RabbitMq
转载:http://www.ityouknow.com/springboot/2016/11/30/spring-boot-rabbitMQ.html RabbitMQ 即一个消息队列,主要是用来实现 ...
- deepin开机进入intramfs无法正常开机
问题原因:由于非正常关机导致文件系统受损 解决方法: fsck /dev/sda6 注释:如果输入上面的指令只是出现如下提示 fsck from util-linux-ng 2.17.2(后面的数字可 ...
- SERVLET API中转发与重定向的区别?
SERVLET API中转发与重定向的区别? 1.转发(forward方法) 转发仅是容器中控制权的转向,在客户端浏览器地址栏中不会显示出转向后的地址. 转发是服务器请求资源,服务器直接访问目标地址的 ...
- 人生苦短_我用Python_logging日志操作_011
话不多说,开搞,基础是先使用自带loggin模块,level为warning以上, 进一步是自定义logger,level可自定义 #!/usr/bin/env python # -*- coding ...
- sql语句中判断空值的函数
COALESCE()函数 主流数据库系统都支持COALESCE()函数,这个函数主要用来进行空值处理,其参数格式如下: COALESCE ( expression,value1,value2……,v ...