这题模拟的话只能过5个case,要想全对的话要把每个想N走的牛跟每个向E走的牛比较,这里有个细节,不能按照输入的默认顺序进行比较,要先对所有的向N走的牛按照他们x坐标的大小升序排列,对所有的向E走的牛按照他们的y坐标的大小升序排列。这样做的目的是为了确保不会有牛因为已经被其他牛挡下的牛而挡。
咱就不多废话直接上代码,因为是个新人所以可能并不是很完美(((
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int n;
static ArrayList<cow> N, E;
static int[] eatenGrass;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
StringTokenizer st;
N = new ArrayList<>();
E = new ArrayList<>();
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
String state = st.nextToken();
if (state.equals("N")) {
N.add(new cow(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), i));
} else {
E.add(new cow(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), i));
}
}
eatenGrass = new int[n];
Arrays.fill(eatenGrass, Integer.MAX_VALUE);
Collections.sort(N, new Comparator<cow>() {
@Override
public int compare(cow o1, cow o2) {
return o1.x - o2.x;
}
});
Collections.sort(E, new Comparator<cow>() {
@Override
public int compare(cow o1, cow o2) {
return o1.y - o2.y;
}
});
for (cow eCow : E) {
for (cow nCow : N) {
if (nCow.x < eCow.x || eCow.y < nCow.y) {
continue;
}
if (nCow.x - eCow.x > eCow.y - nCow.y) { // nCow win
if (eatenGrass[nCow.index] <= eCow.y - nCow.y) {
continue;
} else {
if(eatenGrass[eCow.index] == Integer.MAX_VALUE){
eatenGrass[eCow.index] = nCow.x - eCow.x;
}else{
eatenGrass[eCow.index] = Math.min(nCow.x - eCow.x, eatenGrass[eCow.index]);
}
}
} else if (nCow.x - eCow.x < eCow.y - nCow.y) { // eCow win
if (eatenGrass[eCow.index] <= nCow.x - eCow.x) {
continue;
} else {
if(eatenGrass[nCow.index] == Integer.MAX_VALUE){
eatenGrass[nCow.index] = eCow.y - nCow.y;
}else{
eatenGrass[nCow.index] = Math.min(eCow.y - nCow.y, eatenGrass[nCow.index]);
}
}
}
}
}
helpPrint();
}
public static void helpPrint() {
for (int i = 0; i < n; i++) {
if (eatenGrass[i] == Integer.MAX_VALUE) {
System.out.println("Infinity");
} else {
System.out.println(eatenGrass[i]);
}
}
}
static class cow {
int x, y, index;
public cow(int x, int y, int index) {
this.x = x;
this.y = y;
this.index = index;
}
}
}