728x90
난이도 | Silver1 |
링크 | https://www.acmicpc.net/problem/13908 |
문제 사진
풀이.
다 해보는 브루트 포스 문제
푸는 방법만 안다면, 쉽게 풀 수 있다. M과 N 문제와 비슷한 유형
소스코드.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int N,M,count;
public static void main(String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stz=new StringTokenizer(br.readLine());
N=Integer.parseInt(stz.nextToken());
M=Integer.parseInt(stz.nextToken());
count=0;
boolean[] visited=new boolean[10];
if(M!=0) {
stz=new StringTokenizer(br.readLine());
}
for(int i=0;i<M;i++) {
int index=Integer.parseInt(stz.nextToken());
visited[index]=true;
}
dfs(0,0,visited);
System.out.println(count);
}
public static void dfs(int depth,int num,boolean[] visited) {
if(depth==N) {
if(num==M) {
count++;
}
return;
}
for(int i=0;i<=9;i++) {
if(visited[i]) {
visited[i]=false;
dfs(depth+1,num+1,visited);
visited[i]=true;
}else {
dfs(depth+1,num,visited);
}
}
}
}
후기.
유형이 비슷한 문제가 많으므로 풀기 어렵다면 외우는 방법도 고려해봄직하다.
728x90
댓글