挑战编程 程序设计竞赛训练手册-1.6.2 扫雷(Minesweeper)
2013-01-21 20:22
537 查看
挑战编程 程序设计竞赛训练手册-1.6.2 扫雷(Minesweeper)
//import java.util.Scanner; public class problem_Minesweeper { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub char[][] chstr = { {'*','*','`','`','`'},{'`','`','`','`','`'},{'`','*','`','`','`'}}; //System.out.println("原始图:"); print(chstr); count(chstr);//计算格子中数字 //System.out.println("结果图:"); print(chstr); } private static void print(char[][] chstr){ //打印二维字符数组 for(int i=0;i<chstr.length;i++){//循环一维长度 for(int j=0;j<chstr[i].length;j++){//在每个一维下,循环二维长度 System.out.print(chstr[i][j]+" ");//输出单个字符 } System.out.println(""); System.out.println(""); } } private static void count(char[][] chstr) { //计算位置,执行'*'周围方格+1 for (int i = 0; i < chstr.length; i++) {//循环查找二维字符组 for (int j = 0; j < chstr[i].length; j++) { if (chstr[i][j]=='*') {//如果是'*',周围数字+1 chstr = aroundaddone(i,j,chstr.length,chstr[i].length,chstr);//执行周围数字+1操作 //System.out.println("chstr.length"+chstr.length); }else if(chstr[i][j]=='`'){//残留'`'字符,把其改为'0' chstr[i][j]='0';} } } } private static char[][] aroundaddone(int i,int j,int iLength,int jLength,char[][] chstr) {//周围数字+1操作 // TODO Auto-generated method stub for(int a=i-1;a<=i+1;a++){//控制y轴位置 for(int b = j-1;b<=j+1;b++){//控制x轴位置,限制在周围一圈的位置内+1 if(judgePosition(iLength,jLength,a,b)&&(chstr[a][b]!='*')){//超出扫雷的边界不执行操作 if(chstr[a][b]=='`'){//在边界内,如果是'`'字符,则改为0 chstr[a][b]='0';} int temp = chstr[a][b];//为当前位置执行+1操作 temp++; chstr[a][b]=(char)temp; }else{ } } } return chstr;//返回更新后的二维字符组 } private static boolean judgePosition(int strLength, int chLength, int i, int j) {//判断周围的位置 boolean bool = false; if((i<0)||(j<0)||(i>strLength-1)||(j>chLength-1)){ //如果达到最顶,最底,最左,最右皆超出扫雷边界 }else{ //超出边界不能+1 bool = true;//否则返回真,表示可以执行+1操作 } return bool; } }
相关文章推荐
- 挑战编程 程序设计竞赛训练手册-1.6.7 将军(Check_the_Check)
- 挑战编程 程序设计竞赛训练手册-1.6.8 澳大利亚投票(Australian Voting)
- 挑战编程程序设计竞赛训练手册(Programming Challenges)
- 序-挑战编程 程序设计竞赛训练手册
- 挑战编程 程序设计竞赛训练手册-1.6.1 3n+1问题(3n+1 Problem)
- 挑战编程 程序设计竞赛训练手册-1.6.4 液晶显示屏(LC-Display)
- 挑战编程 程序设计竞赛训练手册-1.6.5 图形化编辑器(Graphical Editor)
- 挑战编程 程序设计竞赛训练手册-1.6.3 旅行(The Trip)
- 挑战编程 程序设计竞赛训练手册-1.6.6 解释器(Interpreter)
- 程序设计竞赛ACM训练手册--从入门到精通
- 系统性训练,励志刷完挑战程序设计竞赛-代码整理135~【中级篇】
- 系统性训练,励志刷完挑战程序设计竞赛-代码整理1~42【初级篇】
- 系统性训练,励志刷完挑战程序设计竞赛-代码整理43~68【初级篇】
- 系统性训练,励志刷完挑战程序设计竞赛-代码整理68~103【初级篇】
- ACM题解系列之八:(美)斯基纳等:《挑战编程-程序设计竞赛训练手册》
- 系统性训练,励志刷完挑战程序设计竞赛-代码整理103~134【初级篇】
- [挑战程序设计竞赛] POJ 1862 - Stripies
- 挑战程序设计竞赛:0101抽签
- poj3009深度优先搜索<挑战程序设计竞赛>
- 挑战程序设计竞赛 部分和问题