生活随笔
收集整理的這篇文章主要介紹了
1352. 虫洞【难度: 中 / 枚举 判环】
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
https://www.acwing.com/problem/content/1354/
#include<bits/stdc++.h>
using namespace std
;
const int N
=15;
int n
;
int to1
[N
],to2
[N
];
bool st
[N
],used
[N
][2],cur
[N
][2];
struct node{int x
,y
;}S
[N
];
bool cmp(node a
,node b
)
{if(a
.y
==b
.y
) return a
.x
<b
.x
;return a
.y
>b
.y
;
}
int ans
;
bool dfs_c(int a
,int b
)
{if(cur
[a
][b
]) return true;if(used
[a
][b
]) return false;cur
[a
][b
]=used
[a
][b
]=true;bool res
=false;if(!b
){if(dfs_c(to2
[a
],1)) res
=true;}else{if(to1
[a
]!=-1&&dfs_c(to1
[a
],0)) res
=true;} cur
[a
][b
]=false;return res
;
}
bool check()
{memset(used
,0,sizeof used
);memset(cur
,0,sizeof cur
);for(int i
=0;i
<n
;i
++)for(int j
=0;j
<2;j
++) if(dfs_c(i
,j
)) return true;return false;
}
void dfs(int u
)
{if(u
==n
/2){if(check()) ans
++;return;}for(int i
=0;i
<n
;i
++){if(!st
[i
]){for(int j
=i
+1;j
<n
;j
++){if(!st
[j
]){st
[i
]=st
[j
]=true;to2
[i
]=j
,to2
[j
]=i
;dfs(u
+1);to2
[i
]=to2
[j
]=-1;st
[i
]=st
[j
]=false;}}break;}}
}
int main(void)
{cin
>>n
;for(int i
=0;i
<n
;i
++) cin
>>S
[i
].x
>>S
[i
].y
;sort(S
,S
+n
,cmp
);memset(to1
,-1,sizeof to1
);memset(to2
,-1,sizeof to2
);for(int i
=1;i
<n
;i
++)if(S
[i
].y
==S
[i
-1].y
) to1
[i
-1]=i
;dfs(0);cout
<<ans
<<endl
;return 0;
}
總結(jié)
以上是生活随笔為你收集整理的1352. 虫洞【难度: 中 / 枚举 判环】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。