`
woxiaoe
  • 浏览: 277065 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

POJ 2612

    博客分类:
  • ACM
J# 
阅读更多

http://acm.pku.edu.cn/JudgeOnline/problem?id=2612

输入布局,和操作,

如果点位空白要输出该点周围的雷数(0~8)

但没有点到雷这输出操作往后的布局。如果点到了雷,还要输出所有的雷,

package com.woxiaoe.acm.pku.P2612;

import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
	static char[][] table,touchTable,showTable;
	static int[][] counts;
	static int[] X = {0,-1,-1,-1,0,1,1,1};
	static int[] Y = {-1,-1,0,1,1,1,0,-1};
	public static void main(String[] args) {
		//Scanner scn = new Scanner(System.in);
		Scanner scn = new Scanner(Main.class.getResourceAsStream("in.dat"));
		PrintWriter out = new PrintWriter(System.out);
		int n;
		n = scn.nextInt();
		table = new char[n][n];
		touchTable = new char[n][n];
		showTable = new char[n][n];
		counts = new int[n][n];
		boolean touched = false;
		for(int i = 0; i < n; i++){
			table[i] = scn.next().toCharArray();
		}
		for(int i = 0; i < n; i++){
			touchTable[i] = scn.next().toCharArray();
		}
		int xi = 0 ,yj = 0;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				if(table[i][j] == '*'){
					for(int k = 0; k < 8; k ++){
						xi = i + X[k];
						yj = j + Y[k];
						if((xi < n && xi >= 0) && (yj < n && yj >= 0) && table[xi][yj] != 'x'){
							counts[xi][yj]++;
						}
					}
					
				}
			}
		}
		for(int i = 0; i < n; i++){
			for(int j = 0 ; j < n; j++){
				showTable[i][j] = '.';
				if(touchTable[i][j] == 'x' && table[i][j] == '.'){//没有猜到雷的情况
					showTable[i][j] = (char)(counts[i][j] + 48); 
				}else if(touchTable[i][j] == 'x' && table[i][j] == '*'){
					touched = true;
				}
			}
		}
		if(touched){
			for(int i = 0 ; i < n; i++){
				for(int j = 0; j < n; j++){
					if(table[i][j] == '*'){
						showTable[i][j] = '*';
					}
				}
			}
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j ++){
				out.print(showTable[i][j]);
			}
			out.println();
		}
		out.flush();
	}
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics