1
2
-- 여러 기준으로 정렬하기
SELECT ANIMAL_ID, NAME, DATETIME FROM ANIMAL_INS ORDER BY NAME ASC, DATETIME DESC
cs

1
2
-- 동물의 아이디와 이름
SELECT ANIMAL_ID, NAME FROM ANIMAL_INS ORDER BY ANIMAL_ID ASC
cs

1
2
-- 어린 동물 찾기
SELECT ANIMAL_ID, NAME FROM ANIMAL_INS WHERE INTAKE_CONDITION != 'Aged' ORDER BY ANIMAL_ID ASC
cs

1
2
-- 아픈 동물 찾기
SELECT ANIMAL_ID, NAME FROM ANIMAL_INS WHERE INTAKE_CONDITION='Sick' ORDER BY ANIMAL_ID ASC
cs

 

1
2
-- 역순 정렬하기
SELECT NAME,DATETIME FROM ANIMAL_INS ORDER BY ANIMAL_ID DESC
cs

 

 

1
2
-- 모든 레코드 조회하기
SELECT * from ANIMAL_INS ORDER BY ANIMAL_ID ASC
cs

 

#유형 : 그래프 탐색

# 난이도 : 실버 1

# 거쳐가는 정점을 기준으로 문제를 푸는 플로이드 와샬의 전형적인 문제였다. 입력이 N으로 주어지는 부분을 Integer.MAX_VALUE로 처리했더니 11%에서 계속 틀렸다고 떠서 잘못풀었나했는데.. 987654321 값을 주니 또 통과하였다.. 왜 그런지 알 수가없다., 

 

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package bj;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class p1058 {
    static int N, result = 0;
    static int arr[][];
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        arr = new int[N+1][N+1];
        for(int i=1; i<=N; i++) {
            String str = br.readLine();
            for(int j=1; j<=N; j++) {
                char ch = str.charAt(j-1);
                
                if(ch == 'Y')
                    arr[i][j] = 1;
                
                else if(i != j)
                    arr[i][j] = 987654321;
                
            }
        }
        floyd_warshall();
        for(int i=1; i<=N; i++) {
            int tmp = 0;
            for(int j=1; j<=N; j++) {
                if(i==j)
                    continue;
                // 한 다리 건너서 아는 친구 + 서로 친구
                else if(arr[i][j] <= 2)
                    tmp++;
            }
            result = Math.max(result, tmp);
        }
        System.out.println(result);
    }
    public static void floyd_warshall() {
        for(int k=1; k<=N; k++) {
            for(int i=1; i<=N; i++) {
                for(int j=1; j<=N; j++) {
                    if(i==|| j==|| i==k)
                        continue;
                    else if(arr[i][j] > arr[i][k] + arr[k][j])
                        arr[i][j] = arr[i][k] + arr[k][j];
                }
            }
        }
    }
}
 
cs

# 유형 : 그래프 탐색

# 난이도 : 골드 4

# 벽을 최소한으로 부수고 목적지까지 가는 BFS문제

# 다음 가야할 좌표의 값이 현재 벽을 부순 개수보다 크다면 이동을 하고 작다면 이동하지 않는다.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package bj;
 
import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
 
public class p2665 {
    static int N;
    static int arr[][], map[][];
    static int moveX[] = {0,1,0,-1};
    static int moveY[] = {-1,0,1,0};
    static int maxValue = Integer.MAX_VALUE;
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        arr = new int[N+1][N+1];
        map = new int[N+1][N+1];
        for(int i=1; i<=N; i++) {
            String str = br.readLine();
            for(int j=1; j<=N; j++) {
                arr[i][j] = str.charAt(j-1)-'0';
                map[i][j] = Integer.MAX_VALUE;
            }
        }
        bfs(1,1);
//        for(int i=1; i<=N; i++) {
//            for(int j=1; j<=N; j++) {
//                System.out.print(map[i][j]+" ");
//            }System.out.println();
//        }
        System.out.println(map[N][N]);
    }
    
    public static void bfs(int x, int y) {
        Queue<Point> queue = new LinkedList<>();
        queue.add(new Point(x,y));
        map[y][x] = 0;
        while(!queue.isEmpty()){
            Point p = queue.poll();
            
            for(int d=0; d<4; d++) {
                int newX = p.x + moveX[d];
                int newY = p.y + moveY[d];
                
                //범위안에 속하면서 다음 좌표가 갱신이 아직 안되어있거나 더 크다면 => 더 작은값으로 갱신할수 있다면 
                if(1<=newX && newX<=&& 1<=newY && newY<=&& map[newY][newX] > map[p.y][p.x]) {
                    if(arr[newY][newX] == 1) {
                        queue.add(new Point(newX, newY));
                        map[newY][newX] = map[p.y][p.x];
                    }else {
                        queue.add(new Point(newX, newY));
                        map[newY][newX] = map[p.y][p.x] + 1;
                    }
                }
            }
        }
    }
    
    
}
 
cs

+ Recent posts