近期题目整理2.0(递归和简单dfs)

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…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CUCKyrie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值