블 루 브리지 컵 주간 연습 의"지뢰 제거"

37092 단어 자바알고리즘
[문제 설명]
Have you ever played Minesweeper? It’s a cute little game which comes within a certain Operating
System which name we can’t really remember. Well, the goal of the game is to find where are all the mines within a M × N field. To help you, the game shows a number in a square which tells you how many mines there are adjacent to that square. For instance, supose the following 4 × 4 field with 2 mines (which are represented by an ‘*’ character):
*…

.*…

If we would represent the same field placing the hint numbers described above, we would end up
with:
*100
2210
1*10
1110
As you may have already noticed, each square may have at most 8 adjacent squares.
[입력]
The input will consist of an arbitrary number of fields. The first line of each field contains two integers
n and m (0 < n, m ≤ 100) which stands for the number of lines and columns of the field respectively.
The next n lines contains exactly m characters and represent the field.
Each safe square is represented by an ‘.’ character (without the quotes) and each mine square
is represented by an ‘*’ character (also without the quotes). The first field line where n = m = 0
represents the end of input and should not be processed.
[출력]
매 쌍 의 정수 i 와 j 에 대해 원래 의 순서에 따라 i 와 j 를 출력 한 다음 에 양자 간 의 정수 중 최대 순환 절 길 이 를 출력 한다.이 세 개의 정 수 는 하나의 빈 칸 으로 분리 하고 같은 줄 에서 출력 해 야 한다.읽 은 모든 그룹의 데 이 터 는 출력 에서 단독 줄 에 있어 야 합 니 다.
[샘플 입력]
4 4
*…

.*…

3 5
**…

.*…
0 0
[샘플 출력]
Field #1:
*100
2210
1*10
1110
Field #2:
**100
33200
1*100
*/
나의 입 출력 과 문제 요 구 는 약간 다르다.단지 그 중의 알고리즘 을 연습 하기 위해 서 였 을 뿐 완전히 일치 하지 않 았 다.
package    ;

import java.util.Scanner;

public class    {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int[][] num = new int[100][2];
		int k = 0;
		while(true) {
			System.out.print("                    :");
			int n = 0,m = 0;
			try {
				n = in.nextInt();
				m = in.nextInt();
			}
			catch(Exception e) {
				System.out.println("        ,     ");
				in.nextLine();
				continue;
			}
			if(n == 0 && m == 0) {
				System.out.println("    !");
				break;
			}
			if(n < 2 || m <2) {
				System.out.println("       2x2!!,     !");
				continue;
			}
			char[][] flag = new char[n][m];
			char[][] res = new char[n][m];
			//       
			for(int i = 0;i < n;i++) {
				for(int j = 0;j < m;j++) {
					res[i][j] = '0';
				}
			}
			System.out.println("     (           ,     enter  ):");
	 		//          
			for(int i = 0;i < n;i++) {
				String str = in.next();
				if(str.length() != m) {
					System.out.println("        !       ");
					i--;
					continue;
				}
				for(int j = 0;j < m;j++) {
					flag[i][j] = str.charAt(j);
				}
			}
			//    
			for(int i = 0;i < n;i++) {
				for(int j = 0;j < m;j++) {
					if(flag[i][j] == '*') {
						res[i][j] = '*';
						find(i,j,n-1,m-1,res);
					}
				}
			}
			//      
			for(int i = 0;i < n;i++) {
				for(int j = 0;j < m;j++) {
					System.out.print(res[i][j]+" ");
				}
				System.out.println();
			}
			System.out.println("-----------------------------------------------------------------");
		}
	}
	public static void find(int i,int j,int n,int m,char[][] res) {
		//          
		if(i == 0 && j == 0) {
			add(i,j+1,res);
			add(i+1,j+1,res);
			add(i+1,j,res);
		}
		else if(i == 0 && j == m) {
			add(i,j-1,res);
			add(i+1,j-1,res);
			add(i+1,j,res);
		}
		else if(i == n && j == 0) {
			add(i-1,j,res);
			add(i-1,j+1,res);
			add(i,j+1,res);
		}
		else if(i == n && j == m) {
			add(i,j-1,res);
			add(i-1,j-1,res);
			add(i-1,j,res);
		}
		//            
		else if(i == 0) {
			add(i,j-1,res);
			add(i+1,j-1,res);
			add(i+1,j,res);
			add(i+1,j+1,res);
			add(i,j+1,res);
		}
		else if(j == 0) {
			add(i-1,j,res);
			add(i-1,j+1,res);
			add(i,j+1,res);
			add(i+1,j+1,res);
			add(i+1,j,res);
		}
		else if(i == n) {
			add(i,j-1,res);
			add(i-1,j-1,res);
			add(i-1,j,res);
			add(i-1,j+1,res);
			add(i,j+1,res);
		}
		else if(j == m) {
			add(i-1,j,res);
			add(i-1,j-1,res);
			add(i,j-1,res);
			add(i+1,j-1,res);
			add(i+1,j,res);
		}
		//          
		else {
			add(i-1,j-1,res);
			add(i-1,j,res);
			add(i-1,j+1,res);
			add(i,j+1,res);
			add(i+1,j+1,res);
			add(i+1,j,res);
			add(i+1,j-1,res);
			add(i,j-1,res);
		}
	}
	public static void add(int i,int j,char[][] res) {
		if(res[i][j] != '*')
			res[i][j] = (char)(res[i][j]+1);
		else
			return;
	}
}

좋은 웹페이지 즐겨찾기