ACM之跳骚---ShinePans

Description

Z城市居住着非常多仅仅跳蚤。在Z城市周六生活频道有一个娱乐节目。一仅仅跳蚤将被请上一个高空钢丝的正中央。钢丝非常长,能够看作是无限长。节目主持人会给该跳蚤发一张卡片。卡片上写有N+1个自然数。当中最后一个是M,而前N个数都不超过M,卡片上同意有同样的数字。跳蚤每次能够从卡片上随意选择一个自然数S,然后向左,或向右跳S个单位长度。而他终于的任务是跳到距离他左边一个单位长度的地方,并捡起位于那里的礼物。 
比方当N=2,M=18时,持有卡片(10, 15, 18)的跳蚤,就能够完毕任务:他能够先向左跳10个单位长度,然后再连向左跳3次,每次15个单位长度,最后再向右连跳3次,每次18个单位长度。而持有卡片(12, 15, 18)的跳蚤,则怎么也不可能跳到距他左边一个单位长度的地方。 
当确定N和M后,显然一共同拥有M^N张不同的卡片。如今的问题是,在这全部的卡片中,有多少张能够完毕任务。 

Input

两个整数N和M(N <= 15 , M <= 100000000)。

Output

能够完毕任务的卡片数。

Sample Input

2 4

Sample Output

12

Hint

这12张卡片各自是: 
(1, 1, 4), (1, 2, 4), (1, 3, 4), (1, 4, 4), (2, 1, 4), (2, 3, 4), 
(3, 1, 4), (3, 2, 4), (3, 3, 4), (3, 4, 4), (4, 1, 4), (4, 3, 4) 

參考了下网上的解法,发现对逻辑的需求还是很高的!自己能力还不足

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>  //system(); 这个指令须要用到此头文件
#include <ctype.h> //toupper要用到
#include <malloc.h> //在内存管理时用到的头文件
#include <string.h>  //字符串的头文件
#include <math.h>  //数学运算

int m = 0, n = 0, c[1000] = { NULL }, position = 0, count_can = 0;

bool jump(int x[])
{
	int temp1[1000] = { NULL }, temp2[1000] = { NULL };
	for (int i = 0; x[i] != NULL; i++)
	{
		if (x[i] == 1)
			return(true);
		temp1[i] = x[i];
	}
	int l = 1, s = 0;
	do{
		s = 0;
		if (l == 1)
		{

			for (int i = 0; temp1[i] != NULL; i++)
			{
				for (int j = i + 1; temp1[j] != NULL; j++)
				{

					if (fabs(temp1[i] - temp1[j]) == 1)
					{
						return(true);
					}
					if (temp1[i] - temp1[j] != 0)
					{
						temp2[s] = (int)fabs(temp1[i] - temp1[j]);
						s++;
					}
				}
				temp1[i] = 0;
			}
			l = 2;
		}
		else
		{

			for (int i = 0; temp2[i] != NULL; i++)
			{
				for (int j = i + 1; temp2[j] != NULL; j++)
				{
					if (fabs(temp2[i] - temp2[j]) == 1)
					{
						return(true);
					}
					if (temp2[i] - temp2[j] != 0)
					{
						temp1[s] = (int)fabs(temp2[i] - temp2[j]);
						s++;
					}
				}
				temp2[i] = 0;
			}
			l = 1;
		}

	} while (s != 0);
	return(false);

}
void card(int depth)
{
	if (depth == m)
	{
		c[m] = n;
		if (jump(c) == true)
		{
			count_can++;
		}
		return;
	}
	else
	{
		for (int i = 1; i <= n; i++)
		{
			c[depth] = i;
			depth++;
			card(depth);
			depth--;
		}
		return;
	}
}
void main()
{
	scanf("%d%d", &m, &n);
	card(0);
	printf("%d
", count_can);
}


原文地址:https://www.cnblogs.com/mfrbuaa/p/4215534.html