本文目錄一覽:
- 1、求一個A*算法的C語言或C++代碼,小弟不勝感激,謝謝
- 2、求8數碼A或A*算法(用C語言)
- 3、求三階矩陣A的逆矩陣C語言算法程序
- 4、C語言a=a++的運算順序是怎麼樣的?
- 5、用C語言怎麼編一個a^n(a的n次方)的算法?
- 6、C語言算法a(n)=2^n+1過程怎麼寫!急!
求一個A*算法的C語言或C++代碼,小弟不勝感激,謝謝
1#include iostream
2#include queue
3usingnamespace std;
4
5struct knight{
6int x,y,step;
7int g,h,f;
8booloperator (const knight k) const{ //重載比較運算符
9return f k.f;
10 }
11}k;
12bool visited[8][8]; //已訪問標記(關閉列表)
13int x1,y1,x2,y2,ans; //起點(x1,y1),終點(x2,y2),最少移動次數ans
14int dirs[8][2]={{-2,-1},{-2,1},{2,-1},{2,1},{-1,-2},{-1,2},{1,-2},{1,2}};//8個移動方向
15priority_queueknight que; //最小優先級隊列(開啟列表)
16
17boolin(const knight a){ //判斷knight是否在棋盤內
18if(a.x0|| a.y0|| a.x=8|| a.y=8)
19returnfalse;
20returntrue;
21}
22int Heuristic(const knight a){ //manhattan估價函數
23return (abs(a.x-x2)+abs(a.y-y2))*10;
24}
25void Astar(){ //A*算法
26 knight t,s;
27while(!que.empty()){
28 t=que.top(),que.pop(),visited[t.x][t.y]=true;
29if(t.x==x2 t.y==y2){
30 ans=t.step;
31break;
32 }
33for(int i=0;i8;i++){
34 s.x=t.x+dirs[i][0],s.y=t.y+dirs[i][1];
35if(in(s) !visited[s.x][s.y]){
36 s.g = t.g +23; //23表示根號5乘以10再取其ceil
37 s.h = Heuristic(s);
38 s.f = s.g + s.h;
39 s.step = t.step +1;
40 que.push(s);
41 }
42 }
43 }
44}
45int main(){
46char line[5];
47while(gets(line)){
48 x1=line[0]-‘a’,y1=line[1]-‘1’,x2=line[3]-‘a’,y2=line[4]-‘1’;
49 memset(visited,false,sizeof(visited));
50 k.x=x1,k.y=y1,k.g=k.step=0,k.h=Heuristic(k),k.f=k.g+k.h;
51while(!que.empty()) que.pop();
52 que.push(k);
53 Astar();
54 printf(“To get from %c%c to %c%c takes %d knight moves.\n”,line[0],line[1],line[3],line[4],ans);
55 }
56return0;
57}
58
求8數碼A或A*算法(用C語言)
題目地址:
BFS:
#include iostream
using namespace std;
int fac[10]={1,1};
bool tflag[9];
struct bbit{
unsigned int val:4;
};
struct bbbit
{
unsigned int val:2;
};
struct Node
{
bbit s[9],pos;
int step;
bbbit path[21],tag;
int hashval()
{
int ret=0,i,j,tmp;
memset(tflag,false,sizeof(tflag));
for(i=0;i8;i++)
{
tmp=0;
for(j=0;js[i].val;j++)
if(!tflag[j])
tmp++;
ret+=tmp*fac[8-i];
tflag[s[i].val]=true;
}
return ret;
}
bool up()
{
if(pos.val=2)return false;
s[pos.val].val^=s[pos.val-3].val;
s[pos.val-3].val^=s[pos.val].val;
s[pos.val].val^=s[pos.val-3].val;
path[step].val=0;
pos.val-=3;
return true;
}
bool down()
{
if(pos.val=6)return false;
s[pos.val].val^=s[pos.val+3].val;
s[pos.val+3].val^=s[pos.val].val;
s[pos.val].val^=s[pos.val+3].val;
path[step].val=1;
pos.val+=3;
return true;
}
bool left()
{
if(pos.val==0||pos.val==3||pos.val==6)return false;
s[pos.val].val^=s[pos.val-1].val;
s[pos.val-1].val^=s[pos.val].val;
s[pos.val].val^=s[pos.val-1].val;
path[step].val=2;
pos.val–;
return true;
}
bool right()
{
if(pos.val==2||pos.val==5||pos.val==8)return false;
s[pos.val].val^=s[pos.val+1].val;
s[pos.val+1].val^=s[pos.val].val;
s[pos.val].val^=s[pos.val+1].val;
path[step].val=3;
pos.val++;
return true;
}
bool operator==(const Nodex)const
{
int i;
for(i=0;i9;i++)if(s[i].val!=x.s[i].val)return false;
return true;
}
}Q[362880],S,A,tmp,top;
struct Hash
{
bool d1,d2;
Node D;
}hash[362880];
inline void mkfac(){int i;for(i=2;i=9;i++)fac[i]=fac[i-1]*i;}
inline int eval(char c){return c==’x’?0:c-‘0’;}
void o(Node x,Node y)
{
int i;
for(i=1;i=x.step;i++)
{
switch(x.path[i].val)
{
case 0:putchar(‘u’);break;
case 1:putchar(‘d’);break;
case 2:putchar(‘l’);break;
case 3:putchar(‘r’);break;
}
}
for(i=y.step;i=1;i–)
switch(y.path[i].val){
case 0:putchar(‘d’);break;
case 1:putchar(‘u’);break;
case 2:putchar(‘r’);break;
case 3:putchar(‘l’);break;
}
puts(“”);
}
int main()
{
char buf[11];
int i,t,l,r;
bool flag;
mkfac();
while(NULL!=gets(buf))
{
t=0;
for(i=0;i=7;i++)A.s[i].val=i+1;A.s[8].val=0;A.pos.val=8;
for(i=0;buf[i];i++)
{
if(buf[i]==’ ‘)continue;
S.s[t].val=eval(buf[i]);
if(S.s[t].val==0)
S.pos.val=t;
t++;
}
l=r=0;
flag=false;
for(i=0;i362880;i++)hash[i].d1=hash[i].d2=false;
S.step=0;S.tag.val=1;
A.step=0;A.tag.val=2;
Q[r++]=S;//tag.val:1
Q[r++]=A;//tag.val:2
while(l=r)
{
top=Q[l++];
top.step++;
tmp=top;
if(tmp.up())
{
if(tmp.tag.val==1)
{
if(!hash[t=tmp.hashval()].d1)
{
hash[t].d1=true;
Q[r++]=tmp;
if(hash[t].d2hash[t].D==tmp)
{
//find ans…
o(tmp,hash[t].D);
goto AA;
}
if(!hash[t].d2)hash[t].D=tmp;
}
}
else
{
if(!hash[t=tmp.hashval()].d2)
{
hash[t].d2=true;
Q[r++]=tmp;
if(hash[t].d1hash[t].D==tmp)
{
//find ans…
o(hash[t].D,tmp);
goto AA;
}
if(!hash[t].d1)hash[t].D=tmp;
}
}
}
tmp=top;
if(tmp.down())
{
if(tmp.tag.val==1)
{
if(!hash[t=tmp.hashval()].d1)
{
hash[t].d1=true;
Q[r++]=tmp;
if(hash[t].d2hash[t].D==tmp)
{
//find ans…
o(tmp,hash[t].D);
goto AA;
}
if(!hash[t].d2)hash[t].D=tmp;
}
}
else
{
if(!hash[t=tmp.hashval()].d2)
{
hash[t].d2=true;
Q[r++]=tmp;
if(hash[t].d1hash[t].D==tmp)
{
//find ans…
o(hash[t].D,tmp);
goto AA;
}
if(!hash[t].d1)hash[t].D=tmp;
}
}
}
tmp=top;
if(tmp.left())
{
if(tmp.tag.val==1)
{
if(!hash[t=tmp.hashval()].d1)
{
hash[t].d1=true;
Q[r++]=tmp;
if(hash[t].d2hash[t].D==tmp)
{
//find ans…
o(tmp,hash[t].D);
goto AA;
}
if(!hash[t].d2)hash[t].D=tmp;
}
}
else
{
if(!hash[t=tmp.hashval()].d2)
{
hash[t].d2=true;
Q[r++]=tmp;
if(hash[t].d1hash[t].D==tmp)
{
//find ans…
o(hash[t].D,tmp);
goto AA;
}
if(!hash[t].d1)hash[t].D=tmp;
}
}
}
tmp=top;
if(tmp.right())
{
if(tmp.tag.val==1)
{
if(!hash[t=tmp.hashval()].d1)
{
hash[t].d1=true;
Q[r++]=tmp;
if(hash[t].d2hash[t].D==tmp)
{
//find ans…
o(tmp,hash[t].D);
goto AA;
}
if(!hash[t].d2)hash[t].D=tmp;
}
}
else
{
if(!hash[t=tmp.hashval()].d2)
{
hash[t].d2=true;
Q[r++]=tmp;
if(hash[t].d1hash[t].D==tmp)
{
//find ans…
o(hash[t].D,tmp);
goto AA;
}
if(!hash[t].d1)hash[t].D=tmp;
}
}
}
}
AA:flag=true;
if(!flag)
puts(“unsolvable”);
}
return 0;
}
A*:
#include iostream
#include queue
using namespace std;
int fac[10]={1,1};
struct Node
{
int s[9],step,pos;
char path[501];
int hashval()
{
int ret=0,i,j,tmp;
bool flag[9];
memset(flag,false,sizeof(flag));
for(i=0;i8;i++)
{
tmp=0;
for(j=0;js[i];j++)
if(!flag[j])
tmp++;
ret+=tmp*fac[8-i];
flag[s[i]]=true;
}
return ret;
}
bool up()
{
if(pos=2)return false;
s[pos]^=s[pos-3];
s[pos-3]^=s[pos];
s[pos]^=s[pos-3];
path[step]=’u’;
pos-=3;
return true;
}
bool down()
{
if(pos=6)return false;
s[pos]^=s[pos+3];
s[pos+3]^=s[pos];
s[pos]^=s[pos+3];
path[step]=’d’;
pos+=3;
return true;
}
bool left()
{
if(pos==0||pos==3||pos==6)return false;
s[pos]^=s[pos-1];
s[pos-1]^=s[pos];
s[pos]^=s[pos-1];
path[step]=’l’;
pos–;
return true;
}
bool right()
{
if(pos==2||pos==5||pos==8)return false;
s[pos]^=s[pos+1];
s[pos+1]^=s[pos];
s[pos]^=s[pos+1];
path[step]=’r’;
pos++;
return true;
}
bool operator==(const Nodex)const
{
int i;
for(i=0;i9;i++)if(s[i]!=x.s[i])return false;
return true;
}
void show()
{
int i,j;
for(i=0;i=6;i+=3,coutendl)
for(j=i;j=i+2;j++)
couts[j];
}
bool operator(const Nodex)const
{
int la=0,lb=0,i;
for(i=0;i8;i++)if(s[i]!=i+1)la++;la+=(s[8]!=0);
for(i=0;i8;i++)if(x.s[i]!=i+1)lb++;lb+=(x.s[8]!=0);
return lalb;
}
}S,A,tmp,top;
priority_queueNode Q;
bool hash[362880];
void mkfac(){int i;for(i=2;i=9;i++)fac[i]=fac[i-1]*i;}
int eval(char c){return c==’x’?0:c-‘0’;}
void output(Node x)
{
int i;
for(i=1;i=x.step;i++)
putchar(x.path[i]);
puts(“”);
}
int main()
{
char buf[11];
int i,t,l,r;
bool flag;
mkfac();
while(NULL!=gets(buf))
{
t=0;
for(i=0;i=7;i++)A.s[i]=i+1;A.s[8]=0;A.pos=8;
for(i=0;buf[i];i++)
{
if(buf[i]==’ ‘)continue;
S.s[t]=eval(buf[i]);
if(S.s[t]==0)
S.pos=t;
t++;
}
l=r=0;
flag=false;
memset(hash,false,sizeof(hash));
S.step=0;
while(!Q.empty())Q.pop();
Q.push(S);
while(!Q.empty())
{
top=Q.top();
Q.pop();
top.step++;
tmp=top;
if(tmp.up())
{
if(!hash[t=tmp.hashval()])
{
hash[t]=true;
Q.push(tmp);
if(tmp==A)
{
//find ans…
output(tmp);
goto AA;
}
}
}
tmp=top;
if(tmp.down())
{
if(!hash[t=tmp.hashval()])
{
hash[t]=true;
Q.push(tmp);
if(tmp==A)
{
//find ans…
output(tmp);
goto AA;
}
}
}
tmp=top;
if(tmp.left())
{
if(!hash[t=tmp.hashval()])
{
hash[t]=true;
Q.push(tmp);
if(tmp==A)
{
//find ans…
output(tmp);
goto AA;
}
}
}
tmp=top;
if(tmp.right())
{
if(!hash[t=tmp.hashval()])
{
hash[t]=true;
Q.push(tmp);
if(tmp==A)
{
//find ans…
output(tmp);
goto AA;
}
}
}
}
AA:flag=true;
if(!flag)
puts(“unsolvable”);
}
return 0;
}
求三階矩陣A的逆矩陣C語言算法程序
#includestdio.h
#includemath.h
#define n 3 //三階矩陣
#define N 20
#define err 0.0001
void main()
{
int i,j,k;
double A[n][n],X[n],u,y[n],max;
printf(“Please input the matrix:\n”);
for(i=0;in;i++)
for(j=0;jn;j++)
scanf(“%lf”,A[i][j]); //輸入矩陣
printf(“Please input the initialized vector:\n”);
for(i=0;in;i++)
scanf(“%lf”,X[i]); //輸入初始向量
k=1;
u=0;
while(1)
{
max=X[0];
for(i=0;in;i++)
{
if(maxX[i]) max=X[i]; //選擇最大值
}
for(i=0;in;i++)
y[i]=X[i]/max;
for(i=0;in;i++)
{
X[i]=0;
for(j=0;jn;j++)
X[i]+=A[i][j]*y[j]; //矩陣相乘
}
if(fabs(max-u)err)
{
printf(“The eignvalue of A is:%f\n”,max);
printf(“The eignvector of A is:”);
for(i=0;in;i++)
printf(“%f “,X[i]);
break;
}
else
{
if(kN)
else
}
}
}
C語言a=a++的運算順序是怎麼樣的?
這兩個程序的輸出結果是相同的:
因為它們的操作都是:先取變量a的值,取完後a自增,最後取前面取到的值賦值給賦值號左邊的變量(所以最後輸出變量的值就都是1)。
用C語言怎麼編一個a^n(a的n次方)的算法?
如果n比較小,可以吧
result
*=
a循環n次。。
如果n比較大,
可以逐步來算。
這樣考慮,f(n)
=
2^n
如果有了
f(m)的結果,
那麼
f(2m)和f(2m+1)
就分別等於
f(m)*f(m)和f(m)*f(m)*a
所以可以從最高位開始查看n的每一位,
如果這一位是1,
那麼
result
=
result
*
result
*
a;
如果這一位是0,那麼result
=
result
*
result;
其中result
的初始值是1。
這樣複雜度就是log(n)的
C語言算法a(n)=2^n+1過程怎麼寫!急!
以下程序以n=0的整數為標準。
#include stdio.h
int main(){
int a=1,i=0;
int n;
printf(“請輸入n的值”);
scnaf(“%d”,n);
for (i=0;in;i++)
{a=a*2;}
printf(“a=%d”,a);
return 0;}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/199898.html