Skip to content

Commit 3ce274b

Browse files
committed
feat : 프로그래머스 DFS/BFS
1 parent cefe121 commit 3ce274b

File tree

6 files changed

+297
-119
lines changed

6 files changed

+297
-119
lines changed

.idea/sonarlint/issuestore/index.pb

Lines changed: 16 additions & 119 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package sgyj.programmers.yeji.dfsbfs;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
public class Solution1844 {
7+
private static int[] dx = {1,0,-1,0};
8+
private static int[] dy = {0,-1,0,1};
9+
private static boolean[][] visited;
10+
11+
public static int solution(int[][] maps) {
12+
int answer = 1;
13+
int n = maps.length;
14+
int m = maps[0].length;
15+
visited = new boolean[n][m];
16+
Deque<MapNode> q = new ArrayDeque<>();
17+
q.offer( new MapNode( 0,0 ) );
18+
visited[0][0]=true;
19+
while ( !q.isEmpty() ){
20+
int len = q.size();
21+
for(int i=0; i<len; i++){
22+
MapNode curNode = q.poll();
23+
for(int d=0; d<dx.length; d++){
24+
int x = curNode.x + dx[d];
25+
int y = curNode.y + dy[d];
26+
if(x==n-1 && y==m-1) return answer+1;
27+
if(x>=0 && x<n && y>=0 && y<m && !visited[x][y] && maps[x][y] == 1){
28+
visited[x][y] = true;
29+
q.offer( new MapNode( x,y ) );
30+
}
31+
}
32+
}
33+
answer++;
34+
}
35+
36+
return -1;
37+
}
38+
39+
public static void main ( String[] args ) {
40+
// [[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,1],[0,0,0,0,1]]
41+
int[][] maps= {
42+
{1,0,1,1,1},
43+
{1,0,1,0,1},
44+
{1,0,1,1,1},
45+
{1,1,1,0,1},
46+
{0,0,0,0,1}
47+
};
48+
System.out.println(solution(maps));
49+
}
50+
51+
}
52+
53+
class MapNode{
54+
int x;
55+
int y;
56+
57+
MapNode(int x, int y){
58+
this.x = x;
59+
this.y = y;
60+
}
61+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package sgyj.programmers.yeji.dfsbfs;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Solution43162 {
7+
private static boolean[] visited;
8+
static List<List<Integer>> computerList;
9+
10+
private static int answer = 0;
11+
public static int solution(int n, int[][] computers) {
12+
visited = new boolean[n+1];
13+
computerList = new ArrayList<>(n+1);
14+
for(int i=0; i<=n; i++){
15+
computerList.add( new ArrayList<>(n+1) );
16+
}
17+
18+
for(int i=0; i<computers.length; i++){
19+
for(int j=0; j<computers[i].length; j++){
20+
if(computers[i][j] != 0){
21+
computerList.get( i+1 ).add( j+1 );
22+
}
23+
}
24+
}
25+
26+
for(int i=1; i<computerList.size(); i++){
27+
if(!visited[i] ){
28+
visited[i] = true;
29+
findNetwork(computerList.get( i ),0);
30+
answer++;
31+
}
32+
}
33+
34+
return answer;
35+
}
36+
37+
private static void findNetwork(List<Integer> netWork, int index){
38+
if(index != netWork.size()){
39+
int target = netWork.get( index );
40+
if ( !visited[target] ){
41+
visited[target] = true;
42+
findNetwork( computerList.get( target ),0 );
43+
}
44+
findNetwork( netWork,index+1 );
45+
}
46+
}
47+
48+
public static void main ( String[] args ) {
49+
int[][] computers = {
50+
{1,1,0},{1,1,1},{0,0,1}
51+
};
52+
int n = 3;
53+
System.out.println(solution(n,computers));
54+
}
55+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package sgyj.programmers.yeji.dfsbfs;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
public class Solution43163 {
7+
private static boolean[] visited;
8+
private static int answer = 1;
9+
10+
public static int solution(String begin, String target, String[] words) {
11+
int n = words.length;
12+
visited = new boolean[n];
13+
Deque<Word> q = new ArrayDeque<>();
14+
q.offer( new Word( begin,0 ) );
15+
while ( !q.isEmpty() ){
16+
int len = q.size();
17+
for(int i=0; i<len; i++){
18+
Word cur = q.poll();
19+
if(cur.count <= n){
20+
for(String compare : words){
21+
if(compareAlphabet(cur.word,compare)){
22+
if(compare.equals( target )) return cur.count+1;
23+
q.offer( new Word( compare,cur.count+1 ) );
24+
}
25+
}
26+
}
27+
28+
}
29+
}
30+
31+
return 0;
32+
}
33+
34+
private static boolean compareAlphabet(String word, String compare){
35+
int count = 0;
36+
for(int i=0; i<word.length(); i++){
37+
if(word.charAt( i ) != compare.charAt( i )){
38+
count++;
39+
}
40+
}
41+
42+
return count == 1;
43+
}
44+
45+
46+
public static void main ( String[] args ) {
47+
String begin = "hit";
48+
String target = "cog";
49+
String[] words = {"hot", "dot", "dog", "lot", "log"};
50+
System.out.println(solution(begin,target,words));
51+
}
52+
}
53+
54+
class Word{
55+
String word;
56+
int count;
57+
58+
Word(String word, int count){
59+
this.word = word;
60+
this.count = count;
61+
}
62+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package sgyj.programmers.yeji.dfsbfs;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.Collections;
7+
import java.util.Deque;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.PriorityQueue;
12+
13+
public class Solution43164 {
14+
15+
private static Map<String,List<String>> ticketMap = new HashMap<>();
16+
private static PriorityQueue<String> pQ = new PriorityQueue<>();
17+
18+
public static String[] solution(String[][] tickets) {
19+
20+
for(String[] ticket : tickets){
21+
List<String> getList = ticketMap.getOrDefault( ticket[0], new ArrayList<>() );
22+
getList.add( ticket[1] );
23+
ticketMap.put( ticket[0] ,getList);
24+
}
25+
26+
List<String> answer = new ArrayList<>();
27+
pQ.offer( "ICN" );
28+
29+
while ( !pQ.isEmpty() ){
30+
int len = pQ.size();
31+
for(int i=0; i<len; i++){
32+
String cur = pQ.poll();
33+
answer.add( cur );
34+
if(!answer.contains( cur )) {
35+
List<String> strings = ticketMap.get( cur );
36+
for ( String string : strings ) {
37+
pQ.offer( string );
38+
}
39+
}
40+
}
41+
}
42+
43+
return answer.toArray( new String[0] );
44+
}
45+
46+
public static void main ( String[] args ) {
47+
String[][] tickets = {
48+
{"ICN", "JFK"},{"HND", "IAD"},{"JFK", "HND"}
49+
};
50+
51+
for(String ticket : solution(tickets)){
52+
System.out.print(ticket + " ");
53+
}
54+
}
55+
}
56+
57+
class AirPort implements Comparable<AirPort>{
58+
String airPort;
59+
boolean visited;
60+
61+
AirPort(String airPort){
62+
this.airPort = airPort;
63+
}
64+
65+
@Override
66+
public int compareTo ( AirPort o ) {
67+
return this.airPort.compareTo( o.airPort );
68+
}
69+
70+
public void visited (){
71+
this.visited = true;
72+
}
73+
}

0 commit comments

Comments
 (0)