版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Hepburn_Sunsir/article/details/89076561
http://hihocoder.com/problemset/problem/1054 dfs
#include<cstring>
#include<cstdio>
using namespace std;
int test[10][10], pos[10], N, M, cnt, es[10][10];
bool vis[10];
void init(){
test[1][3]=test[3][1]=2;
test[4][6]=test[6][4]=5;
test[7][9]=test[9][7]=8;
test[1][7]=test[7][1]=4;
test[2][8]=test[8][2]=5;
test[3][9]=test[9][3]=6;
test[1][9]=test[9][1]=5;
test[3][7]=test[7][3]=5;
}
void dfs(int depth){
if(depth >4){
int k = 0;
for(int i = 2; i<depth; i++)
if(es[pos[i-1]][pos[i]]) k++;
if(k==M) cnt++;
}
for(int i=1; i<=9; i++){
if(!vis[i]){
if(test[i][pos[depth-1]] && !vis[test[i][pos[depth-1]] ])continue;
vis[i]=true;
pos[depth]=i;
dfs(depth+1);
vis[i]=false;
}
}
}
//int huadongjiesuo(){
int main(){
memset(test, 0, sizeof(test));
init();
scanf("%d",&N);
int i, x, y;
while(N--){
scanf("%d",&M);
memset(vis, 0, sizeof(vis));
memset(es, 0, sizeof(es));
for(i=0; i<M; i++){
scanf("%d%d", &x, &y);
es[x][y]=es[y][x]=1;
}
cnt=0;
dfs(1);
printf("%d\n",cnt);
}
return 0;
}
开始刷机试题的第二天,对于递归,函数调用,作用域:翻书回顾!