PTA 球队“食物链” Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
// 数据读取
int N = Reader.nextInt();
map = new char[N][];
ans = new int[N];
for (int i = 0; i < map.length; i++) {
String s = Reader.nextLine();
map[i] = s.toCharArray();
other.add(i);
}
// 处理数据,应对出现LL的情况
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (map[i][j] == 'L') {
if (map[j][i] == 'L') {
map[i][j] = 'W';
}
map[j][i] = 'W';
}
}
}
// dfs
dfs(0);
// 如果一直都没有找到,则表示没有食物链
if (count == 0) {
System.out.println("No Solution");
}
}
static int[] ans;
static int count = 0;
static char[][] map;
static boolean find = false;
static Set<Integer> other = new HashSet<Integer>();
// 剪枝,如果剩下的球队中没有可以战胜第一支球队的球队,则接下来不需要进行探测了
static boolean cut() {
for (int item : other) {
if (map[item][ans[0] - 1] == 'W') {
return true;
}
}
return false;
}
static void dfs(int index) {
// 当前球队入队,从原始集合中去除
other.remove(index);
ans[count++] = index + 1;
// 如果该球队是最后一支球队,则直接与开头的球队进行比较
if (other.isEmpty()) {
if (map[index][ans[0] - 1] == 'W') {
show();
find = true;
}
return;
}
// 剪枝
if (cut()) {
// 从当前球队战胜的球队(编号升序)中开始进行查找
for (int i = 0; i < map[0].length; i++) {
if (map[index][i] == 'W' && other.contains(i)) {
dfs(i);
if (find)
return;
}
}
}
// 没有找到,回溯
count--;
other.add(index);
}
static void show() {
for (int i = 0; i < ans.length - 1; i++) {
System.out.print(ans[i] + " ");
}
System.out.println(ans[ans.length - 1]);
}
}
// Class for buffered reading int and double values *//*
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
// ** call this method to initialize reader for InputStream *//*
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static void init(File file) {
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tokenizer = new StringTokenizer("");
}
// ** get next word *//*
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
// TODO add check for eof if necessary
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static String nextLine() throws IOException {
return reader.readLine();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static char nextChar() throws IOException {
return next().toCharArray()[0];
}
static float nextFloat() throws IOException {
return Float.parseFloat(next());
}
static Double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}