Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
我用的是递归总是RE! 怎么改?????????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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator