栈溢出了 求助
查看原帖
栈溢出了 求助
1028188
xningi楼主2024/4/11 18:30
package 搜索;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class P1088火星人 {
	static int N = 10010;
	static int[] a = new int[N];
	static int[] mars = new int[N];
	static boolean[] st = new boolean[N];
	static int res = 0;
	static int n;
	static int num;
	static boolean flag = false;
	static FastRead fr = new FastRead();
	public static void main(String[] args) throws Exception {
		n = fr.ReadInt();
		num = fr.ReadInt();
		for(int i = 1; i <= n; i ++) {
			mars[i] = fr.ReadInt();
		}
		dfs(1);
		
	}
	
	public static void dfs(int x) {
		if(flag)
			return;
		if(x > n) {
			res ++;
			if(res == num + 1) {
				flag = true;
				for(int i = 1; i <= n; i ++) {
					System.out.print(a[i] + " ");
				}
			}
			return;
		}
		
		for(int i = 1; i <= n; i ++) {
			if(res == 0) {
				i = mars[x];
			}
			if(!st[i]) {
				st[i] = true;
				a[x] = i;
				dfs(x + 1);
				st[i] = false;
				a[x] = 0;
				
			}
		}
	}
	
	static class FastRead {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StreamTokenizer st = new StreamTokenizer(br);
		
		public int ReadInt() throws Exception {
			st.nextToken();
			return (int)st.nval;
		}
	}

}

2024/4/11 18:30
加载中...