Thursday, 23 January 2014

UVa Problem ID 10189 (Mine Sweeper)

import java.io.IOException;
import java.util.StringTokenizer;

class Main implements Runnable {

    static String ReadLn(int maxLength) {  // utility function to read from stdin,
        // Provided by Programming-challenges, edit for style only
        byte line[] = new byte[maxLength];
        int length = 0;
        int input = -1;
        try {
            while (length < maxLength) {//Read untill maxlength
                input = System.in.read();
                if ((input < 0) || (input == '\n')) {
                    break; //or untill end of line ninput
                }
                line[length++] += input;
            }

            if ((input < 0) && (length == 0)) {
                return null;  // eof
            }
            return new String(line, 0, length);
        } catch (IOException e) {
            return null;
        }
    }

    public static void main(String args[]) // entry point from OS
    {
        Main myWork = new Main();  // Construct the bootloader
        myWork.run();            // execute
    }

    public void run() {
        new myStuff().run();
    }
}

class myStuff implements Runnable {
    public void run() {
        String input;
        java.util.StringTokenizer idata;
        int inputno = 0;

        while ((input = Main.ReadLn(255)) != null) {
            inputno++;
            idata = new StringTokenizer(input);
            int n = Integer.parseInt(idata.nextToken());
            int m = Integer.parseInt(idata.nextToken());
            if (n <= 0 || n > 100 || m <= 0 || m > 100) {
                break;
            }
            char inputpattern[][] = new char[n][m];
            for (int i = 0; i < n; i++) {
                String s = Main.ReadLn(255);
                for (int j = 0; j < m; j++) {
                    inputpattern[i][j] = s.charAt(j);
                }
            }
            int output[][] = new int[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (inputpattern[i][j] == '*') {
                        if (i < n - 1) {
                            output[i + 1][j] += 1;
                            if (j > 0) {
                                output[i + 1][j - 1] += 1;
                            }
                            if (j < m - 1) {
                                output[i + 1][j + 1] += 1;
                            }
                        }
                        if (i > 0) {
                            output[i - 1][j] += 1;
                            if (j > 0) {
                                output[i - 1][j - 1] += 1;
                            }
                            if (j < m - 1) {
                                output[i - 1][j + 1] += 1;
                            }
                        }
                        if (j > 0) {
                            output[i][j - 1] += 1;
                        }
                        if (j < m - 1) {
                            output[i][j + 1] += 1;
                        }
                        output[i][j] = 10;
                    }
                }
            }
            if(inputno!=1)
            {
                System.out.println("");
            }
            System.out.println("Field #" + inputno + ":");
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (output[i][j] > 8) {
                        System.out.print("*");
                    } else {
                        System.out.print(output[i][j]);
                    }
                }
               System.out.println("");
            }
        }
    }
}

Previous Post
Next Post

0 comments:

Advertisement