解题思路:
注意事项:
参考代码:
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main09 { static int n,m,count=0,count1=0; static int[][] arr; static int[][] arr2=new int[21000][100]; static char[][] arr3=new char[21000][100]; static List<String> list=new ArrayList<String>(); public static boolean panduan(int[] sai) { for(int i=0;i<m;i++) { int h=1; for(int j=0;j<sai.length;j++) { if(sai[j]==arr[i][h]) { h++; if(h==arr[i][0]+1) break; } } if(!(h==arr[i][0]+1&&arr[i][arr[i][0]+1]==1)&&!(h!=arr[i][0]+1&&arr[i][arr[i][0]+1]==0)) { return false; } } return true; } public static void bfs(int a1[],int n,int k) { if(k==n) { if(panduan(a1)) { for (int i = 0; i < a1.length; i++) { arr2[count][i]=a1[i]; } count++; } return; } for (int i = k; i < n; i++) { swap(a1,k,i); bfs(a1,n,k+1); swap(a1,k,i); } } public static void swap(int a1[],int i,int j) { int temp=a1[i]; a1[i]=a1[j]; a1[j]=temp; } public static void main(String[] args) { Scanner in=new Scanner(System.in); n=in.nextInt(); m=in.nextInt(); int[] a1=new int[n]; arr=new int[m][100]; for (int i = 0; i < n; i++) { a1[i]=i; } for (int i = 0; i < m; i++) { int c=in.nextInt(); arr[i][0]=c; for (int j = 1; j<=c+1; j++) { arr[i][j]=in.nextInt(); } } bfs(a1,n,0); System.out.println(count); for (int i = 0; i <count; i++) { String s=""; for (int j = 0; j < n; j++) { s+=arr2[i][j]; } list.add(s); } Collections.sort(list); for (int i = 0; i < list.size(); i++) { arr3[count1]=list.get(i).toCharArray(); count1++; } for (int i = 0; i < count1; i++) { for (int j = 0; j <n; j++) { System.out.print(arr3[i][j]+" "); } System.out.println(); } } }
0.0分
1 人评分
川哥的吩咐 (C++代码)浏览:1077 |
C语言程序设计教程(第三版)课后习题9.3 (Java代码)浏览:1025 |
A+B for Input-Output Practice (IV) (C++代码)浏览:713 |
C语言程序设计教程(第三版)课后习题7.2 (C语言代码)浏览:686 |
2006年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:674 |
完数 (C语言代码)浏览:757 |
C语言程序设计教程(第三版)课后习题11.1 (C语言代码)浏览:651 |
K-进制数 (C语言描述,蓝桥杯)浏览:955 |
打印十字图 (C语言代码)浏览:2822 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:1100 |