题目描述
思路分析
递归
会超时
通过30%
代码实现
package lanqiao;import java.util.Scanner;public class Main {public static int op[]=new int[7];public static int n,m;public static boolean conflict[][]=new boolean[7][7];public static int MOD=1000000007;public static void init(){op[1]=4;op[4]=1;op[2]=5;op[5]=2;op[3]=6;op[6]=3;}public static void main(String[] args) {init();Scanner scanner = new Scanner(System.in);n = scanner.nextInt();m=scanner.nextInt();for (int i = 0; i < m; i++) {int x=scanner.nextInt();int y=scanner.nextInt();conflict[x][y]=true;conflict[y][x]=true;}long ans=0;for (int up = 1; up <= 6; up++) {ans=(ans+4*f(up,n-1))%MOD;}System.out.println(ans);}/*** 上一层定好了朝上的数字为up的情况下,磊好cnt个筛子的方案数* @param up* @param cnt* @return*/public static long f(int up,int cnt){if(cnt==0){return 4;}long ans=0;for (int upp = 1; upp <= 6; upp++) {if(conflict[op[up]][upp]){//上一个筛子求反和此筛子比较,会冲突continue;}ans=(ans+f(upp,cnt-1))%MOD;}return ans;}
}