Created
December 19, 2016 16:50
-
-
Save JoeUnsung/4db3cbb6559671d20c651d0d0fb342a3 to your computer and use it in GitHub Desktop.
Find the number of wheat and amount of each of wheat out by the depth first search algorithm.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package joe; | |
import java.util.*; | |
import java.lang.*; | |
class dfSearch{ | |
int wheat = 1; | |
int cnt = 0; | |
int map[][]; | |
java.util.Stack<Integer> stack_i = new java.util.Stack(); | |
java.util.Stack<Integer> stack_j = new java.util.Stack(); | |
dfSearch(){} | |
dfSearch(int[][] map, int cnt){ | |
this.cnt = cnt; | |
this.map = new int[cnt+2][cnt+2]; | |
//this.map[0][0] = 5; this.map[1][1] = 5; | |
for(int i = 1 ; i < cnt+1; i++){// 위아래양옆이 0으로 둘러쌓인 이차원배열 생 | |
for(int j = 1 ; j < cnt+1 ; j++){ | |
this.map[i][j] = map[i-1][j-1]; | |
//System.out.print(this.map[i][j]); | |
//System.out.print(map[i-1][j-1]); | |
} | |
System.out.println(); | |
} | |
} | |
public int checkDfs(int i, int j){ | |
//i++; j++; // 왜한거지? // | |
// DFS 서치 작업 | |
map[i][j] = 0; | |
if(map[i][j-1] == 1){ | |
wheat++; | |
stack_i.push(i); | |
stack_j.push(j); | |
j = j-1; | |
checkDfs(i,j); | |
} | |
else if( map[i+1][j] == 1){ | |
wheat++; | |
stack_i.push(i); | |
stack_j.push(j); | |
i = i+1; | |
checkDfs(i,j); | |
} | |
else if(map[i][j+1] == 1){ | |
wheat++; | |
stack_i.push(i); | |
stack_j.push(j); | |
j = j+1; | |
checkDfs(i,j); | |
} | |
else if ( map[i-1][j] == 1){ | |
wheat++; | |
stack_i.push(i); | |
stack_j.push(j); | |
i = i-1; | |
checkDfs(i,j); | |
} | |
else{ | |
i = stack_i.pop(); // error empty stack | |
j = stack_j.pop(); | |
} | |
return wheat; // 농작물의 갯수를 반환. | |
} | |
} | |
public class DS_20101062 { | |
public static void main(String[] args) throws java.lang.Exception{ | |
java.util.Stack<Integer> st = new java.util.Stack(); | |
Scanner sc = new Scanner(System.in); | |
System.out.print("밭의 크기(N) : "); | |
int cnt = sc.nextInt(); | |
System.out.println(); | |
System.out.print(cnt+" BY "+cnt+"정방행렬 :"); | |
System.out.println(); | |
Scanner ss = new Scanner(System.in); | |
int map[][] = new int[cnt][cnt]; | |
for(int i = 0 ; i < cnt ; i++){ | |
String temp = ss.nextLine(); | |
for(int j = 0 ; j < cnt ; j++){ | |
map[i][j] = (int)temp.charAt(j) - 48; | |
//System.out.print(map[i][j]); | |
} | |
} | |
//System.out.println(cnt); // 1열의 농작물 종류의 갯수 출 | |
// for로 모든 밭의 원소들을 한번씩 다 체크하면서 내려감. | |
//dfSearch joe = new dfSearch(map, cnt); | |
int cntCountWheat = 1; | |
int[] countWheat = new int[999]; | |
int[][] tempMap = new int[cnt+2][cnt+2]; | |
java.util.Stack<Integer> stack_i = new java.util.Stack(); | |
java.util.Stack<Integer> stack_j = new java.util.Stack(); | |
for(int i = 1 ; i < cnt+1; i++){// 위아래양옆이 0으로 둘러쌓인 이차원배열 생성 | |
for(int j = 1 ; j < cnt+1 ; j++){ | |
tempMap[i][j] = map[i-1][j-1]; | |
} | |
} | |
for(int i = 1 ; i < cnt+1 ; i++){ | |
for(int j = 1 ; j < cnt+1 ; j++){ | |
if(tempMap[i][j] == 1){ | |
while(true){ | |
if(tempMap[i][j] == 1){ | |
tempMap[i][j] = 0; | |
countWheat[cntCountWheat]++; | |
} | |
if(tempMap[i+1][j] == 1){ // 아래가 곡물인 경우 | |
stack_i.push(i); | |
stack_j.push(j); | |
i++; | |
} | |
else if(tempMap[i][j+1] == 1){ // 오른쪽이 곡물인 경우 | |
stack_i.push(i); | |
stack_j.push(j); | |
j++; | |
} | |
else if(tempMap[i-1][j] == 1){ // 위쪽이 곡물인 경우 | |
stack_i.push(i); | |
stack_j.push(j); | |
i--; | |
} | |
else if(tempMap[i][j-1] == 1){ // 오른쪽이 곡물인 경우 | |
stack_i.push(i); | |
stack_j.push(j); | |
j--; | |
} | |
else if( !stack_i.isEmpty() && !stack_j.isEmpty() ){ // 둘다 스택에 인덱스가 남아있을 경우 | |
i = stack_i.pop(); | |
j = stack_j.pop(); | |
} | |
else{ | |
//System.out.println("check: "+i+" "+j); | |
break; | |
} | |
} | |
cntCountWheat++; // 첫번째 곡물 존을 다 검색한 후 다음 곡물존을 검색하기 이전에 번호 넘버링 1+ | |
} | |
} | |
} | |
//정렬 및 출력 | |
ArrayList<Integer> reposiWheat = new ArrayList<Integer>(); | |
int i = 1; | |
while(countWheat[i] != 0){ | |
reposiWheat.add(countWheat[i]); | |
//System.out.println(reposiWheat.get(i-1)); | |
i++; | |
} | |
System.out.println(i-1); | |
Collections.sort(reposiWheat); // 오름차순으로 어레이리스트 정리. | |
i=1; | |
while(countWheat[i] != 0){ | |
System.out.println(reposiWheat.get(i-1)); | |
i++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment