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写的,大牛帮忙看下为啥WA

Posted by mmx21 at 2010-08-04 12:05:47 on Problem 2488
import java.io.*;
import java.util.*;

public class Main {

	public static int[][] dir = {{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
	public static int times;
	public static int m,n;
	public static boolean[][] flag = new boolean[50][50];
	public static boolean tag;
	public static int[] tracex = new int[50];
	public static int[] tracey = new int[50];
	public static void DFS(int i, int j)
	{
		if(tag) return;
		int x,y;
		times++;
		tracex[times] = i;
		tracey[times] = j;
		if(times == m*n)
		{
			tag = true;
			return;
		}
		flag[i][j] = false;
		for(int p=0;p<8;p++)
		{
			x = i+dir[p][1];
			y = j+dir[p][0];
			if(x>0&&x<=m&&y>0&&y<=n&&flag[x][y])
			{
				DFS(x,y);
				times--;
			}
		}
		flag[i][j]=true;
	}
	
	public static void main(String[] args) 
	{
		int i,j,k;
		Scanner cin = new Scanner(new BufferedInputStream(System.in));
		int count = cin.nextInt();
		for(i=1;i<=count;i++)
		{
			times = 0;
			tag = false;
			m = cin.nextInt();
			n = cin.nextInt();
			for(k=1;k<50;k++)
				for(j=1;j<50;j++)
					flag[k][j]=true;
			DFS(1,1);
			System.out.println("Scenario #" + i +":");
			if(tag)
			{
				for(j=1;j<=m*n;j++)
				{
					char c = (char) (tracey[j]+64);
					System.out.print(c);
					System.out.print(tracex[j]);
				}
				System.out.println();
			}
			else
				System.out.println("Impossible");
			if(i!=count)
				System.out.println();
		}
	}

}

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