-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwormhole.cpp
More file actions
174 lines (156 loc) · 3.75 KB
/
wormhole.cpp
File metadata and controls
174 lines (156 loc) · 3.75 KB
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
/*
ID: dswei191
LANG: C++
TASK: wormhole
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
ofstream fout("wormhole.out");
ifstream fin("wormhole.in");
#define MAX_N 12
int N;
int ret = 0;
vector<int> pairs(MAX_N+1, -1); //pairs[i] = j,表示i和j是一对
vector<int> nxt(MAX_N+1, -1); //next[i] = j, 表示i的右边第一个为j
vector<pair<int, int> > wormholes(MAX_N+1); //表示wormhole的坐标
vector<bool> visit(MAX_N+1, false); //visit[i],表示i是否被访问过
void print(vector<int> vec){
for(int i = 1; i <=N; i++){
cout<< vec[i] <<endl;
}
}
//检测是否有环路
bool checkLoop(int i) {
/**
* 从一个点出来,一直找到他右侧的第一个点
*
* 1. 如果没有找到,则失败
* 2. 如果找到了 i,且已经被访问过,则成功
* 3. 如果找到了 i,且没有被访问过,则进入到改点的配对点 j,继续checkLoop
*/
// if(nxt[i] == -1){
// return false;
// }
//
// //存在右侧节点
// if(visit[nxt[i]] == true){
// return true;
// }
// //右侧节点没有被访问过
// visit[nxt[i]] = true;
//
// return checkLoop(pairs[nxt[i]]);
if(visit[i] == true){
return true;
}
visit[i] = true;
if(nxt[pairs[i]] == -1){
return false;
}else{
return checkLoop(nxt[pairs[i]]);
}
}
bool checkLoop(){
for(int i = 1; i <= N; i++){
bool noLoop = false;
int start = i;
for(int j = 0; j < N; j++){
int pairIndex = pairs[start];
if(nxt[pairIndex] == -1){
noLoop = true;
break;
}else{
start = nxt[pairIndex];
}
}
if(noLoop == false){
return true;
}
}
return false;
}
void makePairs(){
int i;
for(i = 1; i <= N; i++){
if(pairs[i] == -1)
break;
}
if( i > N){
//所有虫洞都配对好了
// if(checkLoop()){
// ret += 1;
// }
for(int k = 1; k <= N; k++){
for(int t = 1; t <= N; t++){
visit[t] = false;
}
if(checkLoop(k)){
ret +=1;
break;
}
}
}
for(int j = i+1; j <= N; j++){
if(pairs[j] == -1){
pairs[i] = j;
pairs[j] = i;
makePairs();
pairs[i] = -1;
pairs[j] = -1;
}
}
}
void makePairs(int index){
if(index > N){
//所有的虫洞都配对好了
for(int k = 1; k <= N; k++){
for(int t = 1; t <= N; t++){
visit[t] = false;
}
if(checkLoop(k)){
ret +=1;
break;
}
}
return;
}
if(pairs[index] != -1){
makePairs(index + 1);
return;
}else{
for(int i = index + 1; i <= N; i++){
if(pairs[i] == -1){
pairs[index] = i;
pairs[i] = index;
makePairs(index + 1);
pairs[i] = pairs[index] = -1;
}
}
}
}
int main(){
fin>>N;
for(int i = 1; i <=N; i++){
int x, y;
fin>>x>>y;
wormholes[i] = make_pair(x, y);
}
for(int i = 1; i <= N; i++){
int nextIndex = -1;
for(int j = 1; j <= N; j++){
if(wormholes[i].second == wormholes[j].second && wormholes[i].first < wormholes[j].first){
if(nextIndex < 0 || wormholes[nextIndex].first > wormholes[j].first){
nextIndex = j;
}
}
}
nxt[i] = nextIndex;
}
makePairs();
fout<<ret<<endl;
return 0;
}