SDJ( 수돈재 아님 ㅎ )

[C] 5894 - Connect the Cows 본문

알고리즘/Backjoon

[C] 5894 - Connect the Cows

ShinDongJun 2020. 1. 4. 19:37

문제 링크 : https://www.acmicpc.net/problem/5894

 

5894번: Connect the Cows

Input Details There are 4 cows, at positions (0,1), (2,1), (2,0), and (2,-5). Output Details There are two different routes: Farmer John can visit cows in the orders 1-2-4-3 or 3-4-2-1 before returning to the origin.

www.acmicpc.net

 

 

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include<stdio.h>
 
#define right 1
#define left 2
#define up 4
#define down 8
 
#define stan_x 1000
#define stan_y 1000
 
#define MAX 0
#define MIN 1
 
int X[2005][2005];
int S[2005][2];
int G[2005][2];
 
int N;
int cnt;
 
void BT(int y, int x, int come, int k);
void search(int y, int x, int mode, int k);
 
int main(void)
{
    int x, y, i;
 
    for(int i = 0; i <= 2000++i)
    {
        S[i][MAX] = -1;
        S[i][MIN] = 2001;
        G[i][MAX] = -1;
        G[i][MIN] = 2001;
    }
 
    scanf("%d"&N);
    
    for(int i = 0; i < N; ++i)
    {
        scanf("%d %d"&x, &y);
        X[y+stan_y][x+stan_x] = 1;
 
        if(S[x+stan_x][MAX] < y+stan_y)
            S[x+stan_x][MAX] = y+stan_y;
        if(y+stan_y < S[x+stan_x][MIN])
            S[x+stan_x][MIN] = y+stan_y;
 
        if(G[y+stan_y][MAX] < x + stan_x)
            G[y+stan_y][MAX] = x+stan_x;
        if(x+stan_x < G[y+stan_y][MIN])
            G[y+stan_y][MIN] = x+stan_x;
    }
 
    for(i = stan_x+1; i <= G[stan_y][MAX]; ++i)
        search(stan_y, i, right, 1);
    
    for(i = G[stan_y][MIN]; i < stan_x; ++i)
        search(stan_y, i, left, 1);
    
    for(i = stan_y+1; i <= S[stan_x][MAX]; ++i)
        search(i, stan_x, up, 1);
 
    for(i = S[stan_x][MIN]; i < stan_y; ++i)
        search(i, stan_x, down, 1);
 
    printf("%d", cnt);
 
    return 0;
}
 
void BT(int y, int x, int come, int k)
{
    int i;
 
    if(k == N)
    {
        if(come == right)
        {
            if(x == stan_x) cnt++;
            else if(x > stan_x && y == stan_y) cnt++;
        }
        if(come == left)
        {
            if(x == stan_x) cnt++;
            else if(x < stan_x && y == stan_y) cnt++;
        }
        if(come == up)
        {
            if(x == stan_x && y > stan_y) cnt++;
            else if(y == stan_y) cnt++;
        }
        if(come == down)
        {
            if(x == stan_x && y < stan_y) cnt++;
            else if(y == stan_y) cnt++;
        }
        return;
    }
 
    for(i = x+1;(come ^ right) && i <= G[y][0]; ++i)
        search(y, i, right, k+1);
    
    for(i = G[y][1];(come ^ left) &&  i < x; ++i)
        search(y, i, left, k+1);
 
    for(i = y+1;(come ^ up) && i <= S[x][0]; ++i)
        search(i, x, up, k+1);
 
    for(i = S[x][1];(come ^ down) && i < y; ++i)
        search(i, x, down, k+1);
 
}
 
void search(int y, int x, int mode, int k)
{
    if(X[y][x])
    {
        X[y][x] = 0;
        BT(y, x, mode, k);
        X[y][x] = 1;
    }
}

'알고리즘 > Backjoon' 카테고리의 다른 글

[C++] 15650 - N과 M (2)  (0) 2020.01.05
[C++] 15649 - N과 M (1)  (0) 2020.01.05
[Python3] 5893 - 17배  (0) 2020.01.03
[C] 13420 - 사칙연산  (0) 2020.01.03
[Python3] 13417 - 카드 문자열  (0) 2020.01.03
Comments