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=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