-
Notifications
You must be signed in to change notification settings - Fork 0
/
deep_first_searching.cpp
113 lines (96 loc) · 2.96 KB
/
deep_first_searching.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
#include<iostream>
#include<vector>
#include<tuple>
using namespace std;
#define MAX_VERTICE 50 /*maximum number of vertices*/
typedef struct node *node_pointer;
typedef struct node{
int row;
int col;
struct node *link;
};
//vector <node_pointer>* VERTEX_LIST=new vector<node_pointer>(MAX_VERTICE);
vector <node_pointer> VERTEX_LIST;
vector <vector<int>> ADJACENT_LIST;
int n=0;
/*vertices currently in use*/
void buid_matrix(vector<vector<char> >*M){
//cout<<"here size get"<<(*M).size()<<(*M)[0].size()<<endl;
int row=(*M)[0].size();
int col=(*M).size()/(*M)[0].size();
char ele;
for(int k=0;k<col;k++){
(*M)[0][k]='.';
}
for(int j=1;j<row-1;j++){
(*M)[j][0]='.';
for(int i=1;i<col-1;i++){
cin>>ele;
(*M)[j][i]=ele;
}
(*M)[j][col-1]='.';
}
for(int h=0;h<col;h++){
(*M)[row-1][h]='.';
}
}
void GRAPH_LIST_generation(vector<vector<char> >*M){
int row=(*M)[0].size();
int col=(*M).size()/(*M)[0].size();
for(int j=1;j<row-1;j++){
for(int i=1;i<col-1;i++){
//下面生成graph
if(((*M)[j][i])=='*'){
//cout<<"row="<<i<<" col="<<j<<" ="<<(*M)[j][i]<<endl;
node_pointer vertex;
vertex=(node_pointer)malloc(sizeof(node_pointer));
vertex->row=j;vertex->col=i;
VERTEX_LIST.push_back(vertex);
//test
//cout<<(*VERTEX_LIST)[0]<<endl;
}
else{}
//上面生成graph
}
cout<<endl;
}
/*
while(VERTEX_LIST.empty()!=true){
cout<<(VERTEX_LIST.back())->row<<" "<<(VERTEX_LIST.back())->col<<endl;
cout<<VERTEX_LIST[0]->row<<VERTEX_LIST[0]->col<<endl;
VERTEX_LIST.pop_back();
}*/
//cout<<"judge start"<<endl;
for(int loc=0;loc<VERTEX_LIST.size();loc++){
vector<int> adjacent_unit;
for(int k=0;k<VERTEX_LIST.size();k++){
//cout<<VERTEX_LIST[k]->col<<endl;
//檢查比鄰
if((loc!=k)&&((abs(VERTEX_LIST[k]->col-VERTEX_LIST[loc]->col)<2)&&(abs(VERTEX_LIST[k]->row-VERTEX_LIST[loc]->row)<2))){
//cout<<"in the judgement"<<endl;
adjacent_unit.push_back(k);
}
}
ADJACENT_LIST.push_back(adjacent_unit);
adjacent_unit.clear();
}
for(int t=0;t<ADJACENT_LIST.size();t++){
cout<<"the connected unit ["<<t<<"] number ="<<ADJACENT_LIST[t].size()<<endl;
/*
for(int h=0;h<ADJACENT_LIST[t].size();h++){
cout<<"{"<<ADJACENT_LIST[t][h]<<"}";
}*/
cout<<endl;
}
}
int main(){
int i,j;
while((i!=0)&&(j!=0)){
cin>>i>>j;
vector<vector<char> >*M = new vector<vector<char> >((i+2)*(j+2), vector<char>(i+2));
//cout<<(*M).size()<<endl;
buid_matrix(M);
GRAPH_LIST_generation(M);
}
return 0;
}