a算法c語言,C和A的算法

本文目錄一覽:

求一個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-hk/n/199898.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-05 14:01
下一篇 2024-12-05 14:01

相關推薦

  • 蝴蝶優化算法Python版

    蝴蝶優化算法是一種基於仿生學的優化算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化算法Python版…

    編程 2025-04-29
  • Python實現爬樓梯算法

    本文介紹使用Python實現爬樓梯算法,該算法用於計算一個人爬n級樓梯有多少種不同的方法。 有一樓梯,小明可以一次走一步、兩步或三步。請問小明爬上第 n 級樓梯有多少種不同的爬樓梯…

    編程 2025-04-29
  • AES加密解密算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演着非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Harris角點檢測算法原理與實現

    本文將從多個方面對Harris角點檢測算法進行詳細的闡述,包括算法原理、實現步驟、代碼實現等。 一、Harris角點檢測算法原理 Harris角點檢測算法是一種經典的計算機視覺算法…

    編程 2025-04-29
  • 數據結構與算法基礎青島大學PPT解析

    本文將從多個方面對數據結構與算法基礎青島大學PPT進行詳細的闡述,包括數據類型、集合類型、排序算法、字符串匹配和動態規劃等內容。通過對這些內容的解析,讀者可以更好地了解數據結構與算…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • 瘦臉算法 Python 原理與實現

    本文將從多個方面詳細闡述瘦臉算法 Python 實現的原理和方法,包括該算法的意義、流程、代碼實現、優化等內容。 一、算法意義 隨着科技的發展,瘦臉算法已經成為了人們修圖中不可缺少…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29

發表回復

登錄後才能評論