ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 지뢰찾기
    Coding Test/Algorithm Labs 2023. 3. 27. 15:18

     

     

    문제 :

    지뢰 찾기라는 게임은 맵에서 지뢰들이 어디에 있는지 유추해 내는 게임이다. 이 게임 프로그램은 플레이어가 어떤 지점을 클릭했을 때 그 지점 주변(상, 하, 좌, 우, 대각선, 총 8곳)에 지뢰가 몇 개가 있는지를 알려준다.

    플레이어는 가장 적은 클릭을 통해, 지뢰들이 어디에 있는지를 유추해 내는 것이 목적인 게임이다. 중간에 지뢰가 있는 지점을 클릭하면 게임오버된다.

    이때 어떤 지점을 클릭했을 때, 주변에 몇개의 지뢰들이 존재하는지를 출력하는 프로그램을 작성해 보자  

     

    입력 :

    첫째 줄에 게임의 맵의 크기를 나타내는 정수 N과 M이 주어진다. N은 맵의 행 수, M은 맵의 열 수를 나타낸다. N, M은 5 이상 100 이하의 수이다.

    둘째 줄에는 플레이어가 클릭한 지점의 위치 X와 Y가 주어진다. X는 행 번호, Y는 열 번호를 나타낸다. (행 번호는 1이상1 이상 N이하, 열 번호는 1 이상 M이하의 수이다.)

    셋째 줄부터 N줄에 걸쳐 NxM 게임 맵의 상태가 주어진다. 이때 0은 지뢰가 없는 지점을 1은 지뢰가 있는 지점을 나타낸다.

     

    출력 : 

    클릭된 지점 주변에 있는 지뢰의 개수를 출력한다. 클릭된 지점이 지뢰가 있는 지점이면 game over를 출력한다.

     

    예제 입력 :

    9 8
    4 5
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 1 0 0 0 0
    0 0 0 0 0 1 0 0
    0 0 0 1 0 1 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0

     

    예제 출력 : 

    4

     

    예제 입력 : 

    9 8
    4 6
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 1 0 0 0 0
    0 0 0 0 0 1 0 0
    0 0 0 1 0 1 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0

     

    예제 출력 :

    game over

     

     

    오답 코드

    package week3day1;
    
    import java.util.Scanner;
    
    public class SearchMine {
    
    	public static void main(String[] args) {
    		
    		Scanner sc = new Scanner(System.in);
    		
    		int n = sc.nextInt();
    		int m = sc.nextInt();
    		int x = sc.nextInt();
    		int y = sc.nextInt();
    		
    		System.out.println(n);
    		System.out.println(m);
    		System.out.println(x);
    		System.out.println(y);
    		
    		
    		int[][] arr = null;
    		
    		for(int i=1; i<=n; i++ ) {  // 배열 생성
    			for(int j=1; j<=m; j++) {
    				arr = new int[i][j];
    			}
    		}
    		
    		if(arr[x][y] == 1) {
    			System.out.println("game over");
    		}
    		else {
    			int ans = 0;
    			
    			ans += arr[x-1][y-1];
    			ans += arr[x-1][y];
    			ans += arr[x-1][y+1];
    			ans += arr[x][y-1];
    			ans += arr[x][y+1];
    			ans += arr[x+1][y-1];
    			ans += arr[x+1][y];
    			ans += arr[x+1][y+1];
    			
    			System.out.println(ans);
    		}
    
    	}
    
    }

    이런 식으로 코드를 짰는데 계속 0만 출력이 된다.. 

    배열 입력하는 코드가 잘못되었었다.

     

    정답 코드 

    package week3day1;
    
    import java.util.Scanner;
    
    public class SearchMine {
    
    	public static void main(String[] args) {
    		
    		Scanner sc = new Scanner(System.in);
    		
    		int n = sc.nextInt();
    		int m = sc.nextInt();
    		int x = sc.nextInt();
    		int y = sc.nextInt();
    		
    		
    		int[][] arr = new int[110][110];
    		
    		for(int i=1; i<=n; i++ ) {  // 배열 입력
    			for(int j=1; j<=m; j++) {
    				 arr[i][j] = sc.nextInt();
    			}
    		}
    		
    		if(arr[x][y] == 1) {
    			System.out.println("game over");
    		}
    		else {
    			int ans = 0;
    			
    			ans += arr[x-1][y-1];
    			ans += arr[x-1][y];
    			ans += arr[x-1][y+1];
    			ans += arr[x][y-1];
    			ans += arr[x][y+1];
    			ans += arr[x+1][y-1];
    			ans += arr[x+1][y];
    			ans += arr[x+1][y+1];
    			
    			System.out.println(ans);
    		}
    
    	}
    
    }

    배열 입력받는 부분을 생성하는 개념이라고 생각해 arr = new int [i][j]; 로 썼었는데

     

    arr [i][j] = sc.nextInt(); 이런 식으로 입력받아야 한다.

     

    'Coding Test > Algorithm Labs' 카테고리의 다른 글

    문자열 뒤집기  (1) 2023.03.29
    대소문자 변환  (1) 2023.03.28
    행렬 뒤집기  (1) 2023.03.23
    두번째 최소값 찾기  (2) 2023.03.23
    최대값 구하기  (0) 2023.03.21

    댓글

Designed by Tistory.