PTA 球队“食物链” Java

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());
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值