https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV4su3xKXFUDFAUf


1. init()이 한번만 실행될 수 있게 flag를 넣었다.

2. A->B B->A가 같음을 이용하여 query 수를 줄였다. 대신 compare()와 visit배열을 이용해 답을 찾아갔다.


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
#if 0
#define N          4
 
typedef struct {
    int strike;
    int ball;
} Result;
 
// API
extern Result query(int guess[]);
 
Result compare(int key, int obj)
{
    int count[10= { 0 };
    Result result = { 00 };
    
    for (int i = 0; i < N; ++i){
        ++count[key % 10];
        ++count[obj % 10];
        if (key % 10 == obj % 10++result.strike;
        key = key / 10;
        obj = obj / 10;
    }
 
    for (int i = 0; i < 10++i){
        if (count[i] == 2++result.ball;
    }
 
    result.ball -= result.strike;
 
    return result;
}
 
int A[6000];
int V[10000];
int Count_guess;
int flag_init = 0;
 
void init(void)
{
    int i, j, k, l;
    
    if (flag_init == 0){
        flag_init = 1;
        Count_guess = 0;
        for (i = 0; i < 10++i){
            for (j = 0; j < 10++j){
                if (j == i) continue;
                for (k = 0; k < 10++k){
                    if (k == i || k == j) continue;
                    for (l = 0; l < 10++l){
                        if (l == i || l == j || l == k) continue;
                        A[Count_guess++= 1000 * i + 100 * j + 10 * k + l;
                    }
                }
            }
        }
    }
 
    for (register int i = 0; i < Count_guess; ++i) V[A[i]] = 0;
}
 
void doUserImplementation(int guess[]) 
{
    Result result;
    Result hub;
    int key;    
 
    init();
 
    while (1){
        for (register int i = 0; i < Count_guess; ++i){
            if (V[A[i]] == 0) {
                key = A[i];
                guess[3= A[i] % 10;
                guess[2= (A[i] / 10) % 10;
                guess[1= (A[i] / 100) % 10;
                guess[0= A[i] / 1000;
                result = query(guess);
                break;
            }
        }
 
        if (result.strike == 4break;
 
        for (register int i = 0; i < Count_guess; ++i){
            if (V[A[i]] == 0){
                hub = compare(key, A[i]);
                if (hub.ball == result.ball && hub.strike == result.strike) continue;
                V[A[i]] = 1;
            }
        }
    }
}
 
#endif
cs


'알고리즘 > SW Expert Academy' 카테고리의 다른 글

1244 최대 상금  (0) 2018.09.20
1210 Ladder1  (0) 2018.09.20
1204 최빈수 구하기  (0) 2018.09.20
1206 View  (0) 2018.09.18
1251 하나로  (0) 2018.09.18

https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV15Khn6AN0CFAYD&categoryId=AV15Khn6AN0CFAYD&categoryType=CODE


DFS썼는데 N이 커지면 안될듯


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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <stdio.h>
 
typedef struct _st
{
    int num;
    int order;
}ST;
 
#define MAX (6+6)
#define SWAP(x,y) { ST temp; temp = x; x = y; y = temp; }
int T, tc;
char num_s[MAX];
//int num[MAX];
//int order[MAX];
ST a[MAX];
int count[MAX];
int count_flag;
int digit = 6;
int N;
int ans;
 
int sum_array(void)
{
    int i;
    int S;
 
    S = a[1].num;
    for (i = 2; i <= digit; i++)
    {
        S *= 10;
        S += a[i].num;
    }
    return S;
}
 
void init(void)
{
    int i;
 
    ans = 0;
    count_flag = 0;
    for (i = 1; i <= digit; i++)
    {
        a[i].order = 1;
    }
    for (i = 0; i <= 9; i++)
    {
        count[i] = 0;
    }
}
 
void input(void)
{
    int i, j;
 
    //값 받기
    scanf("%s"&num_s[1]);
    for (i = 1; num_s[i] != 0; i++)
    {
        a[i].num = num_s[i] - '0';
        count[a[i].num]++;
    }
    digit = i - 1;
 
    //count_flag설정
    for (i = 0; i <= 9; i++)
    {
        if (count[i] > 1)
        {
            count_flag = 1;
            break;
        }
    }
 
    //order정하기
    for (i = 1; i <= digit; i++)
    {
        for (j = 1; j <= i; j++)
        {
            if (a[i].num < a[j].num)
            {
                a[i].order++;
            }
            else if (a[i].num > a[j].num)
            {
                a[j].order++;
            }
        }
    }
 
    //times 조절
    scanf("%d"&N);
    if (N > digit)
    {
        if ((digit + N) & 1) N = digit - 1;
        else N = digit; //둘다 홀인경우 or 짝인경우
    }
}
 
void DFS(int L, int C, int S)
{
    int i, j;
    int flag = 0;
 
    if (C >= N)
    {
        if (S > ans) ans = S;
        return;
    }
    if (L >= digit)
    {
        if (C < N)
        {
            //N-C 값 비교해서 짝 홀/ 같은거 있으면 리턴 없으면 (N-C)&1
            if (count_flag == 1)
            {
                if (S > ans) ans = S;
            }
            else
            {
                if ((N - C) & 1// N-C가 홀수면
                {
                    if (ans < S - 9 * a[digit - 1].num + 9 * \
                    a[digit].num) ans = S - 9 * a[digit - 1].num + 9 * a[digit].num;
                }
                else
                {
                    if (S > ans) ans = S;
                }
            }           
        }
        return;
    }
 
    //L번째 자리에서 order가 L인 것만 움직인다.
    if (a[L].order == L)
    {
        if (count[a[L].num] > 1)
        {
            flag = 1;
            count[a[L].num]--;
            for (i = L + 1; i <= digit; i++)
            {
                if (a[i].order == L) a[i].order++;
            }
        }
        DFS(L + 1, C, sum_array());
        if (flag == 1)
        {
            flag = 0;
            count[a[L].num]++;
            for (i = L + 1; i <= digit; i++)
            {
                if (a[i].order == L + 1) a[i].order--;
            }
        }
    }
    else
    {
        for (i = L + 1; i <= digit; i++)
        {
            if (a[i].order == L)
            {
                //바꾸고 같은 L이 있으면 order내에서 더해주고...
                if (count[a[i].num] > 1)
                {
                    flag = 1;
                    count[a[i].num]--;
                    for (j = L + 1; j <= digit; j++)
                    {
                        if (j == i) continue;
                        if (a[j].order == L) a[j].order++;
                    }
                }
                SWAP(a[L], a[i]);
                DFS(L + 1, C + 1, sum_array());
                if (flag == 1)
                {
                    flag = 0;
                    count[a[L].num]++;
                    for (j = L + 1; j <= digit; j++)
                    {
                        if (j == i) continue;
                        if (a[j].order == L + 1) a[j].order--;
                    }
                }
                SWAP(a[L], a[i]);
            }
        }
    }
}
 
void output(void)
{
    printf("#%d %d\n", tc, ans);
}
 
int main(void)
{
    scanf("%d"&T);
    for (tc = 1; tc <= T; tc++)
    {
        init();
        input();
        DFS(100);
        output();
    }
    return 0;
}
 
cs


'알고리즘 > SW Expert Academy' 카테고리의 다른 글

1768. [SW Test 샘플문제] 숫자야구게임  (1) 2018.09.28
1210 Ladder1  (0) 2018.09.20
1204 최빈수 구하기  (0) 2018.09.20
1206 View  (0) 2018.09.18
1251 하나로  (0) 2018.09.18

https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14ABYKADACFAYh&categoryId=AV14ABYKADACFAYh&categoryType=CODE


좌우 먼저 보고 위로 올라갑니당


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
#include <stdio.h>
typedef struct st1
{
    int r; int c;
}ST1;
 
int a[102][102];
ST1 SP, EP;
int WP, RP;
ST1 Queue[102*102];
void InQueue(int rr, int cc) { Queue[WP].r = rr; Queue[WP++].c = cc; }
ST1 DeQueue(void) { return Queue[RP++]; }
int dr[] = { 00-1 };
int dc[] = { -110 };
 
int tc;
 
void input(void)
{
    scanf("%d"&tc);
    for (int i = 0; i < 100; i++)
    {
        for (int j = 0; j < 100; j++)
        {
            scanf("%d"&a[i][j]);
            if (a[i][j] == 2)
            {
                EP.r = i; 
                EP.c = j;
            }
        }
    }
}
 
ST1 BFS(int r, int c)
{
    int i, nr, nc;
    ST1 out;
 
    WP = 0;
    RP = 0;
 
    InQueue(r, c);
    //a[r][c] = 2;
 
    while (RP < WP)
    {
        out = DeQueue();
        for (i = 0; i < 3; i++)
        {
            nr = out.r + dr[i];
            nc = out.c + dc[i];
            if (nr < 0 || nc < 0 || nr >= 100 || nc >= 100continue;
            if (a[nr][nc] == 1)
            {
                InQueue(nr, nc);
                a[nr][nc] = 2;
                break;
            }
        }
    }
    return Queue[WP-1];
}
 
int main(void)
{
    for (tc = 1; tc <= 10; tc++)
    {
        ST1 temp;
 
        input();
        temp = BFS(EP.r, EP.c);
        //output();
        printf("#%d %d\n", tc, temp.c);
    }
    return 0;
}
 
cs


'알고리즘 > SW Expert Academy' 카테고리의 다른 글

1768. [SW Test 샘플문제] 숫자야구게임  (1) 2018.09.28
1244 최대 상금  (0) 2018.09.20
1204 최빈수 구하기  (0) 2018.09.20
1206 View  (0) 2018.09.18
1251 하나로  (0) 2018.09.18

https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV13zo1KAAACFAYh&categoryId=AV13zo1KAAACFAYh&categoryType=CODE


정렬, look-up table


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
#include <stdio.h>
 
typedef struct st_
{
    int num;
    int val;
}ST;
 
#define SWAP(x,y)    {ST temp; temp=x; x=y; y=temp;}
int T, tc;
ST a[100+10];
 
 
void init(void)
{
    int i;
 
    for (i = 0; i <= 100; i++)
    {
        a[i].val = 0;
    }
}
 
void input(void)
{
    int i;
    int score;
 
    scanf("%d"&tc);
    for (i = 1; i <= 1000; i++)
    {
        scanf("%d"&score);
        a[score].num = score;
        a[score].val++;        
    }
}
 
void sort(void)
{
    int i, j;
 
    for (i = 1; i <= 100; i++)
    {
        for (j = 0; j < i; j++)
        {
            if (a[j].val < a[i].val)
            {
                SWAP(a[i], a[j]);
            }
            else if (a[j].val == a[i].val && a[j].num < a[i].num)
            {
                SWAP(a[i], a[j]);
            }
        }
    }
}
 
void output(void)
{
    printf("#%d %d\n", tc, a[0].num);
}
 
int main(void)
{
    scanf("%d"&T);
    for (tc = 1; tc <= T; tc++)
    {
        init();
        input();
        sort();
        output();
    }
 
    return 0;
}
cs


'알고리즘 > SW Expert Academy' 카테고리의 다른 글

1244 최대 상금  (0) 2018.09.20
1210 Ladder1  (0) 2018.09.20
1206 View  (0) 2018.09.18
1251 하나로  (0) 2018.09.18
1868 파핑파핑 지뢰찾기  (0) 2018.09.18

https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV134DPqAA8CFAYh


1. 매 순간 비교만

  • 4 kb메모리
  • 2 ms실행시간
  • 516코드길이



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
#if 01
#pragma warning (disable:4996)
#include <stdio.h>
 
#define MAX (1000+5)
#define max(a,b)    ((a)>(b)?(a):(b))
int tc;
int N;
int a[MAX];
 
int main(void)
{
    int i;
    int count;
     
 
    for (tc = 1; tc <= 10; tc++) {
        scanf("%d"&N);
        //init
        count = 0;
        //input         
        for (i = 1; i <= N; i++) {
            scanf("%d"&a[i]);
        }
        //process
        for (i = 3; i <= N - 2; i++) {
            count += max(0, a[i] - max(max(a[i - 2], a[i - 1]), max(a[i + 1], a[i + 2])));
        }
        //output
        printf("#%d %d\n", tc, count);
    }
}
 
#endif
cs


2. 체크를 위해 배열을 만들었었음. 

  • 12,752 kb메모리
  • 39 ms실행시간
  • 782코드길이


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
#if 01
#pragma warning (disable:4996)
#include <stdio.h>
 
bool a[1000 + 10][255 + 10];
 
int main(void)
{
    int tc;
    int N;
    int height;
    int count;
    int flag;
     
    int i, j, k;
     
    for (tc = 1; tc <= 10; tc++)
    {
        scanf("%d"&N);
        //init
        count = 0;
        flag = 0;
        for (i = 1; i <= N; i++) {
            for (j = 1; j <= 255; j++) {
                a[i][j] = 0;
            }
        }
        //input
        for (i = 1; i <= N; i++) {
            scanf("%d"&height);
            for (j = 1; j <= height; j++) {
                a[i][j] = 1;
            }
        }
        //process
        for (i = 1; i <= N; i++)
        {
            for (j = 1; a[i][j] != 0; j++)
            {
                flag = 0;
                for (k = -2; k <= 2; k++) {
                    if (k == 0continue;
                    if (a[i + k][j] == 1) flag = 1;
                }
                if (flag == 0) count++;
            }
        }
        //output
        printf("#%d %d\n", tc, count);      
    }
#endif
cs


'알고리즘 > SW Expert Academy' 카테고리의 다른 글

1210 Ladder1  (0) 2018.09.20
1204 최빈수 구하기  (0) 2018.09.20
1251 하나로  (0) 2018.09.18
1868 파핑파핑 지뢰찾기  (0) 2018.09.18
1767 프로세서 연결하기  (0) 2018.09.17

https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV15StKqAQkCFAYD


1. 간선 저장 후 KRUSKAL방법으로 MST를 구한다.


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
#if 01
#pragma warning(disable : 4996)
#include <stdio.h>
#include <stdlib.h>
typedef long long ll;
typedef struct st1
{
    ll x; ll y;
}ST1;
typedef struct st2
{
    ll val; int p1; int p2;
}ST2;
 
#define MAX (1000+5)
#define D_SQR(a,b)  ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))
#define SWAP(A,B)   {ST2 temp; temp = A; A=B; B=temp;};
int T, tc;
int N;      //1~1000
int p[MAX];
ST1 obj[MAX]; 
ST2 L_list[MAX*(MAX-1)/2];
double E;
 
int Find(int x) { return x == p[x] ? x : Find(p[x]); }
void Union(int x, int y) { p[Find(x)] = Find(y); }
 
void init(void)
{
    scanf("%d"&N);
    for (int i = 1; i <= N; i++) { p[i] = i; }
}
 
void input(void)
{
    int index;
 
    for (int i = 1; i <= N; i++) { scanf("%d"&obj[i].x); }
    for (int i = 1; i <= N; i++) { scanf("%d"&obj[i].y); }
 
    index = 1;
    for (int i = 2; i <= N; i++)    {
        for (int j = 1; j < i; j++)        {
            L_list[index].val = D_SQR(obj[i], obj[j]);
            L_list[index].p1 = i;
            L_list[index++].p2 = j;
        }
    }
    scanf("%lf"&E);
}
 
ll MST_KRUSKAL(void)
{
    int count;
    ll sum;
 
    count = 0;
    sum = 0;
    for (int i = 1; i <= N*(N - 1/ 2; i++)    {
        if (Find(L_list[i].p1) != Find(L_list[i].p2))        {
            Union(L_list[i].p1, L_list[i].p2);
            sum += L_list[i].val;
        }
    }
    return sum;
}
 
void output(void)
{
    printf("#%d %.0f\n", tc, E*MST_KRUSKAL());
}
 
int compare(const void *arg1, const void *arg2)
{
    if (((ST2 *)arg1)->val > ((ST2 *)arg2)->val) return 1;
    else if (((ST2 *)arg1)->val == ((ST2 *)arg2)->val) return 0;
    else return -1;
}
 
int main(void)
{
    scanf("%d"&T);
    for (tc = 1; tc <= T; tc++)    {
        init();
        input();
        qsort(&L_list[1], N*(N - 1/ 2sizeof(ST2), compare);
        output();
    }
    return 0;
}
 
#endif
cs


'알고리즘 > SW Expert Academy' 카테고리의 다른 글

1210 Ladder1  (0) 2018.09.20
1204 최빈수 구하기  (0) 2018.09.20
1206 View  (0) 2018.09.18
1868 파핑파핑 지뢰찾기  (0) 2018.09.18
1767 프로세서 연결하기  (0) 2018.09.17

1. count_mine 함수를 만들어서 (r,c)에 따른 지뢰 개수 호출

2. BFS호출 때마다 ans++가 되는데, count_mine == 0 이면 큐에 넣어서 한번에 펼쳐지는 곳을 찾는다.

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
123
124
125
126
127
128
#if 01
#pragma warning (disable : 4996)
#include <stdio.h>
 
typedef struct _st
{
    int r; 
    int c;
}ST;
 
#define MAX (300+10)
int T, tc;
int N;
 
char a[MAX][MAX];
 
ST Queue[MAX*MAX];
int WP, RP;
int dr[] = {-1100-11-11 };
int dc[] = {00-11-1-111 };
void InQueue(int a, int b) { Queue[WP].r = a; Queue[WP++].c = b; }
ST DeQueue(void) { return Queue[RP++]; }
 
int ans;
 
 
void init(void)
{
    ans = 0;
}
 
void input(void)
{
    int i;
 
    scanf("%d"&N);
    for (i = 0; i < N; i++)
    {
        scanf("%s"&a[i][0]);
    }
}
 
int count_mine(int r, int c)
{
    int i, nr, nc;
    int mine;
 
    mine = 0;
    for (i = 0; i < 8; i++)
    {
        nr = r + dr[i];
        nc = c + dc[i];
        if (nr<0 || nc<0 || nr>- 1 || nc>- 1continue;
        if (a[nr][nc] == '*')  mine++;
    }
    return mine;
}
 
void BFS(int sr, int sc)
{
    int i, nr, nc;
    ST out;
    int mine;
 
    WP = 0;
    RP = 0;
 
    InQueue(sr, sc);
    a[sr][sc] = '0';
 
    while (RP<WP)
    {
        out = DeQueue();
        for (i = 0; i < 8; i++)
        {
            nr = out.r + dr[i];
            nc = out.c + dc[i];
            if (nr<0 || nc<0 || nr>- 1 || nc>- 1continue;
            if (a[nr][nc] == '.')
            {
                mine = count_mine(nr, nc);
                if (mine == 0) InQueue(nr, nc);
                a[nr][nc] = mine + '0';
            }
        }
    }
}
 
void output(void)
{
    printf("#%d %d\n", tc, ans);
}
 
int main(void)
{
    int i, j;
 
    scanf("%d"&T);
    for (tc = 1; tc <= T; tc++)
    {
        init();
        input();
        //process1 - BFS
        for (i = 0; i < N; i++)
        {
            for (j = 0; j < N; j++)
            {
                if (a[i][j] == '.' && count_mine(i, j) == 0
                {
                    BFS(i, j);
                    ans++;
                }
            }
        }
        //process2 - count
        for (i = 0; i < N; i++)
        {
            for (j = 0; j < N; j++)
            {
                if (a[i][j] == '.') ans++;
            }
        }
        output();
    }
    return 0;
}
 
#endif
cs


'알고리즘 > SW Expert Academy' 카테고리의 다른 글

1210 Ladder1  (0) 2018.09.20
1204 최빈수 구하기  (0) 2018.09.20
1206 View  (0) 2018.09.18
1251 하나로  (0) 2018.09.18
1767 프로세서 연결하기  (0) 2018.09.17

https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV4suNtaXFEDFAUf


1. struct구성 : 위치와 연결됬는지 확인하는 변수 3개
2. DFS내에서 사용되는 Attach, Detach함수

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#if 01
#include <stdio.h>
 
typedef struct _st
{
    int r;
    int c;
    int p;
}ST;
 
#define INF (144)
#define MAX (12+3)
int T, tc;
int N;
int K;
int a[MAX][MAX];
ST core[MAX];
int max_core;
int min_wire[MAX];
 
int dr[] = { 0-1100 };
int dc[] = { 000-11 };
 
void input(void)
{
    int i, j;
 
    scanf("%d"&N);
    for (i = 1; i <= N; i++)
    {
        for (j = 1; j <= N; j++)
        {
            scanf("%d"&a[i][j]);
            if (a[i][j] == 1)
            {
                core[++K] = (ST) { i, j, 0 };
                if (i == 1 || i == N || j == 1 || j == N) core[K].p = 1;
            }
        }
    }
    //테두리
    for (i = 0; i <= N + 1; i++)
    {
        a[i][0= 3;
        a[i][N + 1= 3;
        a[0][i] = 3;
        a[N + 1][i] = 3;
    }
}
 
void init(void)
{
    int i;
 
    K = 0;
    max_core = 0;
    for (i = 0; i < MAX; i++)
    {
        min_wire[i] = INF;
    }
}
 
//return: i방향으로 연결 가능/ 0: 연결불가능
int check(int dir, int L)
{
    int i;
    int nr, nc;
 
    nr = core[L].r;
    nc = core[L].c;
 
    for (i = 1; i <= N; i++)
    {
        nr += dr[dir];
        nc += dc[dir];
        if (a[nr][nc] == 1 || a[nr][nc] == 2return 0;
        if (a[nr][nc] == 3break;
    }
     
    return i - 1//길이
}
 
void Attach(int dir, int L, int len)
{
    int i;
    int nr, nc;
 
    nr = core[L].r;
    nc = core[L].c;
 
    for (i = 1; i <= len; i++)
    {
        nr += dr[dir];
        nc += dc[dir];
        a[nr][nc] = 2;
    }
}
 
void Detach(int dir, int L, int len)
{
    int i;
    int nr, nc;
 
    nr = core[L].r;
    nc = core[L].c;
 
    for (i = 1; i <= len; i++)
    {
        nr += dr[dir];
        nc += dc[dir];
        a[nr][nc] = 0;
    }
}
 
void DFS(int L, int C, int W)
{
    int i;
 
    if (C + (K - L + 1< max_core) return;
    if (L > K)
    {
        if (C >= max_core && W < min_wire[C])
        {
            //printf("[%d] W:%d\n", C,W);
            //VVV();
            max_core = C;
            min_wire[C] = W;
        }
        return;
    }
 
    //전원?
    if (core[L].p == 1)
    {
        DFS(L + 1, C + 1, W);
    }
    else
    {
        //코어L 전선설치 4방향
        for (i = 1; i <= 4; i++)
        {
            int hub;
 
            hub = check(i, L);
            if (hub == 0continue;
            Attach(i, L, hub);
            DFS(L + 1, C + 1, W + hub);
            Detach(i, L, hub);
        }
        //코어L 전선설치 x
        DFS(L + 1, C, W);
    }
}
 
void output(void)
{
    printf("#%d %d\n", tc, min_wire[max_core]);
}
 
int main(void)
{
    scanf("%d"&T);
    for (tc = 1; tc <= T; tc++)
    {
        init();
        input();
        DFS(100);
        output();
    }
}
 
#endif
cs


'알고리즘 > SW Expert Academy' 카테고리의 다른 글

1210 Ladder1  (0) 2018.09.20
1204 최빈수 구하기  (0) 2018.09.20
1206 View  (0) 2018.09.18
1251 하나로  (0) 2018.09.18
1868 파핑파핑 지뢰찾기  (0) 2018.09.18

+ Recent posts