UVA 10189 Java

import java.util.*;
import static java.lang.System.*;

public class main{
  public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int t = 0, h = 0, w =0;
    while(sc.hasNextInt())
    {
    	h = sc.nextInt();
    	w = sc.nextInt();
    	if(h == 0 || w == 0)
    		break;
    	if(t != 0)
    		System.out.println();
    	System.out.printf("Field #%d:\r\n", ++t);
    	char[][] map = new char[h][w];
    	for(int i = 0; i < h; i++)
    	{
    		String str = sc.next();
    		for(int j = 0; j < w; j++)
    			map[i][j] = str.charAt(j);
    	}
    	for(int i = 0; i < h; i++)
    	{
    		for(int j = 0; j < w; j++)
    		{
    			int count = 0;
    			if(map[i][j] == '*')
    				System.out.print("*");
    			else
    			{
    				if( i - 1 >= 0 && map[i - 1][j] == '*')
    					count++;
    				if( i + 1 < h && map[i + 1][j] == '*')
    					count++;
    				if( j - 1 >= 0 && map[i][j - 1] == '*')
    					count++;
    				if( j + 1 < w && map[i][j + 1] == '*')
    					count++;
    				if( i - 1 >= 0 && j - 1 >= 0 && map[i - 1][j - 1] == '*')
    					count++;
    				if( i - 1 >= 0 && j + 1 < w && map[i - 1][j + 1] == '*')
    					count++;
    				if( i + 1 < h && j - 1 >= 0 && map[i + 1][j - 1] == '*')
    					count++;
    				if( i + 1 < h && j + 1 < w && map[i + 1][j + 1] == '*')
    					count++;
    				System.out.print(count);
    			}	
    		}
    		System.out.println();
    	}
    }
  }
};

留言

這個網誌中的熱門文章

C# 井字遊戲