| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1419 人关注过本帖
标题:”魔方阵“ 和 ”八皇后“的算法?
只看楼主 加入收藏
ericyoubin
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-9-12
收藏
得分:0 

//想到一个算法,在网上居然没有找到类似的解法。如果要转换成纯C语言的,只需要将几个打印的地方替换一下就行了。
void switchpos(int * arr, int x, int y)
{
int tempv;
tempv = arr[x];
arr[x]=arr[y];
arr[y] = tempv;
}
#define MAX_COUNT 8
int arr[MAX_COUNT]={1,2,4,8,16,32,64,128};
void printBinaryForInteger(int &integer)
{
int k,a=0;
int b[8]={0,0,0,0,0,0,0,0};
while (integer!=0)
{
k=integer%2;
b[a++]=k;
integer=integer/2;
}
a=8 ;
while (a>0)
{
cout<<b[--a];
}
cout<<endl;
}

int all_count=0;
void getlist(int *intarr,int *arrhead,int n)
{
int *arrtemp= new int[n];
int *headtemp= new int[MAX_COUNT];
for (int x=0;x<MAX_COUNT;x++)
headtemp[x]= arrhead[x];
for (int x=0;x<n;x++)
{
arrtemp[x]=intarr[x];
}
if (n>1)
{
for (int i=0;i<n;i++)
{
if (i!=0) //第一次不用颠倒首元素的位置
{
switchpos(arrtemp,0,i);
for (int x=0;x<n;x++)
{
headtemp[MAX_COUNT-n+x]= arrtemp[x];
}
bool bFind=false;
for (int x=0;x<MAX_COUNT;x++)
{
for (int y=1;y<MAX_COUNT;y++)
{
if ((x+y<MAX_COUNT&& headtemp[x]*pow(2,y)==headtemp[x+y])
|| (x-y>=0&& headtemp[x]*pow(2,y)==headtemp[x-y]))
{
bFind=true;
break;
}
}
if (bFind) break;
}
if (!bFind)
{
all_count++;
for (int x=0;x<MAX_COUNT;x++)
{
int tempi= headtemp[x];
printBinaryForInteger(tempi);
}
cout<<"--------------"<<all_count<<endl;
}
}
headtemp[MAX_COUNT-n]= arrtemp[0];
getlist(arrtemp+1,headtemp, n-1);
}
}
delete []arrtemp;
delete []headtemp;
}

int main(void)
{
int arrhead[8];
getlist(arr,arrhead,MAX_COUNT);
getchar();
return 0;
}

2007-09-17 22:38
爱以走远
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:52
帖 子:7542
专家分:21
注 册:2007-3-16
收藏
得分:0 
呵呵
研究这个没意思
偶就看拉魔方
什么回朔还没看过

   好好活着,因为我们会死很久!!!
2007-09-17 22:39
快速回复:”魔方阵“ 和 ”八皇后“的算法?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020015 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved