POJ 2612

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();
	}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值