Dancing Links_模版
这个模版是我从SuDoKu_DLX上面分离出来的,我也是刚接触DLX,总的来说只要能转换为精确覆盖问题的问题,都可以用DLX来解决,而且速度绝对是数一数二的。。。
1 const int SIZE = 16; 2 const int MAX_C = SIZE * SIZE * 4; //最大列 3 const int MAX_R = SIZE * SIZE * SIZE; //最大行 4 const int MAX_LINK = MAX_C * MAX_R; //链表最大范围 5 6 int L[MAX_LINK],R[MAX_LINK],U[MAX_LINK],D[MAX_LINK]; //抽象链表 7 int C[MAX_LINK],O[MAX_LINK],S[MAX_C],H[MAX_R]; //C&O代表列&行,S每一列的节点数,H每一行的第一个节点 8 int NodeNumber; //用来指向节点 9 //////////////////////Dancing Links模版////////////////////// 10 void init(void); //Dancing Links的抽象链表初始化 11 void insert(int,int); //在链表的一个位置中添加标记 12 void remove(int); //删除一列,同时删除这一列中的行 13 void resume(int); //恢复一列,同时恢复这一列中的行 14 //////////////////////Dancing Links模版////////////////////// 15 16 void init(void) 17 { 18 for(int i=0;i<=MAX_C;i++) 19 { 20 L[i]=i-1; 21 R[i]=i+1; 22 U[i]=i; 23 D[i]=i; 24 C[i]=i; 25 O[i]=0; 26 } 27 L[0]=MAX_C; 28 R[MAX_C]=0; 29 NodeNumber=MAX_C+1; 30 memset(S,0,sizeof(S)); 31 memset(H,0,sizeof(H)); 32 } 33 34 void insert(int i,int j) 35 { 36 if(H[i]) 37 { 38 L[NodeNumber]=L[H[i]]; 39 R[NodeNumber]=H[i]; 40 L[R[NodeNumber]]=NodeNumber; 41 R[L[NodeNumber]]=NodeNumber; 42 } 43 else 44 { 45 L[NodeNumber]=NodeNumber; 46 R[NodeNumber]=NodeNumber; 47 H[i]=NodeNumber; 48 } 49 U[NodeNumber]=U[j]; 50 D[NodeNumber]=j; 51 U[D[NodeNumber]]=NodeNumber; 52 D[U[NodeNumber]]=NodeNumber; 53 C[NodeNumber]=j; 54 O[NodeNumber]=i; 55 S[j]++; 56 NodeNumber++; 57 } 58 59 void remove(int c) 60 { 61 L[R[c]]=L[c]; 62 R[L[c]]=R[c]; 63 for(int i=D[c];i!=c;i=D[i]) 64 { 65 for(int j=R[i];j!=i;j=R[j]) 66 { 67 U[D[j]]=U[j]; 68 D[U[j]]=D[j]; 69 S[C[j}]--; 70 } 71 } 72 } 73 74 void resume(int c) 75 { 76 for(int i=U[c];i!=c;i=U[i]) 77 { 78 for(int j=L[i];j!=i;j=L[j]) 79 { 80 U[D[j]]=j; 81 D[U[j]]=j; 82 S[C[j]]++; 83 } 84 } 85 L[R[c]]=c; 86 R[L[c]]=c; 87 }
——现在的努力是为了小时候吹过的牛B!!
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步