Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

java写的 不知道哪里错了

Posted by WinterFarmer at 2013-04-18 19:29:25 on Problem 1915
对搜索的位置进行了放缩

import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	static int l;
	static int maxi;
	static int maxj;
	static int mini;
	static int minj;
	static int ei;
	static int ej;
	static boolean[][] vis;

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		while (n-- > 0) {
			l = s.nextInt();
			int si = s.nextInt();
			int sj = s.nextInt();
			ei = s.nextInt();
			ej = s.nextInt();
			maxi = Integer.MAX_VALUE;
			maxj = Integer.MAX_VALUE;
			mini = Integer.MIN_VALUE;
			minj = Integer.MIN_VALUE;
			vis = new boolean[l][l];
			int step = bfs(si, sj);
			System.out.println(step);
		}
	}

	static class Point {
		int i;
		int j;
		int step;

		Point(int i, int j) {
			this.i = i;
			this.j = j;
		}

		Point(int i, int j, int step) {
			this.i = i;
			this.j = j;
			this.step = step;
		}
	}

	static int bfs(int si, int sj) {
		LinkedList<Point> pnt = new LinkedList<Point>();
		visit(si, sj, vis, pnt, -1);
		while (true) {
			Point p = pnt.poll();
			if (p == null)
				return 0;
			if (p.i == ei && p.j == ej)
				return p.step;
			int i, j;
			i = p.i - 2;
			j = p.j - 1;
			if (check(i, j) && notvisit(i, j, vis))
				visit(i, j, vis, pnt, p.step);
			i = p.i - 2;
			j = p.j + 1;
			if (check(i, j) && notvisit(i, j, vis))
				visit(i, j, vis, pnt, p.step);
			i = p.i - 1;
			j = p.j - 2;
			if (check(i, j) && notvisit(i, j, vis))
				visit(i, j, vis, pnt, p.step);
			i = p.i - 1;
			j = p.j + 2;
			if (check(i, j) && notvisit(i, j, vis))
				visit(i, j, vis, pnt, p.step);
			i = p.i + 1;
			j = p.j - 2;
			if (check(i, j) && notvisit(i, j, vis))
				visit(i, j, vis, pnt, p.step);
			i = p.i + 1;
			j = p.j + 2;
			if (check(i, j) && notvisit(i, j, vis))
				visit(i, j, vis, pnt, p.step);
			i = p.i + 2;
			j = p.j - 1;
			if (check(i, j) && notvisit(i, j, vis))
				visit(i, j, vis, pnt, p.step);
			i = p.i + 2;
			j = p.j + 1;
			if (check(i, j) && notvisit(i, j, vis))
				visit(i, j, vis, pnt, p.step);
			pnt = tidy(pnt);
		}
	}

	static boolean check(int i, int j) {
		if (i >= l || i < 0 || j >= l || j < 0 || i > maxi || i < mini
				|| j > maxj || j < minj)
			return false;
		else
			return true;
	}

	static void visit(int i, int j, boolean[][] vis, LinkedList<Point> pnt,
			int laststep) {
		vis[i][j] = true;
		Point p = new Point(i, j, laststep + 1);
		pnt.addLast(p);
		setminmax(i, j);
	}

	static boolean notvisit(int i, int j, boolean[][] vis) {
		return !vis[i][j];
	}

	static void setminmax(int i0, int j0) {
		mini = Math.max(Math.min(i0, ei) - 3, mini);
		minj = Math.max(Math.min(j0, ej) - 3, minj);
		maxi = Math.min(Math.max(i0, ei) + 3, maxi);
		maxj = Math.min(Math.max(j0, ej) + 3, maxj);
	}

	static LinkedList<Point> tidy(LinkedList<Point> pnt) {
		LinkedList<Point> newpnt = new LinkedList<Point>();
		for (Point p : pnt) {
			if (check(p.i, p.j))
				newpnt.addLast(p);
		}
		return newpnt;
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator