Notice
Recent Posts
Recent Comments
Link
«   2026/09   »
일 월 화 수 목 금 토
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Tags more
Archives
Today
Total
관리 메뉴

seaking110 님의 블로그

N과 M 본문

오늘의 문제

N과 M

seaking110 2025. 1. 6. 17:45

문제 

 

풀이 코드

public class Main {
	public static int []arr;
	public static boolean [] visited;
	public static StringBuilder sb = new StringBuilder();
	public static int n;
	public static int m;
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(bf.readLine());
        n = Integer.parseInt(st.nextToken());
        m = Integer.parseInt(st.nextToken());
        arr = new int[m];
        visited = new boolean[n+1];
        dfs(0);
        System.out.println(sb);
    }
    public static void dfs(int dep) {
    	if(dep == m) {
    		for(int i=0;i<arr.length;i++) {
    			sb.append(arr[i]+" ");
    		}
    		sb.append("\n");
    		return;
    	}
    	for(int i=1;i<=n;i++) {
    		if(!visited[i]) {
    			arr[dep] = i;
    			visited[i] = true;
        		dfs(dep+1);
        		visited[i] = false;
    		}
    	}
    	
    }
}

 


문제 

풀이 코드

 

public class Main {
	public static int []arr;
	public static boolean [] visited;
	public static StringBuilder sb = new StringBuilder();
	public static int n;
	public static int m;
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(bf.readLine());
        n = Integer.parseInt(st.nextToken());
        m = Integer.parseInt(st.nextToken());
        arr = new int[m];
        visited = new boolean[n+1];
        dfs(0,1);
        System.out.println(sb);
    }
    public static void dfs(int dep, int start) {
    	if(dep==m) {
    		for(int a : arr) {
    			sb.append(a+" ");
    		}
    		sb.append("\n");
    		return;
    	}
    	for(int i=start;i<=n;i++) {
    		if(!visited[i]) {
    			visited[i] = true;
    			arr[dep] = i;
    			dfs(dep+1,i);
    			visited[i] = false;
    		}
    	}
    }
}

 

문제 

풀이 코드

public class Main {
	public static int []arr;
	public static StringBuilder sb = new StringBuilder();
	public static int n;
	public static int m;
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(bf.readLine());
        n = Integer.parseInt(st.nextToken());
        m = Integer.parseInt(st.nextToken());
        arr = new int[m];
        dfs(0);
        System.out.println(sb);
    }
    public static void dfs(int dep) {
    	if(dep==m) {
    		for(int a : arr) {
    			sb.append(a+" ");
    		}
    		sb.append("\n");
    		return;
    	}
    	for(int i=1;i<=n;i++) {
    			arr[dep] = i;
    			dfs(dep+1);

    	}
    }
}

 

문제 

풀이 코드

 

public class Main {
	public static int []arr;
	public static StringBuilder sb = new StringBuilder();
	public static int n;
	public static int m;
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(bf.readLine());
        n = Integer.parseInt(st.nextToken());
        m = Integer.parseInt(st.nextToken());
        arr = new int[m];
        dfs(0,1);
        System.out.println(sb);
    }
    public static void dfs(int dep, int start) {
    	if(dep==m) {
    		for(int a : arr) {
    			sb.append(a+" ");
    		}
    		sb.append("\n");
    		return;
    	}
    	for(int i=start;i<=n;i++) {
    			arr[dep] = i;
    			dfs(dep+1,i);
    	}
    }
}

 

백트래킹 유형의 N과 M 4문제를 풀어봤다! 백트래킹의 기초는 연습했고 내일부턴 백트래킹 심화문제들을 풀어보려고 한다!

'오늘의 문제' 카테고리의 다른 글

1992번 퀴드트리  (0) 2025.01.14
14889번 : 스타트와 링크  (1) 2025.01.13
14888번 연산자 끼워넣기  (0) 2025.01.09
9663번 N-Queen  (0) 2025.01.07
백준 1316번 그룹 단어 체커  (0) 2025.01.03