-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs_labelling_matrix_imp.cpp
212 lines (196 loc) · 4.93 KB
/
bfs_labelling_matrix_imp.cpp
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
211
212
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <cmath>
#include <stack>
#include <queue>
#include <utility>
#define size 19
using namespace std;
class graph {
public:
bool input[size][size], adj[size][size][4], adj_c[size][size][4];
int label_source[size][size], label_sink[size][size], source_p, sink_p;
graph() {
memset(label_source, 0, sizeof(int)*size*size);
memset(label_sink, 0, sizeof(int)*size*size);
memset(adj, 0, sizeof(int)*size*size*4);
source_p = 0;
sink_p = 17;
}
void read_input() {
int i, j;
for(i=0; i<size; i++) {
for(j=0; j<size; j++) cin>>input[i][j];
}
}
void make_adj() {
int i, j;
for(i=0; i<size; i++) {
adj[i][0][0] = 0;
adj[0][i][1] = 0;
adj[i][size-1][2] = 0;
adj[size-1][i][3] = 0;
}
for(i=0; i<size; i++) {
for(j=1; j<size; j++) {
if(input[i][j]) adj[i][j][0] = input[i][j-1];
}
}
for(i=1; i<size; i++) {
for(j=0; j<size; j++) {
if(input[i][j]) adj[i][j][1] = input[i-1][j];
}
}
for(i=0; i<size; i++) {
for(j=0; j<size-1; j++) {
if(input[i][j]) adj[i][j][2] = input[i][j+1];
}
}
for(i=0; i<size-1; i++) {
for(j=0; j<size; j++) {
if(input[i][j]) adj[i][j][3] = input[i+1][j];
}
}
memcpy(adj_c, adj, sizeof(bool)*size*size*4);
}
void assign_label() {
int i, j;
for(i=0; i<size; i++) {
for(j=0; j<size; j++) {
if(input[i][j]) {
label_source[i][j] = i+j;
label_sink[i][j] = j+abs(i-sink_p);
}
}
}
/*for(i=0; i<size; i++) {
* for(j=0; j<size; j++) cout<<"("<<label_source[i][j]<<", "<<label_sink[i][j]<<") ";
* cout<<endl;
}*/
}
int cr_b_i, cr_b_j, cr_e_i, cr_e_j, i, j;
int find_critical_edge() {
int i, j;
/*for(i=0; i<size; i++) {
* for(j=0; j<size; j++) {
* for(int k=0; k<4; k++) cout<<adj_c[i][j][k]<<" ";
* cout<<" ";
}
cout<<endl;
}*/
for(j=size-1; j>=0; j--)
{
for(i=0; i<size; i++)
{
//cout<<i<<" "<<j<<endl;
if(adj_c[i][j][1])
{
cr_b_i=i-1;
cr_b_j=j;
cr_e_i=i;
cr_e_j=j;
adj_c[i][j][1] = 0;
adj_c[i][j][3] = 0;
return 0;
}
else if(adj_c[i][j][0])
{
cr_b_i=i;
cr_b_j=j-1;
cr_e_i=i;
cr_e_j=j;
adj_c[i][j][0] = 0;
adj_c[i][j-1][2] = 0;
return 0;
}
}
}
/* all edges got covered */
if( i==size && j==-1) cr_b_i = -1;//finish
/*find path from source*/
}
void find_paths() {
int total_path_length = 0, count = 0;
make_adj();
assign_label();
while(1) {
find_critical_edge();
if(cr_b_i == -1) break;
count++;
int max_path_length = 0, path_length = 1;
i = cr_b_i;
j = cr_b_j;
path_length = label_source[cr_b_i][cr_b_j] + label_sink[cr_e_i][cr_e_j] + 1;
if(!max_path_length) max_path_length = path_length;
total_path_length += path_length;
stack< pair<int, int> > source;
queue< pair<int, int> > sink;
//source.push(make_pair(cr_e_i, cr_e_j));
while(i>source_p || j>0) {
source.push(make_pair(i, j));
if(adj_c[i][j][1] && (label_source[i][j] == label_source[i-1][j]+1)) {
adj_c[i][j][1] = 0;
adj_c[i-1][j][3] = 0;
i--;
}
else if(adj_c[i][j][0] && (label_source[i][j] == label_source[i][j-1]+1)) {
adj_c[i][j][0] = 0;
adj_c[i][j-1][2] = 0;
j--;
}
else adj[i][j][0] ? j-- : i--;
}
source.push(make_pair(source_p, 0));
i = cr_e_i;
j = cr_e_j;
while(i!=sink_p || j>0) {
//cout<<i<<","<<j<<" ";
sink.push(make_pair(i, j));
if(adj_c[i][j][0] && (label_sink[i][j] == label_sink[i][j-1]+1)) {
adj_c[i][j][0] = 0;
adj_c[i][j-1][2] = 0;
/*if(max_path_length >= path_length+2) {
* int k = i, l = j, slag;
* while(k>=0 && adj_c[k][l][1] && slag) {k--;}
}*/
j--;
}
else if(adj_c[i][j][3] && (label_sink[i][j] == label_sink[i+1][j]+1)) {
adj_c[i][j][3] = 0;
adj_c[i+1][j][1] = 0;
i++;
}
else if(adj_c[i][j][1] && (label_sink[i][j] == label_sink[i-1][j]+1) && i > sink_p) {
adj_c[i][j][1] = 0;
adj_c[i-1][j][3] = 0;
i--;
}
else if(adj[i][j][0]) j--;
else if(adj[i][j][3]) i++;
else i--;
if(!max_path_length) max_path_length = path_length;
}
//getchar();
sink.push(make_pair(sink_p, 0));
cout<<"Path "<<count<<" : ";
while(!source.empty()) {
cout<<"("<<source.top().first<<","<<source.top().second<<") -> ";
source.pop();
}
while(!sink.empty()) {
cout<<"("<<sink.front().first<<","<<sink.front().second<<") -> ";
sink.pop();
}
cout<<"(length = "<<path_length<<")"<<endl;
}
cout<<"Number of paths : "<<count<<endl;
cout<<"Total path length : "<<total_path_length;
}
};
int main() {
graph g;
g.read_input();
g.find_paths();
return 0;
}