zoukankan      html  css  js  c++  java
  • LightOJ

    https://vjudge.net/contest/317762#problem/L

    Given a set of digits S, and an integer n, you have to find how many n-digit integers are there, which contain digits that belong to S and the difference between any two adjacent digits is not more than two.

    Input

    Input starts with an integer T (≤ 300), denoting the number of test cases.

    Each case contains two integers, m (1 ≤ m < 10) and n (1 ≤ n ≤ 10). The next line will contain m integers (from 1 to 9) separated by spaces. These integers form the set S as described above. These integers will be distinct and given in ascending order.

    Output

    For each case, print the case number and the number of valid n-digit integers in a single line.

    Sample Input

    3

    3 2

    1 3 6

    3 2

    1 2 3

    3 3

    1 4 6

    Sample Output

    Case 1: 5

    Case 2: 9

    Case 3: 9

    Note

    For the first case the valid integers are

    11

    13

    31

    33

    66

    题意分析:

    给出m个数,要求组合成n位数,且相邻两位之间的差不超过2.

    解题思路:

    直接深搜,对于每一位只考虑前一位的相差2以内的数。

    #include <stdio.h>
    #include <math.h>
    #include <algorithm>
    #define N 15
    using namespace std;
    int a[N];
    int n, m, ans;
    
    void dfs(int x, int len)
    {
    	if(len==n)
    	{
    		ans++;
    		return ;	
    	}	
    	for(int i=x-2; i<=x+2; i++)
    	{
    		if(i<0 || i>=m)
    			continue;	
    
    		if(abs(a[x]-a[i])<=2)
    			dfs(i, len+1);				
    	}	
    	return ;
    }
    int main()
    {
    	int t=0, T;
    	scanf("%d", &T);
    	while(T--)
    	{
    		scanf("%d%d", &m, &n);
    		for(int i=0; i<m; i++)
    			scanf("%d", &a[i]);
    		ans=0;
    		for(int i=0; i<m; i++)
    			dfs(i, 1);
    		printf("Case %d: %d
    ", ++t, ans);
    	}
    	return 0;
    }
  • 相关阅读:
    装饰器模式
    原型模式
    观察者模式
    Apollo 代码的编译演示
    Apollo 框架的剖析1
    gPRC学习笔记
    Docker入门
    ROS入门学习
    Mudo C++网络库第十一章学习笔记
    Mudo C++网络库第十章学习笔记
  • 原文地址:https://www.cnblogs.com/zyq1758043090/p/11852530.html
Copyright © 2011-2022 走看看