全排列的深度優先演算法:
#include using namespace std;int a[10],book[10],n;
void dfs(int step)
123+456=789的問題深度優先演算法:
#include using namespace std;int a[10],book[10],total=0;;
void dfs(int step)
,,,};
int tx,ty,k;
if(x==p&&y==q) }
return;
}int main()
;int main()
; int book[51][51]=;
int next[4][2]=,,,};
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(headn||ty<1||ty>m)
continue;
if(a[tx][ty]==0&&book[tx][ty]==0)
if(tx==p&&ty==q)
}if(flag==1)
break;
head++;
} cout《海島問題深度優先演算法:
#include using namespace std;
int a[51][51], book[51][51],n,m,sum;
void dfs(int x,int y)
,,,};
for(k=0;k<=3;k++) }
return;
}int main()
;int main()
; int book[51][51]=;
int next[4][2]=,,,};
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(headn||ty<1||ty>m)
continue;
if(a[tx][ty]>0&&book[tx][ty]==0)
} head++;
} cout《判斷有幾個連續島嶼:
#include #include using namespace std;
int a[51][51], book[51][51],n,m,sum,area=0;
void dfs(int x,int y,int color)
,,,};
a[x][y]=color;
for(k=0;k<=3;k++) }
return;
}int main()
x = i; y = j;
while(a[x][y] != '#')
x = i; y = j;
while(a[x][y] != '#')
x = i; y = j;
while(a[x][y] != '#')
return sum;
}void dfs(int x, int y)
, , , };
int k, sum, tx, ty;
sum = getnum(x, y);
if(sum > maxi)
for(k=0; k<4; k++)
return;
}int main()
book[startx][starty] = 1;
maxi = getnum(startx, starty);
mx = startx;
my = starty;
dfs(startx, starty);
cout<<"將炸彈放置在("《炸彈人廣度優先:
#include using namespace std;
struct note
;char a[20][20];
int getnum(int i, int j)
x = i; y = j;
while(a[x][y] != '#')
x = i; y = j;
while(a[x][y] != '#')
x = i; y = j;
while(a[x][y] != '#')
return sum;
}int main()
; int i, k, tx, ty, startx, starty, sum, max=0, mx, my, m, n;
int next[4][2] = , , , };
cin>>n>>m>>startx>>starty;
for(i=0; i>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)}}
head++;
}cout<<"將炸彈放置在("
system("pause");
return 0;
}
演算法 深度優先和廣度優先
深度優先搜尋屬於圖演算法的一種,是乙個針對圖和樹的遍歷演算法,英文縮寫為dfs depth first search 深度優先搜尋利用深度優先搜尋演算法可以產生目標圖的相應拓撲排序表,利用拓撲排序表可以方便的解決很多相關的圖論問題,如最大路徑問題等等。一般用堆資料結構來輔助實現dfs演算法。文字描述...
深度優先演算法和廣度優先演算法
圖形的深度優先搜尋法 void dfs int current 主程式 建立圖形後,將遍歷內容印出.void main 邊線陣列 int i for i 1 i 8 i creategraph node,20 建立圖形 printf 圖形的鄰接鍊錶內容 n for i 1 i 8 i printf ...
演算法 廣度優先演算法和深度優先演算法
廣度 bfs 和深度 dfs 優先演算法這倆個演算法是圖論裡面非常重要的兩個遍歷的方法。下面乙個例子迷宮計算,如下圖 解釋 所謂廣度,就是一層一層的,向下遍歷,層層堵截,看下面這幅圖,我們如果要是廣度優先遍歷的話,我們的結果是v1 v2 v3 v4 v5 v6 v7 v8。廣度優先搜尋的思想 訪問頂...