Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions 11월23일/초중급/2071050_2580
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class BOJ_2580 {

private static int map[][];
private static int zero;
private static boolean row[][];
private static boolean col[][];
private static boolean area[][];
private static LinkedList<Blank> list;
private static class Blank{
int r, c;
public Blank(int r, int c) {
// TODO Auto-generated constructor stub
this.r = r;
this.c = c;
}
}
private static int area_set(int r, int c) {
if (r >= 0 && r<= 2) {
if (c >= 0 && c<= 2)
return 0;
else if(c >=3 && c <= 5)
return 1;
else
return 2;
}

else if (r >= 3 && r<= 5) {
if (c >= 0 && c<= 2)
return 3;
else if(c >=3 && c <= 5)
return 4;
else
return 5;
}
else {
if (c >= 0 && c<= 2)
return 6;
else if(c >=3 && c <= 5)
return 7;
else
return 8;
}
}

private static boolean backtracking(int depth, int size) {
if(depth == size) {
for(int i=0;i<9;i++) {
for (int j=0;j<9;j++) {
System.out.print(map[i][j]+ " ");
}
System.out.println();
}
return true;
}
int tempr = list.get(depth).r;
int tempc = list.get(depth).c;
int tempa = area_set(tempr, tempc);
for(int i=1;i<=9;i++) {
if(!row[tempr][i] && !col[tempc][i] && !area[tempa][i]) {
map[tempr][tempc] = i;
row[tempr][i] = true;
col[tempc][i] = true;
area[tempa][i] = true;
if(backtracking(depth + 1, size))
return true;
map[tempr][tempc] = 0;
row[tempr][i] = false;
col[tempc][i] = false;
area[tempa][i] = false;
}
}
return false;
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
map = new int [9][9];
row = new boolean [9][10];
col = new boolean [9][10];
area = new boolean [9][10];
list = new LinkedList<>();
int depth = 0;
for(int i=0;i<9;i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j=0;j<9;j++)
{
int val = Integer.parseInt(st.nextToken());
map[i][j] = val;
if (val == 0)
{
list.add(new Blank(i,j));
}
else {
row[i][val] = true;
col[j][val] = true;
area[area_set(i, j)][val] = true;
}
}
}
int size = list.size();
backtracking(depth, size);
}

}
51 changes: 51 additions & 0 deletions 11월23일/초중급/2071051_10971
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BOJ_10971 {

private static int map[][];
private static boolean visited[];
private static int N;
private static int min;
private static boolean allVisited(boolean visited[]) {
for(int i=0;i<N;i++) {
if (visited[i] == false)
return false;
}
return true;
}
private static void backtracking(int cost, int now, int start) {
if(allVisited(visited)) {
cost += map[now][start];
min = Math.min(cost, min);
return ;
}
for(int i=0;i<N;i++) {
if (visited[i] == false && map[now][i] > 0) { //i도시를 아직 방문하지 않았고 now->i로 가는 길이 있다면
visited[i] = true;
backtracking(cost + map[now][i], i, start);
visited[i] = false;
}
}
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine()); //도시의 수
map = new int[N][N];
visited = new boolean[N];
min = Integer.MAX_VALUE;
for(int i=0;i<N;i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for(int j=0;j<N;j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
for(int i=0;i<N;i++) {
backtracking(0, i, i);
}
System.out.println(min);
}
}