Coloring problem a circle is divided into N-sector, M-color, the number of coloring methods of the heavy-color of adjacent blocks

Source: Internet
Author: User

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; 
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.