全排列的深度优先算法:
#include <iostream> using namespace std; int a[10],book[10],n; void dfs(int step) { int i; if(step==n+1) { for(i=1;i<=n;i++) cout<<a[i]; cout<<endl; return; } for(i=1;i<=n;i++) { if(book[i]==0) { a[step]=i; book[i]=1; dfs(step+1); book[i]=0; } } return; } int main() { cin>>n; dfs(1); system("pause"); return 0; }
123+456=789的问题深度优先算法:
#include <iostream> using namespace std; int a[10],book[10],total=0;; void dfs(int step) { int i; if(step==10) { if(a[1]*100+a[2]*10+a[3]+a[4]*100+a[5]*10+a[6]==a[7]*100+a[8]*10+a[9]) { total++; cout<<a[1]<<a[2]<<a[3]<<"+"<<a[4]<<a[5]<<a[6]<<"="<<a[7]<<a[8]<<a[9]<<endl; } return; } for(i=1;i<=9;i++) { if(book[i]==0) { a[step]=i; book[i]=1; dfs(step+1); book[i]=0; } } return; } int main() { dfs(1); cout<<total/2<<endl; system("pause"); return 0; }
迷宫问题的深度优先算法:
#include <iostream> using namespace std; int n,m,p,q, mini=99999999; int a[51][51], book[51][51]; void dfs(int x,int y, int step) { int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; int tx,ty,k; if(x==p&&y==q) { if(step<mini) mini=step; return; } for(k=0;k<=3;k++) { tx=x+next[k][0]; ty=y+next[k][1]; if(tx<1||tx>n||ty<1||ty>m) continue; if(a[tx][ty]==0&&book[tx][ty]==0) { book[tx][ty]=1; dfs(tx,ty,step+1); book[tx][ty]=0; } } return; } int main() { FILE *stream; freopen_s(&stream,"sample_input.txt", "r", stdin); int i,j,startx,starty; cin>>n>>m; for(i=1;i<=n;i++) for(j=1;j<=m;j++) cin>>a[i][j]; cin>>startx>>starty>>p>>q; book[startx][starty]=1; dfs(startx,starty,0); cout<<mini; fclose(stdin); system("pause"); return 0; }
迷宫问题的广度优先算法:
#include <iostream> using namespace std; struct node { int x; int y; int f; //f父亲在队列中的编号, 输出路径用 int step; }; int main() { struct node que[2501]; int a[51][51]={0}; int book[51][51]={0}; int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; int head,tail; int i,j,k,n,m,startx,starty,p,q,tx,ty,flag; cout<<"Please input the bianjie:"; cin>>n>>m; cout<<"Please input the array:"; for(i=1;i<=n;i++) for(j=1;j<=m;j++) cin>>a[i][j]; cout<<"please input the startx, starty, p,q:"; cin>>startx>>starty>>p>>q; //队列初始化 head=1; tail=1; //往队列插入迷宫入口坐标 que[tail].x=startx; que[tail].y=starty; que[tail].f=0; que[tail].step=0; tail++; book[startx][starty]=1; flag=0; while(head<tail) { for(k=0;k<=3;k++) { tx=que[head].x+next[k][0]; ty=que[head].y+next[k][1]; if(tx<1||tx>n||ty<1||ty>m) continue; if(a[tx][ty]==0&&book[tx][ty]==0) { book[tx][ty]=1; que[tail].x=tx; que[tail].y=ty; que[tail].f=head; que[tail].step=que[head].step+1; tail++; } if(tx==p&&ty==q) { flag=1; break; } } if(flag==1) break; head++; } cout<<que[tail-1].step<<endl; system("pause"); return 0; }
海岛问题深度优先算法:
#include <iostream> using namespace std; int a[51][51], book[51][51],n,m,sum; void dfs(int x,int y) { int k, tx,ty, next[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; for(k=0;k<=3;k++) { tx=x+next[k][0]; ty=y+next[k][1]; if(tx<1||tx>n||ty<1||ty>m) continue; if(a[tx][ty]>0&&book[tx][ty]==0) { sum++; book[tx][ty]=1; dfs(tx,ty); } } return; } int main() { FILE *stream; freopen_s(&stream,"sample_input.txt", "r", stdin); //freopen_s(&stream,"sample_output.txt", "w", stdout); int i,j,startx,starty; cin>>n>>m>>startx>>starty; for(i=1;i<=n;i++) for(j=1;j<=m;j++) cin>>a[i][j]; book[startx][starty]=1; sum=1; dfs(startx,starty); cout<<sum<<endl; fclose(stdin); //fclose(stdout); system("pause"); return 0; }
海岛广度优先算法:
#include <iostream> using namespace std; struct node { int x; int y; }; int main() { struct node que[2501]; int a[51][51]={0}; int book[51][51]={0}; int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; int head,tail; int i,j,k,n,m,startx,starty,p,q,tx,ty,sum,max=0; cout<<"Please input n,m,startx, starty:"; cin>>n>>m>>startx>>starty; cout<<"Please input the array:"; for(i=1;i<=n;i++) for(j=1;j<=m;j++) cin>>a[i][j]; //队列初始化 head=1; tail=1; //往队列插入降落起始坐标 que[tail].x=startx; que[tail].y=starty; tail++; book[startx][starty]=1; sum=1; while(head<tail) { for(k=0;k<=3;k++) { tx=que[head].x+next[k][0]; ty=que[head].y+next[k][1]; if(tx<1||tx>n||ty<1||ty>m) continue; if(a[tx][ty]>0&&book[tx][ty]==0) { sum++; book[tx][ty]=1; que[tail].x=tx; que[tail].y=ty; tail++; } } head++; } cout<<sum<<endl; system("pause"); return 0; }
判断有几个连续岛屿:
#include <iostream> #include <iomanip> using namespace std; int a[51][51], book[51][51],n,m,sum,area=0; void dfs(int x,int y,int color) { int k, tx,ty, next[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; a[x][y]=color; for(k=0;k<=3;k++) { tx=x+next[k][0]; ty=y+next[k][1]; if(tx<1||tx>n||ty<1||ty>m) continue; if(a[tx][ty]>0&&book[tx][ty]==0) { sum++; book[tx][ty]=1; dfs(tx,ty,color); book[tx][ty]=0; } } return; } int main() { FILE *stream; freopen_s(&stream,"sample_input.txt", "r", stdin); freopen_s(&stream,"sample_output.txt", "w", stdout); int i,j,num=0; cin>>n>>m; for(i=1;i<=n;i++) for(j=1;j<=m;j++) cin>>a[i][j]; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(a[i][j]>0) { sum=1; num--; book[i][j]=1; dfs(i,j,num); cout<<"每个岛屿的面积为:"<<sum<<endl; area+=sum; } } } for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { cout<<setw(3)<<a[i][j]<<" "; } cout<<endl; } cout<<"有"<<-num<<"个小岛"<<endl; cout<<"总面积为"<<area<<endl; fclose(stdin); fclose(stdout); //fclose(stdout); system("pause"); return 0; }
炸弹人深度优先:
#include <iostream> using namespace std; char a[20][20]; int book[20][20], maxi, mx, my, n, m; int getnum(int i, int j){ int x, y, sum=0; x = i; y = j; while(a[x][y] != '#') { if(a[x][y] == 'G') sum++; x--; } x = i; y = j; while(a[x][y] != '#') { if(a[x][y] == 'G') sum++; x++; } x = i; y = j; while(a[x][y] != '#') { if(a[x][y] == 'G') sum++; y--; } x = i; y = j; while(a[x][y] != '#') { if(a[x][y] == 'G') sum++; y++; } return sum; } void dfs(int x, int y) { int next[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; int k, sum, tx, ty; sum = getnum(x, y); if(sum > maxi) { maxi = sum; mx = x; my = y; } for(k=0; k<4; k++) { tx = x + next[k][0]; ty = y + next[k][1]; //判断边界 if(tx<0 || tx > n-1 || ty < 0 || ty > n-1) continue; //判断墙和是否走过 if(a[tx][ty] != '.' || book[tx][ty] != 0) continue; book[tx][ty] = 1; dfs(tx, ty); } return; } int main(){ FILE *stream; freopen_s(&stream,"sample_input.txt", "r", stdin); int i, startx, starty; cin>>n>>m>>startx>>starty; for(i=0; i<n; i++) { cin>>a[i]; } book[startx][starty] = 1; maxi = getnum(startx, starty); mx = startx; my = starty; dfs(startx, starty); cout<<"将炸弹放置在("<<mx<<","<<my<<")处, 可以消灭"<<maxi<<"个敌人"; fclose(stdin); system("pause"); return 0; }
炸弹人广度优先:
#include <iostream> using namespace std; struct Note { int x; int y; }; char a[20][20]; int getnum(int i, int j){ int x, y, sum=0; x = i; y = j; while(a[x][y] != '#') { if(a[x][y] == 'G') sum++; x--; } x = i; y = j; while(a[x][y] != '#') { if(a[x][y] == 'G') sum++; x++; } x = i; y = j; while(a[x][y] != '#') { if(a[x][y] == 'G') sum++; y--; } x = i; y = j; while(a[x][y] != '#') { if(a[x][y] == 'G') sum++; y++; } return sum; } int main() { FILE *stream; freopen_s(&stream,"sample_input.txt", "r", stdin); struct Note que[401]; int head=1, tail=1; int book[20][20] = {0}; int i, k, tx, ty, startx, starty, sum, max=0, mx, my, m, n; int next[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; cin>>n>>m>>startx>>starty; for(i=0; i<n; i++) { cin>>a[i]; } que[tail].x = startx; que[tail].y = starty; tail++; book[startx][starty] = 1; max = getnum(startx, starty); mx = startx; my = starty; while(head < tail) { for(k = 0; k< 4; k++) { tx = que[head].x + next[k][0]; ty = que[head].y + next[k][1]; if(tx<0 || tx>n-1 || ty<0 || ty>n-1) continue; if(a[tx][ty]=='.'&&book[tx][ty]==0) { book[tx][ty] = 1; que[tail].x = tx; que[tail].y = ty; tail++; sum = getnum(tx, ty); if(sum > max) { max = sum; mx = tx; my = ty; } } } head++; } cout<<"将炸弹放置在("<<mx<<","<<my<<")处, 可以消灭"<<max<<"个敌人"; fclose(stdin); system("pause"); return 0; }