- 注册时间
- 2007-12-27
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 40150
- 在线时间
- 小时
|
发表于 2009-10-16 09:20:53
|
显示全部楼层
修改后的代码如下:
此外如果内存还是太小,宏STABLE还可以改小一半左右.但是需要注意STABLE和WALK必须互素.- // chp.cpp : Defines the entry point for the console application.
- //
-
- #include "stdafx.h"
- #include <string.h>
- #include <assert.h>
- #include <stdlib.h>
- #define N 8
- #define BITS (N*N)
- typedef unsigned long long SET;
- SET nghb[BITS];
- #define EMPTY_SET 0LL
- #define INDEX(x,y) ((x)*N+(y))
- #define GET_X(ind) ((ind)/N)
- #define GET_Y(ind) ((ind)%N)
- #define ADD_INDEX(s, ind) ((s)|=1LL<<(ind))
- #define TEST_INDEX(s, ind) (((s)&(1LL<<(ind)))!=0)
- #define CORNMASK (3|(3<<N))
- #define TEST_BIT(x, b) (((x)&(1LL<<(b)))!=0)
- #define SET_BIT(x, b) ((x)|=(1LL<<(b)))
-
- #define GET_CORN(x) (((x)&3)| (((x)>>N)&3)<<2)
- #define DISP_CORN(x) (((x)&3)| ((((x)>>2)&3)<<N))
- #define ECVALUE(x) (((x)&~CORNMASK)|DISP_CORN(cr[GET_CORN(x)]))
-
- #define STABLE 50000033
- #define WALK 1000
- int hc;
- int ec;
- int nec;
- int lec;
- long long htable[STABLE];
- //long long htable2[STABLE];
- char cr[16]={1,-1,-1,-1,-1,-1,-1,-1,4,-1,5,-1,3,-1,11,7};
- char rc[16]={-1,0,-1,12,8,10,-1,15,-1,-1,-1,14,-1,-1,-1,-1};
-
- FILE *fout;
- void dump(long long v)
- {
- int i,j;
- for(i=0;i<N;i++){
- for(j=0;j<N;j++){
- if(TEST_BIT(v,i*N+j)){
- fprintf(fout,"1 ");
- }else{
- fprintf(fout,"0 ");
- }
- }
- fprintf(fout,"\n");
- }
- fprintf(fout,"\n");
- }
-
- void copy_ec(long long *p)
- {
- int i,t;
- for(i=0,t=0;i<STABLE;i++){
- if(htable[i]!=-1LL){
- if(cr[GET_CORN(htable[i])]!=-1){
- p[t++]=htable[i];
- }
- }
- }
- }
-
- void dump_ec(long long htable[])
- {
- int i;
- fout = fopen("dump.txt","w");
- for(i=0;i<STABLE;i++){
- if(htable[i]!=-1LL){
- if(cr[GET_CORN(htable[i])]!=-1){
- dump(htable[i]);
- }
- }
- }
- fclose(fout);
- }
-
- void init()
- {
- memset(htable,-1,sizeof(htable));
- // memset(htable2,-1,sizeof(htable2));
- }
-
- int hash(SET s)
- {
- return (int)(s%STABLE);
- }
-
- int add_hash(long long htable[],long long key, bool eq)
- {
- int v=hash(key);
- long long ev;
- ev=ECVALUE(key);
- if(hc>=STABLE){
- fprintf(stderr,"out of hash table range\n");
- exit(-1);
- }
- do{
- if(htable[v]==-1LL){
- if(eq){
- htable[v]=ev;nec++;
- }else{
- htable[v]=key;
- ec++;
- }
- hc++;
- return v;
- }else if(htable[v]!=key&&htable[v]!=ev){
- v=(v+WALK)%STABLE;
- }else{
- if(eq){
- assert(htable[v]==ev);
- }else{
- assert(htable[v]==key);
- }
- return v;
- }
- }while(1);
- }
-
- int query_hash_entry(long long htable[],long long key)///return index to hash table
- {
- long long ev=ECVALUE(key);
- int v=hash(key);
- do{
- if(htable[v]==-1LL)return v;
- if(htable[v]==key||htable[v]==ev)
- return v;
- v=(v+WALK)%STABLE;
- }while(1);
- }
-
- void init_neighbour()
- {
- int i,j;
- for(i=0;i<N;i++){
- for(j=0;j<N;j++){
- SET s=EMPTY_SET;
- ADD_INDEX(s,INDEX(i,j));
- if(i>0&&j>0){
- ADD_INDEX(s,INDEX(i-1,j-1));
- }
- if(i>0){
- ADD_INDEX(s,INDEX(i-1,j));
- }
- if(j>0){
- ADD_INDEX(s,INDEX(i,j-1));
- }
- if(i>0&&j<N-1){
- ADD_INDEX(s,INDEX(i-1,j+1));
- }
- if(j<N-1){
- ADD_INDEX(s,INDEX(i,j+1));
- }
- if(i<N-1&&j>0){
- ADD_INDEX(s,INDEX(i+1,j-1));
- }
- if(i<N-1){
- ADD_INDEX(s,INDEX(i+1,j));
- }
- if(i<N-1&&j<N-1){
- ADD_INDEX(s,INDEX(i+1,j+1));
- }
- nghb[INDEX(i,j)]=s;
- }
- }
- }
- #if 0
- void visit(long long neighb)
- {
- int i;
- int e=query_hash_entry(htable,neighb);
- if(htable[e]==neighb){
- int ee=query_hash_entry(htable2,neighb);
- if(htable2[ee]==-1){
- htable2[ee]=neighb;
- lec++;
- for(i=0;i<BITS;i++){
- if(TEST_BIT(neighb,i)==0){
- long long newneighb=neighb;
- newneighb|=nghb[i];
- visit(newneighb);
- }
- }
- }
- }else{
- for(i=0;i<BITS;i++){
- if(TEST_BIT(neighb,i)==0){
- long long newneighb=neighb;
- newneighb|=nghb[i];
- int e2=query_hash_entry(htable,newneighb);
- if(htable[e2]==newneighb){
- visit(newneighb);
- break;
- }
- }
- }
- }
- }
- #endif
- int query(long long neighb)
- {
- int e=query_hash_entry(htable,neighb);
- if(htable[e]!=-1)
- return e;
- int i;
- for(i=0;i<BITS;i++){
- if(TEST_BIT(neighb,i)==0){
- long long newneighb=neighb;
- newneighb|=nghb[i];
- int r=query(newneighb);
- if(htable[r]==newneighb)
- break;
- }
- }
- if(i<BITS){///find any status is newneighb
- return add_hash(htable,neighb,true);
- }else{
- return add_hash(htable,neighb,false);
- }
- }
-
- int cmpll(const void *p,const void *q)
- {
- const long long *lp=(const long long *)p;
- const long long *lq=(const long long *)q;
- if(*lp==*lq)return 0;
- if(*lp<*lq)return -1;
- return 1;
- }
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- int i,j;
- init();
- init_neighbour();
- int s=query(0LL);
- if(htable[s]==0LL){
- printf("First fail\n");
- }else{
- printf("First successful\n");
- }
- printf("Total table size %d\n",hc);
- printf("Total eq stat found %d\n",ec);
- printf("Total neq stat found %d\n",nec);
- #if 0
- for(i=0;i<BITS;i++){
- long long r=nghb[i];
- printf("%d set={ ",i);
- for(j=0;j<BITS;j++){
- if(!TEST_BIT(r,j)){
- long long newneighb=r;
- newneighb|=nghb[j];
- int u=query_hash_entry(htable,newneighb);
- if(htable[u]==newneighb){
- printf("%d ",j);
- }
- }
- }
- printf("}\n");
- }
- #endif
- return 0;
- }
复制代码 |
|