[백준 2146번] 다리 만들기

2017. 9. 6. 00:12알고리즘/백준

반응형




소스코드


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
#include <iostream>
using namespace std;
 
// 0은 바다, 1은 육지
int n, map[101][101], visit[101][101], cnt=2;
int que[10001], h, t, cque[10001], ch,ct;
int Min = 98765432;
 
void visitInit() {
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            visit[i][j] = 0;
}
 
void bfs() {
    while (h != t){
        int pos = que[h++];
        int x = pos / 1000;
        int y = pos % 1000;
 
        if (x - 1 >= 0 && visit[x - 1][y] == 0) {
            if (map[x - 1][y]==1){
                que[t++= (x - 1* 1000 + y;
                visit[x - 1][y] = 1;
            }
            else if(map[x-1][y]==0) {
                map[x][y] = cnt;
            }
        }
        if (x + 1 < n && visit[x + 1][y] == 0) {
            if (map[x + 1][y]==1) {
                que[t++= (x + 1* 1000 + y;
                visit[x + 1][y] = 1;
            }
            else if(map[x+1][y]==0){
                map[x][y] = cnt;
            }
        }
        if (y - 1 >= 0 && visit[x][y-1== 0) {
            if (map[x][y - 1]==1) {
                que[t++= x * 1000 + (y-1);
                visit[x][y-1= 1;
            }
            else if(map[x][y-1]==0){
                map[x][y] = cnt;
            }
        }
        if (y + 1 < n && visit[x][y + 1== 0) {
            if (map[x][y + 1]==1) {
                que[t++= x * 1000 + (y + 1);
                visit[x][y + 1= 1;
            }
            else if(map[x][y+1]==0){
                map[x][y] = cnt;
            }
        }
        
    }
}
 
 
int minPath(int myNum) {
    while (h != t) {
        int pos = que[h++];
        int x = pos / 1000;
        int y = pos % 1000;
        int c = cque[ch++];
 
        if (x - 1 >= 0 && visit[x - 1][y] == 0) {
            if (map[x - 1][y] == 0) {
                que[t++= (x - 1* 1000 + y;
                cque[ct++= c + 1;
                visit[x - 1][y] = 1;
            }
            else if (map[x - 1][y] != myNum && map[x-1][y]!=1) {
                return c;
            }
        }
        if (x + 1 < n && visit[x + 1][y] == 0) {
            if (map[x + 1][y] == 0) {
                que[t++= (x + 1* 1000 + y;
                cque[ct++= c + 1;
                visit[x + 1][y] = 1;
            }
            else if (map[x + 1][y] != myNum&& map[x + 1][y] != 1) {
                return c;
            }
        }
        if (y - 1 >= 0 && visit[x][y-1== 0) {
            if (map[x][y-1== 0) {
                que[t++= x * 1000 + (y-1);
                cque[ct++= c + 1;
                visit[x][y-1= 1;
            }
            else if (map[x][y - 1!= myNum&& map[x][y-1!= 1) {
                return c;
            }
        }
        if (y + 1 < n && visit[x][y + 1== 0) {
            if (map[x][y + 1== 0) {
                que[t++= x * 1000 + (y + 1);
                cque[ct++= c + 1;
                visit[x][y + 1= 1;
            }
            else if (map[x][y + 1!= myNum && map[x][y+1!= 1) {
                return c;
            }
        }
    }
}
 
int main() {
    cin >> n;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            cin >> map[i][j];
 
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n;j++)
            if (map[i][j]==1 && visit[i][j]==0) {
                h = 0, t = 0;
                que[t++= i * 1000 + j;
                bfs();
                cnt++;
            }
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (map[i][j]>1) {
                h = 0, t = 0, ch=0, ct=0;
                visitInit();
                que[t++= i * 1000 + j;
                cque[ct++= 1;
                int ret = minPath(map[i][j]);
                if (ret < Min)
                    Min = ret;
            }
        }
    }
 
    cout << Min-1 << '\n';
    return 0;
}
cs


반응형

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

[백준 14723번] 이산수학 과제  (0) 2017.09.19
[백준 10886번] 0 = not cute / 1 = cute  (0) 2017.09.18
[백준 1251번] 단어 나누기  (0) 2017.09.06
[백준 4948번] 베르트랑 공준  (0) 2017.09.03
[백준 1063번] 킹  (0) 2017.09.03