Java 虫洞递归与贝西奶牛

Java 虫洞递归与贝西奶牛,java,algorithm,recursion,Java,Algorithm,Recursion,这是USACO网站上的描述: Farmer John周末进行高能物理实验的嗜好适得其反,造成了N个虫洞(2如果你想了解这种递归,也许其他人会经历它 我可以设置问题及其想要实现的目标: 为了让母牛掉进无限循环中,它必须在B洞下面,A洞在B洞下面 由于奶牛只在水平方向(x)移动,因此两个孔必须位于水平线上,即y1=y2 因此,如果点是A(x1,y1)和B(x2,y2),并且cow位于C(x,y),无限循环的条件是 y==y1==y2 x1小于x2 及 x1

这是USACO网站上的描述:


Farmer John周末进行高能物理实验的嗜好适得其反,造成了N个虫洞(2如果你想了解这种递归,也许其他人会经历它

我可以设置问题及其想要实现的目标:

为了让母牛掉进无限循环中,它必须在B洞下面,A洞在B洞下面

由于奶牛只在水平方向(x)移动,因此两个孔必须位于水平线上,即y1=y2

因此,如果点是A(x1,y1)和B(x2,y2),并且cow位于C(x,y),无限循环的条件是

y==y1==y2

x1小于x2


x1 import java.io.*; import java.util.*; import java.awt.*; public class codetester { public static void main(String []args) throws IOException { wormhole test = new wormhole(); BufferedReader f= new BufferedReader(new FileReader("wormhole.in")); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("wormhole.out"))); int N=Integer.parseInt(f.readLine()); ArrayList<Point> rawdata = new ArrayList<Point>(); for(int i=0;i<N;i++) { String [] twocoords=f.readLine().split(" "); int x=Integer.parseInt(twocoords[0]); int y=Integer.parseInt(twocoords[1]); rawdata.add(new Point(x,y)); } } public ArrayList<ArrayList<Pair<Point,Point>>> possiblepairs(ArrayList<Point> raw) { ArrayList<Pair<Point,Point>> a= new ArrayList<Pair<Point,Point>>(); iterate(raw,a,0,1); return a; } public void iterate(ArrayList<Point> raw, ArrayList<Pair<Point,Point>> a, int firstpoint, int secondpoint) { int count=0; if(secondpoint<=raw.size()-1) { a.add(new Pair(raw.get(firstpoint),raw.get(secondpoint))); iterate(raw,a,firstpoint,secondpoint+1); } else { count++; iterate(raw,a,firstpoint+count,firstpoint+count+1); } } public boolean wormholeinrow(Point currentcoords, ArrayList<Point> raw) { boolean inrow=false; int y=currentcoords.y; int x=currentcoords.x; for(Point a: raw) { if(a.getY()==y && a.getX()>x) { inrow=true; //made need to return these coords instead. will allow us to transport bessie to next wormhole or decide if she is done. } } return inrow; //if raw y coords contains y. then there is still stuff in the same row. but check if the x coord is greater to see if the point is to the right of the currentcoords. } public static class Pair<K, V> { private final K element0; private final V element1; public static <K, V> Pair<K, V> createPair(K element0, V element1) { return new Pair<K, V>(element0, element1); } public Pair(K element0, V element1) { this.element0 = element0; this.element1 = element1; } public K getElement0() { return element0; } public V getElement1() { return element1; } } }