Frogger crossing the river [recursive method]


topic description

There is a river with a stone pier on the left ( AAArea A ) has numbers1, 2, 3, 4, …, n 1, 2, 3, 4, …, n1234, n ofnn frogs, there arekkk lotus leaves (CCArea C ), andhhh stone piers (DDArea D ), with a stone pier on the right (BBArea B ), as shown in the figure below. nn frogs want to cross the river (from the stone pierAAA to the stone pier on the right bankBBB ), the rule is:
insert image description here

  • The stone pier can bear any number of frogs, and the lotus leaf can only bear one frog (regardless of size);
  • Frogs can: AAA B B B (available fromAAA jumps toBBB , same below),AAA C C C A A A D D D C C C B B B D D D B B B D D D C C C C C C D D D
  • When there are multiple frogs on a stone pier, the frog on it can only jump to the frog that is 1 size larger than it.

Your task is to give hhh k k k , calculate and output how many frogs can cross the river smoothly according to the above rules?

Input and output samples

input sample

2 2 2 3 3 3

There are 2 2 in the middle of the river2 stone grate,3 33 lotus leaves

output sample

16 16 16

Up to 16 1616 frogs can cross the river according to the rules


recursive solution

the key here
From specific to general, the derivation process is as follows:
f ( 0 , 0 ) = 1 f(0,0)=1f(0,0)=1
f ( 0 , k ) = k + 1 ; f(0,k)=k+1; f(0,k)=k+1 ;       (e.g.k = 3 k=3k=3 when there are4 44 frogs can cross the river)
f ( 1 , k ) = 2 ( k + 1 ) ; f(1,k)=2(k+1);f(1,k)=2(k+1 ) ;       (recursion thinking)
f ( 2 , k ) = 2 ( k + 1 ) × 2 = 2 2 × ( k + 1 ) ; f(2,k)=2(k+1)×2=2 ^2×(k+1);f(2,k)=2(k+1)×2=22×(k+1 ) ;
...
and so on to get the recursive formula:F ( i , k ) = F ( i − 1 , k ) × 2 = 2 i × ( k + 1 ) ; F(i,k)=F(i -1,k)×2=2^i×(k+1);F(i,k)=F(i1,k)×2=2i×(k+1 ) ;
...
therefore,f ( h , k ) = 2 h × ( k + 1 ) f(h,k)=2^h×(k+1)f(h,k)=2h×(k+1)


AC code

#include <bits/stdc++.h>
using namespace std;
long long h,k,ans=1;
int main()
{
    
    
	cin >>h >>k;
	while (h--)
		ans*=2;
	ans*=(k+1);
	cout <<ans;
	return 0;
}

Guess you like

Origin blog.csdn.net/DUXS11/article/details/132079158