The recursive method of solving, for some special cases alone to discuss the return, such as n=3 m<2 must have no solution, such as, the recursive bottom color (1,m) color (2,m) and other direct return values.
The reason why we use N to do recursion is not much explained (maybe my algorithm is not very good, my intuition tells me to use N)
N,m Consider two cases, the newly added nth sector color is related to the next two blocks,
1. If the next two block color is different then is color (n-1,m), then the nth block has M-2 color, there is a color (n-1,m) * (M-2) species possible;
2. If the next two block is the same color then it is color (n-2,m) (the equivalent can be arbitrarily removed from the nth block next to the piece does not affect the discussion), then the nth block has M-1 color, there is a color (n-2,m) * (M-1) species possible;
In general, there are 1+2 kinds of situation.
Package foroffer;
Import Java.util.Scanner;
public class offer {public
static void Main (string[] args) {
System.out.println ("Please enter N,m");
Scanner scan = new Scanner (system.in);
int N = Scan.nextint ();
int M = Scan.nextint ();
if (N >= 1 && m>=1) {
int kind = color (n,m);
SYSTEM.OUT.PRINTLN ("Paint color Type" +kind);}
}
public static int color (int n,int M) {
int kind_new = 0;
if (N = = 1 && M >=1)
return m;
if (N = = 2 && M >=2)
return m* (M-1);
if (N = = 3 && m>=3)
return m* (M-1) * (M-2);
if (N >3 && M >=2) {
Kind_new=color (n-1,m) * (M-2) + color (n-2,m) * (M-1);//iterations are summed in two cases
}
return kind_new;
}
}