Mine Sweeper

From Progteam

Revision as of 15:35, 17 June 2008 by Kerry (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Checkmark.jpg This problem has been solved by mlc413.
Checkmark.jpg This problem has been solved by kerry.



Mine Sweeper
Problem Number 2612
Sorter: mlc413
Source: Unknown
Link: http://acm.pku.edu.cn/JudgeOnline/problem?id=2612



Mine Sweeper is problem number 2612 on the Peking University ACM site.

Given a map of the completed board and a grid showing which positions have been clicked, print out the current state of a minesweeper game.




import java.util.*;

public class Main{

    public static Scanner in;
    public static boolean lost = false;
 
    public static void main(String[] args){
        in=new Scanner(System.in);

        doStuff();
    }

    public static void doStuff(){
        int N=in.nextInt();
	int board [][] = new int[N+2][N+2];
	int sol [][] = new int[N+2][N+2];

	for(int i = 1; i <= N; i++){
	    String line = in.next();
	    for(int j = 1; j <=N; j++){
		board[i][j] = line.charAt(j-1);
	    }
	}

	for(int i = 1; i <= N; i++){
	    String line = in.next();
	    for(int j = 1; j <=N; j++){
		sol[i][j] = line.charAt(j-1);
	    }
	}

	solve(board, sol, N);
        
    }

    public static void solve(int[][] board, int[][] sol, int N){
	char[][] grid = new char[N+2][N+2];
	for(int i = 1; i <= N; i++){
	    for(int j = 1; j <=N; j++){
		if(sol[i][j] == 'x'){
		    if(board[i][j] == '*'){	
			lost = true;
		    }
		    else
			grid[i][j] =(char)('0' + getNeighbors(board, i, j));
		}
		else
		   grid[i][j] = '.';
	    }

	}
	if(lost){
	    for(int i = 1; i <= N; i++){
		for(int j = 1; j <=N; j++){
		    if(board[i][j] == '*'){
			grid[i][j] = '*';
		    }
		}
	    }
	}
	for(int i = 1; i <= N; i++){
	    for(int j = 1; j <=N; j++){
		System.out.print(grid[i][j]);
	    }
	    System.out.println();
	}
	
    }
    
    public static int getNeighbors(int [][] board, int i , int j){
	int neighbors = 0;
	for(int m = i-1; m <= i+1; m++){
	    for(int n = j-1; n <= j+1; n++){
		if( board[m][n] == '*'){
		    neighbors++;
		}
	    }
	}
	return neighbors;
    }
}


Kerry answer in C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>



int main() {
int n, i, j, x, y, boom, numbers[12][12];
char *board[12], *played[12], out[12][12];

        scanf("%d\n", &n);
        for(i=0;i<=n;i++) {
                board[i]=(char *)malloc(sizeof(char)*(n+4));
                played[i]=(char *)malloc(sizeof(char)*(n+4));
        }

        for(i=0;i<n;i++) {
                scanf("%s", board[i]);
        }
        for(i=0;i<n;i++) {
                scanf("%s", played[i]);
        }

        for(i=0;i<n;i++)
                for(j=0;j<n;j++) {
                        numbers[i][j]=0;
                        if ((i==0) && (j==0)) {
                                for(x=i;x<=i+1;x++)
                                        for(y=j;y<=j+1;y++)
                                                if (board[x][y]=='*')
                                                        numbers[i][j]++;
                        }
                        if ((i==0) && !(j==0)) {
                                for(x=i;x<=i+1;x++)
                                        for(y=j-1;y<=j+1;y++)
                                                if (board[x][y]=='*')
                                                        numbers[i][j]++;
                        }
                        if (i && !j) {
                                for(x=i-1;x<=i+1;x++)
                                        for(y=j;y<=j+1;y++)
                                                if (board[x][y]=='*')
                                                        numbers[i][j]++;
                        }
                        if (i && j) {
                                for(x=i-1;x<=i+1;x++)
                                        for(y=j-1;y<=j+1;y++)
                                                if (board[x][y]=='*')
                                                        numbers[i][j]++;
                        }

                }

        for(i=0;i<n;i++) {
                for (j=0;j<n;j++) {

                        if (board[i][j]=='*')
                                numbers[i][j]='*';

                        if (played[i][j]=='.')
                                out[i][j]=('.'-'0');
                        else if (played[i][j]=='x') {
                                out[i][j]=numbers[i][j];
                                if(numbers[i][j]=='*')
                                        boom=1;
                        }
                }
        }
        if(boom==1) {
                for(i=0;i<n;i++) {
                        for (j=0;j<n;j++) {
                                if (numbers[i][j]=='*')
                                        putchar('*');
                                else
                                        putchar(out[i][j]+'0');
                        }
                        printf("\n");
                }
        }
        else {
                for(i=0;i<n;i++) {
                        for (j=0;j<n;j++) {
                                putchar(out[i][j]+'0');
                        }
                        printf("\n");
                }
        }
}

Personal tools