1.HDU-1207
题目描述:

经典的汉诺塔问题经常作为一个递归的经典例题存在。可能有人并不知道汉诺塔问题的典故。汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往上按大小顺序摞着64片黄金圆盘。上帝命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一回只能移动一个圆盘。有预言说,这件事完成时宇宙会在一瞬间闪电式毁灭。也有人相信婆罗门至今仍在一刻不停地搬动着圆盘。恩,当然这个传说并不可信,如今汉诺塔更多的是作为一个玩具存在。Gardon就收到了一个汉诺塔玩具作为生日礼物。

Gardon是个怕麻烦的人(恩,就是爱偷懒的人),很显然将64个圆盘逐一搬动直到所有的盘子都到达第三个柱子上很困难,所以Gardon决定作个小弊,他又找来了一根一模一样的柱子,通过这个柱子来更快的把所有的盘子移到第三个柱子上。下面的问题就是:当Gardon在一次游戏中使用了N个盘子时,他需要多少次移动才能把他们都移到第三个柱子上?很显然,在没有第四个柱子时,问题的解是2^N-1,但现在有了这个柱子的帮助,又该是多少呢?
 AC代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL f[70];
int n;
int main()
{f[1] = 1;f[2] = 3;f[3] = 5;for(int i = 3;i <= 64;i++){LL minn = 0x3f3f3f3f3f3f;for(int j = 1;j < i;j++){if(2*f[j] + pow(2,i - j) - 1 < minn){minn = 2*f[j] + pow(2,i - j) - 1;}}f[i] = minn;}while(~scanf("%d",&n)){printf("%lld\n",f[n]);}return 0;
}

这个题首先需要有一个知识铺垫:经典汉若塔问题里,移动的次数满足2^N-1次。这里的话给了四根柱子,所以再去推一个公式不太现实……
这里这样考虑:先把A柱子上的X个盘子移到第四个柱子D上,经过了f[X]次移动,接下来把剩下的盘子移动到柱子C上,这就是一个经典汉若塔问题了,然后把D上的盘子移动到C上就可以。

2.POJ-1979 Red And Black
题目描述:

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can’t move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input:
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

‘.’ - a black tile

‘#’ - a red tile

‘@’ - a man on a black tile(appears exactly once in a data set)

The end of the input is indicated by a line consisting of two zeros.
Output:
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
AC代码:

#include<iostream>
using namespace std;
const int maxn = 25;
char a[maxn][maxn];
int w,h,ans;
void dfs(int x,int y)
{if(x - 1 >= 0&&a[x - 1][y] == '.'){a[x - 1][y] = '#';dfs(x - 1,y);ans++;}if(x + 1 < h&&a[x + 1][y] == '.'){a[x + 1][y] = '#';dfs(x + 1,y);ans++;}if(y - 1 >= 0&&a[x][y - 1] == '.'){a[x][y - 1] = '#';dfs(x,y - 1);ans++;}if(y + 1 < w&&a[x][y + 1] == '.'){a[x][y + 1] = '#';dfs(x,y + 1);ans++;}
}
int main()
{int manx,many;while(cin >> w >>h){if(w == 0&&h == 0) return 0;ans = 0;for(int i = 0;i < h;i++){for(int j = 0;j < w;j++){cin >> a[i][j];if(a[i][j] == '@'){manx = i;many = j;}}}a[manx][many] = '#';ans++;dfs(manx,many);cout << ans << endl;}return 0;
}

本题应该说是一个比较简单的DFS的题目,类似于走迷宫问题,@为当前的位置,#的位置是可以走的,所以直接锁定范围DFS即可。注意DFS的时候要分别考虑四个方向的情况。

3.OpenJ_Bailian - 4117
题目描述:
将正整数n 表示成一系列正整数之和,n=n1+n2+…+nk, 其中n1>=n2>=…>=nk>=1 ,k>=1 。
正整数n 的这种表示称为正整数n 的划分。正整数n 的不同的划分个数称为正整数n 的划分数。
AC代码:

#include<bits/stdc++.h>
using namespace std;
int dfs(int a,int b)
{if(a == 0) return 1;if(b == 0) return 0;if(a >= b) return dfs(a - b,b) + dfs(a,b - 1); //分为使用b减和不使用b减两种情况if(a < b) return dfs(a,b - 1);
}
int main()
{int n;while(cin >> n){int ans = dfs(n,n);cout << ans << endl;}return 0;
}

这个题也是一个DFS的题目,构造一个DFS(N,N),前一个N是指的被减数,后面的N是指的减数,所以DFS回溯的临界就是被减数减为0,此时返回1,如果减数为0,则返回0.

4.Paths on a Grid
题目描述:
Imagine you are attending your math lesson at school. Once again, you are bored because your teacher tells things that you already mastered years ago So you decide to waste your time with drawing modern art instead.

Fortunately you have a piece of squared paper and you choose a rectangle of size n*m on the paper. Let’s call this rectangle together with the lines it contains a grid. Starting at the lower left corner of the grid, you move your pencil to the upper right corner, taking care that it stays on the lines and moves only to the right or up. The result is shown on the left:

Really a masterpiece, isn’t it? Repeating the procedure one more time, you arrive with the picture shown on the right. Now you wonder: how many different works of art can you produce?
Input:
The input contains several testcases. Each is specified by two unsigned 32-bit integers n and m, denoting the size of the rectangle. As you can observe, the number of lines of the corresponding grid is one more in each dimension. Input is terminated by n=m=0.
Output:
For each test case output on a line the number of different art works that can be generated using the procedure described above. That is, how many paths are there on a grid where each step of the path consists of moving one unit to the right or one unit up? You may safely assume that this number fits into a 32-bit unsigned integer.
AC代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
long long n,m,ans,x;
long long getc(long long a,long long b)
{long long s = 1;for(long long i = 1;i <= b;i++){s = s*(a - b + i)/i; }return s;
}
int main()
{while(~scanf("%lld %lld",&n, &m)&&(n != 0||m != 0)){ans = 0;x = n;if(m < n) x = m;printf("%lld\n",getc(m + n,x));}return 0;
}

我们这样想,既然只能走上方向或者右方向,也就是说走的总数是不变的m+n,为了省时间我们可以选择m,n中最小的那一个赋值给x,然后C(m+n,x)就可以了。(我做的时候给T了10次TAT,所以一定要注意选择求C(m+n,x)的方法。

5.51Nod - 1073 约瑟夫环
题目描述:
N个人坐成一个圆环(编号为1 - N),从第1个人开始报数,数到K的人出列,后面的人重新从1开始报数。问最后剩下的人的编号。

例如:N = 3,K = 2。2号先出列,然后是1号,最后剩下的是3号。
(自杀游戏)
AC代码:

#include<stdio.h>int f(int n, int m){if(n==1)    return 0;else return (f(n-1,m)+m)%n;
} int main()
{int m, n;scanf("%d %d",&n,&m);printf("%d\n", f(n,m)+1);return 0;
}

直白说,这个约瑟夫环套的公式QAQ…

近期题目整理2.0(递归和简单dfs)相关推荐

  1. Day9-2021.1.17-剑指offer的八道二叉树题目的整理。涉及递归调用+广度优先遍历。

    2021年1月17日 时间都去哪了? 今日计划: 1.小学初中的辅导.任务已被安排~ 2.组会 3.剑指offer的二叉树题目 4.图解tcp ip 的内容 今日工作: 1.小学初中的辅导.任务已被安 ...

  2. 【面试】网易游戏面试题目整理及答案(3)

    网易游戏面试题目整理及答案(3) 数据库部分 MySQL 事务 MySQL锁机制 MySQL调优 MySQL分区.分表.分库 主从复制 其他问题 数据库部分 MySQL 事务 事务的隔离级别有哪些?M ...

  3. 【面试】网易游戏面试题目整理及答案(5)

    网易游戏面试题目整理及答案(5) 算法 操作系统 Linux部分 其他 参考资料 算法 Leetcode 75题:请写出一个高效的在m*n矩阵中判断目标值是否存在的算法,矩阵具有如下特征: 1)每一行 ...

  4. 基本算法之递推与递归的简单应用

    递推与递归的简单应用 常见的枚举形式 实现指数型枚举 DFS (一) DFS (二) 位运算(一) 位运算(二) 实现组合型枚举 DFS + 剪枝 实现排列型枚举 DFS 费解的开关 奇怪的汉诺塔 分 ...

  5. 习题2.6 递归求简单交错幂级数的部分和 (15 分)

    习题2.6 递归求简单交错幂级数的部分和 (15 分) 本题要求实现一个函数,计算下列简单交错幂级数的部分和: f(x,n)=x−x​2​​+x​3​​−x​4​​+⋯+(−1)​n−1​​x​n​​ ...

  6. C语言递归(pta递归求简单交错幂级数的部分和)

    函数自己调用自己的形式称为函数的递归调用. 代码一:n的阶乘: #include<stdio.h> long fact(int n){long f;if(n==1)f=1; //递归出口e ...

  7. 考研操作系统题目整理

    操作系统题目整理 大家觉得有用点点赞啊啊我升到3级就可以自定义标签了谢谢~ 说复试题目过于牵强,只是自己整理的一些知识点而已,为了便于理解和背诵,有些部分定义和说明尽量简明扼要,如有错误请多多指教!( ...

  8. 计算机适应性考试题目,计算机控制考试题目整理

    <计算机控制考试题目整理>由会员分享,可在线阅读,更多相关<计算机控制考试题目整理(7页珍藏版)>请在人人文库网上搜索. 1.简答题1.3计算机控制系统的典型形式有哪些?各有什 ...

  9. 【面试】嵌入式C语言题目整理

    [面试]嵌入式C语言题目整理 描述内存四区. 内存四区分为:代码区.静态区.堆区.栈区 代码区就是用来存放代码的. 静态区用来存放全局变量.静态变量.常量(字符串常量.const修饰的全局变量). 堆 ...

最新文章

  1. 2021年大数据Flink(十):流处理相关概念
  2. 兼容微信小程序的流式网络请求库
  3. taro 重新加载小程序_Taro 小程序采坑
  4. [转]阮一峰:蒙特卡罗方法入门
  5. l源码安装mysql升级_[Linux]javaEE篇:源码安装mysql
  6. C#可空类型(Nullable Types)
  7. 【转】 SLIC超像素分割详解(一):简介
  8. Oracle数据库(二)—— 基本的SQL SELECT语句
  9. android 架构份额,Android 架构设计比较分析
  10. Callable介绍
  11. selenium实现chrome分屏截图的合并
  12. 跟我一起学docker(15)--监控日志和日志管理
  13. Distinct去除集合中的重复项GetHashCode方法没有返回obj.GetHashCode()导致出错
  14. Graph——bfs,dfs
  15. idea安装python 插件_IntelliJ IDEA安装运行python插件方法
  16. 为什么区块链世界既需要计算机科学家也需要经济学家?
  17. 中国行政区划 地级 js
  18. M1 Repast Simphony打不开 无反应问题解决
  19. 网络流24题 最小路径覆盖(DCOJ8002)
  20. 不想用百度云,那就教你自建属于自己的云盘!

热门文章

  1. 数据结构与算法题目集(中文) - 7-46 新浪微博热门话题(30 分)
  2. ESP32设备驱动-LSM303 3D加速度计/磁力计驱动
  3. 在moveit2中实现四连杆及曲柄滑块
  4. 为什么掌握了ROS1机器人开发工具并不能加速掌握ROS2呢???
  5. 奶牛乘法c语言数组,C++程序题,奶牛问题
  6. 「双11」广东人最能买,上海人买火锅底料第一,浙江有人买进了医院……
  7. 半监督主题模型Correlation Explanation
  8. 微信小程序添加音乐组件
  9. 每个男孩的机械梦「GitHub 热点速览 v.21.41」
  10. 微信开发手机在线调试