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

there are some bugs in my code!

Posted by 200811317 at 2009-04-11 06:16:48 on Problem 2612 and last updated at 2009-04-11 06:21:32
Hi, I can't see where the error is!
It can't count the surrounding stars correctly! it always ignores some of them!

Note: You can see lots of commented lines, which I use for debugging.

// Code begins
import java.util.*;

public class Main {
	public static boolean[][] mined;
	public static boolean[][] opened;
	public static boolean exploded = false;
	public static int n;

	public static void main(String[] args) {
		Scanner scanInt = new Scanner(System.in);
		 Scanner scanLine = /*new Scanner(
			//	"...**..*\n......*.\n....*...\n........\n........\n.....*..\n...**.*.\n.....*..\nxxx.....\nxxxx....\nxxxx....\nxxxxx...\nxxxxx...\nxxxxx...\nxxx.....\nxxxxx...");
		//*/new Scanner(System.in);

		n = /*8;// */scanInt.nextInt();
		// System.out.println(n);
		mined = new boolean[n][n];
		opened = new boolean[n][n];

		for (int i = 0; i < n; i++) {
			String line = scanLine.nextLine(); // .trim();
			// System.out.println(line);
			for (int j = 0; j < n; j++) {
				if (line.charAt(j) == '*') {
					mined[i][j] = true;
				} else {
					mined[i][j] = false;
				}
				// System.out.println(mined[i][j]);
			}
		}

		for (int i = 0; i < n; i++) {
			String line = scanLine.nextLine(); // .trim();
			for (int j = 0; j < n; j++) {
				if (line.charAt(j) == 'x') {
					opened[i][j] = true;
					if (mined[i][j]) {
						exploded = true;
					}
				} else {
					opened[i][j] = false;
				}
			}
		}

		// print(mined);
		// print(opened);

		// System.exit(0);
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				System.out.print(getSqr(i, j));
			}
			System.out.println();
		}
	}

	public static char getSqr(final int x, final int y) {
		if (exploded && mined[x][y]) {
			return '*';
		} else if (!opened[x][y]) {
			return '.';
		}

		int count = 0, iTo, jTo;

		int i = ((x - 1) >= 0) ? x - 1 : x;
		iTo = ((x + 1) < n) ? x + 1 : x;

		int j = ((y - 1) >= 0) ? y - 1 : y;
		jTo = ((y + 1) < n) ? y + 1 : y;

		for (; i <= iTo; i++) {
			for (; j <= jTo; j++) {
				
				if (i != x && j != y && mined[i][j]) {
					count++;
				}
			}
		}
		char ret = ("" + count).charAt(0);
		return ret;
	}

	public static void print(boolean[][] a) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				System.out.print(((a[i][j]) ? 'x' : '.') + "" + i + "," + j
						+ " ");
			}
			System.out.println();
		}
		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