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

我用的是递归总是RE! 怎么改?????????

Posted by 589742 at 2006-09-27 20:28:05 on Problem 2386
import java.io.*;
import java.util.*;

public class Main {

	private int row = 1;
	private int column = 1;
	private char[][] array;
	private int number = 0;
	
	public void init() {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in),65536);
			String num = br.readLine();
			StringTokenizer st = new StringTokenizer(num);
			if(st.hasMoreTokens()) {
				row = Integer.parseInt(st.nextToken());
			}
			if(st.hasMoreTokens()) {
				column = Integer.parseInt(st.nextToken());
			}
			array = new char[row][column];
            for(int i = 0;i < row;i ++) {
            	String temp = br.readLine();
            	for(int j = 0;j < column;j ++) {
            	    array[i][j] =  temp.charAt(j);
            	}
            }
		} catch(Exception e) {}
	}
	
	public int getNumber(int i,int j,int top,int left) {
		int flag = 0;
		if(array[i][j] == 'W') {
 			array[i][j] = '.';
			flag = 1;
			if((i > top+1 && i < row)  || (i == top+1 && j > left+1 && j < column)) {
				if(i-1 >= 0) {
					if(j-1 >= 0) {
				        getNumber(i-1,j-1,top,left);
				        getNumber(i,j-1,top,left);
					}
				    getNumber(i-1,j,top,left);
				    if(j+1 < column) {
				        getNumber(i-1,j+1,top,left);
				        getNumber(i,j+1,top,left);
				    }
				}
				if(i+1 < row) {
					if(j-1 >= 0) {
				        getNumber(i+1,j-1,top,left);
					}
				    getNumber(i+1,j,top,left);
				    if(j+1 < column) {
				        getNumber(i+1,j+1,top,left);
				    }
				}
			} else {
				if(j-1 > 0) {
					getNumber(i,j-1,top,left);
				}
				if(j+1 < column) {
					getNumber(i,j+1,top,left);
				}
				if(i+1 < row) {
					if(j-1 >= 0) {
				        getNumber(i+1,j-1,top,left);
					}
				    getNumber(i+1,j,top,left);
				    if(j+1 < column) {
				        getNumber(i+1,j+1,top,left);
				    }
				}
			} 
			
		}
		return flag;
	}
	
	public void run() {
		for(int i = 0;i < row;i ++) {
        	for(int j = 0;j < column;j ++) {
        	    int m = getNumber(i,j,i,j);
        	    number = number + m;
        	}
        }
		System.out.println(number);
	}
	
	public static void main(String[] args) {
        Main lc = new Main();
        lc.init();
        lc.run();
	}
}


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