회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
제가 개인적으로 공부하면서 만드는 겁니다. 도움 주실 수 있나요?
mj85232
3년 전
0
재귀를 하고 리턴 하는데 재귀를 한 시점의 변수가 되돌아 오지 않습니다.
import java.io.BufferedReader; // 자바 BufferedReader 불러오기 import java.io.InputStreamReader; // 자바 InputStreamReader 불러오기 import java.io.BufferedWriter; // 자바 BufferedWriter 불러오기 import java.io.OutputStreamWriter; // 자바 OutputStreamWriter 불러오기 import java.util.Random; // 자바 Random 불러오기 import java.io.IOException; // 자바 IOException 불러오기 class Main { // -2 (안열린 칸) // -1 (지뢰) // 0 (주변 지뢰 없음) // 1 이상 (주변 지뢰 갯수) public static int[][] Map_Create() throws IOException{ // 지뢰찾기 게임 만들기 int[][] Map = Size(); // 지뢰 생성하기 Map = Mine_creation(Map); // 주변 지뢰 숫자 for(int i = 0; i < Map.length; i++){ for(int j = 0; j < Map.length; j++){ if(Map[i][j] == -1){ // 먼저 지뢰를 찾아준다. Map = land_mines(Map, i, j); } } } return Map; } public static int[][] Size() throws IOException{ BufferedReader Br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("맵 크기를 정해주세요 : "); int Size = Integer.parseInt(Br.readLine()), Map[][] = new int[Size][Size]; return Map; } // 맵 사이즈 정하기 public static int[][] Mine_creation(int[][] Map){ Random Rd = new Random(); for(int i = 0; i < Map.length * Map.length / 10; i++) { int[] R = new int[2]; for (int j = 0; j < 2; j++){ R[j] = Rd.nextInt(Map.length-1); if(Map[R[0]][R[1]] == -1){ j--; continue; } } Map[R[0]][R[1]] = -1; } return Map; } // 지뢰 생성하기 public static int[][] land_mines(int[][] Map, int wi, int le){ for(int i = -1; i < 2; i++){ for(int j = -1; j < 2; j++){ if((wi + i != -1 && le + j != -1) && (i + wi != Map.length && le + j != Map.length)){ if(Map[wi + i][le + j] != -1){ Map[wi + i][le + j] += 1; } } } } return Map; } public static int Game(int[][] GMap, int[][] Map) throws IOException{ BufferedReader Br = new BufferedReader(new InputStreamReader(System.in)); for(int i = 0; i < GMap.length; i++){ for (int j = 0; j < GMap.length; j++){ System.out.printf("%3d", GMap[i][j]); } System.out.print("\n"); } while(true){ System.out.print("\n어디 칸을 선택 하실겁니까? : "); String[] Spick = Br.readLine().split(" "); int[] pick = {Integer.parseInt(Spick[0])-1, Integer.parseInt(Spick[1])-1}; if(Map[pick[0]][pick[1]] != -1){ GMap = open(GMap, Map, pick); } else{ System.out.print("폭탄을 밟으셨습니다."); break; } for(int i = 0; i < GMap.length; i++){ for (int j = 0; j < GMap.length; j++){ System.out.printf("%3d", GMap[i][j]); } System.out.print("\n"); } System.out.print("\n"); for(int i = 0; i < GMap.length; i++){ for (int j = 0; j < GMap.length; j++){ System.out.printf("%3d", Map[i][j]); } System.out.print("\n"); } } int a; while(true){ System.out.print("게임을 더 하시겠습니까?(더한다 1, 안한다 0) : "); a = Integer.parseInt(Br.readLine()); if(a != 1 && a != 0) System.out.print("\n알아 볼 수 없는 숫자 입니다.\n"); else break; } return a; } public static int[][] open(int[][] GMap, int[][] Map, int[] pick){ GMap[pick[0]][pick[1]] = Map[pick[0]][pick[1]]; System.out.printf("오픈 %d 자리 %d %d\n", GMap[pick[0]][pick[1]], pick[0]+1, pick[1]+1); if(GMap[pick[0]][pick[1]] == 0){ for(int i = -1; i < 2; i++){ for(int j = -1; j < 2; j++){ int[] temp = pick; if((pick[0] + i > -1 && pick[0] + i < GMap.length) && (pick[1] + j > -1 && pick[1] + j < GMap.length)){ temp[0] += i; temp[1] += j; if(GMap[pick[0]][pick[1]] == -2){ System.out.printf("재귀 %d 자리 %d %d\n", GMap[temp[0]][temp[1]], temp[0]+1, temp[1]+1); GMap = open(GMap, Map, temp); } else if(GMap[pick[0]][pick[1]] != 0){ GMap[pick[0]][pick[1]] = Map[pick[0]][pick[1]]; System.out.printf("오픈 %d 자리 %d %d\n", GMap[temp[0]][temp[1]], temp[0]+1, temp[1]+1); } } } } } System.out.printf("복귀 자리 %d %d\n\n", pick[0]+1, pick[1]+1); for(int i = 0; i < GMap.length; i++){ for (int j = 0; j < GMap.length; j++){ System.out.printf("%3d", GMap[i][j]); } System.out.print("\n"); } System.out.print("\n"); return GMap; } public static void main(String[] args) throws IOException{ // 지뢰찾기 게임 만들기 while(true){ int[][] Map = Map_Create(); int[][] GMap = new int [Map.length][Map.length]; for(int i = 0; i < GMap.length; i++){ for (int j = 0; j < GMap.length; j++){ GMap[i][j] = -2; } } Game(GMap, Map); } } }
댓글을 작성하려면
로그인
해야 합니다.
mj85232 3년 전
재귀를 하고 리턴 하는데 재귀를 한 시점의 변수가 되돌아 오지 않습니다.