C C++,算法實例
一、數論算法
1.求兩數的最大公約數
function gcd(a,b:integer):integer;begin if b=0 then gcd:=aelse gcd:=gcd (b,a mod b);end ;
2.求兩數的最小公倍數
function lcm(a,b:integer):integer;beginif a<b then swap(a,b);lcm:=a;while lcm mod b>0 do inc(lcm,a);end;
3.素數的求法
A.小范圍內判斷一個數是否為質數:
function prime (n: integer): Boolean;var I: integer;beginfor I:=2 to trunc(sqrt(n)) doif n mod I=0 then begin prime:=false; exit;end;prime:=true;end;
B.判斷longint范圍內的數是否為素數(包含求50000以內的素數表):
procedure getprime;var i,j:longint;p:array[1..50000] of boolean;beginfillchar(p,sizeof(p),true);p[1]:=false;i:=2;while i<50000 do beginif p[i] then beginj:=i*2;while j<50000 do beginp[j]:=false;inc(j,i);end;end;inc(i);end;l:=0;for i:=1 to 50000 doif p[i] then begininc(l);pr[l]:=i;end;end;{getprime}function prime(x:longint):integer;var i:integer;beginprime:=false;for i:=1 to l doif pr[i]>=x then breakelse if x mod pr[i]=0 then exit;prime:=true;end;{prime}
二、圖論算法
1.最小生成樹
A.Prim算法:
procedure prim(v0:integer);varlowcost,closest:array[1..maxn] of integer;i,j,k,min:integer;beginfor i:=1 to n do beginlowcost[i]:=cost[v0,i];closest[i]:=v0;end;for i:=1 to n-1 do begin{尋找離生成樹最近的未加入頂點k}min:=maxlongint;for j:=1 to n doif (lowcost[j]<min) and (lowcost[j]<>0) then beginmin:=lowcost[j];k:=j;end;lowcost[k]:=0; {將頂點k加入生成樹}{生成樹中增加一條新的邊k到closest[k]}{修正各點的lowcost和closest值}for j:=1 to n doif cost[k,j]<lwocost[j] then beginlowcost[j]:=cost[k,j];closest[j]:=k;end;end;end;{prim}
B.Kruskal算法:(貪心)
按權值遞增順序刪去圖中的邊,若不形成回路則將此邊加入最小生成樹。
function find(v:integer):integer; {返回頂點v所在的集合}var i:integer;begini:=1;while (i<=n) and (not v in vset[i]) do inc(i);if i<=n then find:=i else find:=0;end; procedure kruskal;vartot,i,j:integer;beginfor i:=1 to n do vset[i]:=[i];{初始化定義n個集合,第I個集合包含一個元素I}p:=n-1; q:=1; tot:=0; {p為尚待加入的邊數,q為邊集指針}sort;{對所有邊按權值遞增排序,存于e[I]中,e[I].v1與e[I].v2為邊I所連接的兩個頂點的序號,e[I].len為第I條邊的長度}while p>0 do begini:=find(e[q].v1);j:=find(e[q].v2);if i<>j then begininc(tot,e[q].len);vset[i]:=vset[i]+vset[j];vset[j]:=[];dec(p);end;inc(q);end;writeln(tot);end;
2.最短路徑
A.標號法求解單源點最短路徑:
vara:array[1..maxn,1..maxn] of integer;b:array[1..maxn] of integer; {b[i]指頂點i到源點的最短路徑}mark:array[1..maxn] of boolean; procedure bhf;varbest,best_j:integer;beginfillchar(mark,sizeof(mark),false);mark[1]:=true; b[1]:=0;{1為源點}repeatbest:=0;for i:=1 to n doIf mark[i] then {對每一個已計算出最短路徑的點}for j:=1 to n doif (not mark[j]) and (a[i,j]>0) then if (best=0) or (b[i]+a[i,j]<best) then beginbest:=b[i]+a[i,j]; best_j:=j;end;if best>0 then beginb[best_j]:=best;mark[best_j]:=true;end;until best=0;end;{bhf}
B.Floyed算法求解所有頂點對之間的最短路徑:
procedure floyed;beginfor I:=1 to n dofor j:=1 to n doif a[I,j]>0 then p[I,j]:=I else p[I,j]:=0; {p[I,j]表示I到j的最短路徑上j的前驅結點}for k:=1 to n do {枚舉中間結點}for i:=1 to n dofor j:=1 to n doif a[i,k]+a[j,k]<a[i,j] then begina[i,j]:=a[i,k]+a[k,j];p[I,j]:=p[k,j];end;end;
C. Dijkstra 算法:
vara:array[1..maxn,1..maxn] of integer;b,pre:array[1..maxn] of integer; {pre[i]指最短路徑上I的前驅結點}mark:array[1..maxn] of boolean;procedure dijkstra(v0:integer);beginfillchar(mark,sizeof(mark),false);for i:=1 to n do begind[i]:=a[v0,i];if d[i]<>0 then pre[i]:=v0 else pre[i]:=0;end;mark[v0]:=true;repeat {每循環一次加入一個離1集合最近的結點并調整其他結點的參數}min:=maxint; u:=0; {u記錄離1集合最近的結點}for i:=1 to n doif (not mark[i]) and (d[i]<min) then beginu:=i; min:=d[i];end;if u<>0 then beginmark[u]:=true; for i:=1 to n doif (not mark[i]) and (a[u,i]+d[u]<d[i]) then begind[i]:=a[u,i]+d[u];pre[i]:=u;end;end;until u=0;end;
3.計算圖的傳遞閉包
Procedure Longlink;VarT:array[1..maxn,1..maxn] of boolean;BeginFillchar(t,sizeof(t),false);For k:=1 to n doFor I:=1 to n doFor j:=1 to n do T[I,j]:=t[I,j] or (t[I,k] and t[k,j]);End;
4.無向圖的連通分量
A.深度優先
procedure dfs ( now,color: integer);beginfor i:=1 to n doif a[now,i] and c[i]=0 then begin {對結點I染色}c[i]:=color;dfs(I,color);end;end;
B 寬度優先(種子染色法)
5.關鍵路徑
幾個定義: 頂點1為源點,n為匯點。
a. 頂點事件最早發生時間Ve[j], Ve [j] = max{ Ve [j] + w[I,j] },其中Ve (1) = 0;
b. 頂點事件最晚發生時間 Vl[j], Vl [j] = min{ Vl[j] – w[I,j] },其中 Vl(n) = Ve(n);
c. 邊活動最早開始時間 Ee[I], 若邊I由<j,k>表示,則Ee[I] = Ve[j];
d. 邊活動最晚開始時間 El[I], 若邊I由<j,k>表示,則El[I] = Vl[k] – w[j,k];
若 Ee[j] = El[j] ,則活動j為關鍵活動,由關鍵活動組成的路徑為關鍵路徑。
求解方法:
a. 從源點起topsort,判斷是否有回路并計算Ve;
b. 從匯點起topsort,求Vl;
c. 算Ee 和 El;
6.拓撲排序
找入度為0的點,刪去與其相連的所有邊,不斷重復這一過程。
例 尋找一數列,其中任意連續p項之和為正,任意q 項之和為負,若不存在則輸出NO.
7.回路問題
Euler回路(DFS)
定義:經過圖的每條邊僅一次的回路。(充要條件:圖連同且無奇點)
Hamilton回路
定義:經過圖的每個頂點僅一次的回路。
一筆畫
充要條件:圖連通且奇點個數為0個或2個。
9.判斷圖中是否有負權回路 Bellman-ford 算法
x[I],y[I],t[I]分別表示第I條邊的起點,終點和權。共n個結點和m條邊。procedure bellman-fordbeginfor I:=0 to n-1 do d[I]:=+infinitive;d[0]:=0;for I:=1 to n-1 dofor j:=1 to m do {枚舉每一條邊}if d[x[j]]+t[j]<d[y[j]] then d[y[j]]:=d[x[j]]+t[j];for I:=1 to m doif d[x[j]]+t[j]<d[y[j]] then return false else return true;end;
10.第n最短路徑問題
*第二最短路徑:每舉最短路徑上的每條邊,每次刪除一條,然后求新圖的最短路徑,取這些路徑中最短的一條即為第二最短路徑。
*同理,第n最短路徑可在求解第n-1最短路徑的基礎上求解。
三、背包問題
*部分背包問題可有貪心法求解:計算Pi/Wi
數據結構:
w[i]:第i個背包的重量;
p[i]:第i個背包的價值;
1.0-1背包: 每個背包只能使用一次或有限次(可轉化為一次):
A.求最多可放入的重量。
NOIP2001 裝箱問題
有一個箱子容量為v(正整數,o≤v≤20000),同時有n個物品(o≤n≤30),每個物品有一個體積 (正整數)。要求從 n 個物品中,任取若千個裝入箱內,使箱子的剩余空間為最小。
l 搜索方法
procedure search(k,v:integer); {搜索第k個物品,剩余空間為v}var i,j:integer;beginif v<best then best:=v;if v-(s[n]-s[k-1])>=best then exit; {s[n]為前n個物品的重量和}if k<=n then beginif v>w[k] then search(k+1,v-w[k]);search(k+1,v);end;end; l DPF[I,j]為前i個物品中選擇若干個放入使其體積正好為j的標志,為布爾型。實現:將最優化問題轉化為判定性問題f [I, j] = f [ i-1, j-w[i] ] (w[I]<=j<=v) 邊界:f[0,0]:=true.For I:=1 to n doFor j:=w[I] to v do F[I,j]:=f[I-1,j-w[I]];優化:當前狀態只與前一階段狀態有關,可降至一維。F[0]:=true;For I:=1 to n do beginF1:=f;For j:=w[I] to v doIf f[j-w[I]] then f1[j]:=true;F:=f1;End;
B.求可以放入的最大價值。
F[I,j] 為容量為I時取前j個背包所能獲得的最大價值。
F [i,j] = max { f [ i – w [ j ], j-1] + p [ j ], f[ i,j-1] }
C.求恰好裝滿的情況數。
DP:
Procedure update;var j,k:integer;beginc:=a;for j:=0 to n doif a[j]>0 thenif j+now<=n then inc(c[j+now],a[j]);a:=c;end;
2.可重復背包
A求最多可放入的重量。
F[I,j]為前i個物品中選擇若干個放入使其體積正好為j的標志,為布爾型。
狀態轉移方程為
f[I,j] = f [ I-1, j – w[I]*k ] (k=1.. j div w[I])
B.求可以放入的最大價值。
USACO 1.2 Score Inflation
進行一次競賽,總時間T固定,有若干種可選擇的題目,每種題目可選入的數量不限,每種題目有一個ti(解答此題所需的時間)和一個si(解答此題所得的分數),現要選擇若干題目,使解這些題的總時間在T以內的前提下,所得的總分最大,求最大的得分。
*易想到:
f[i,j] = max { f [i- k*w[j], j-1] + k*p[j] } (0<=k<= i div w[j])
其中f[i,j]表示容量為i時取前j種背包所能達到的最大值。
*實現:
BeginFillChar(f,SizeOf(f),0);For i:=1 To M DoFor j:=1 To N DoIf i-problem[j].time>=0 ThenBegint:=problem[j].point+f[i-problem[j].time];If t>f[i] Then f[i]:=t;End;Writeln(f[M]);End.
C.求恰好裝滿的情況數。
Ahoi2001 Problem2
求自然數n本質不同的質數和的表達式的數目。
思路一,生成每個質數的系數的排列,在一一測試,這是通法。
procedure try(dep:integer);var i,j:integer;begincal; {此過程計算當前系數的計算結果,now為結果}if now>n then exit; {剪枝}if dep=l+1 then begin {生成所有系數}cal;if now=n then inc(tot);exit;end;for i:=0 to n div pr[dep] do beginxs[dep]:=i;try(dep+1);xs[dep]:=0;end;end;
思路二,遞歸搜索效率較高
procedure try(dep,rest:integer);var i,j,x:integer;beginif (rest<=0) or (dep=l+1) then beginif rest=0 then inc(tot);exit;end;for i:=0 to rest div pr[dep] dotry(dep+1,rest-pr[dep]*i);end;{main: try(1,n); }
思路三:可使用動態規劃求解
USACO1.2 money system
V個物品,背包容量為n,求放法總數。
轉移方程:
Procedure update;var j,k:integer;beginc:=a;for j:=0 to n doif a[j]>0 thenfor k:=1 to n div now doif j+now*k<=n then inc(c[j+now*k],a[j]);a:=c;end;{main}begin read(now); {讀入第一個物品的重量}i:=0; {a[i]為背包容量為i時的放法總數}while i<=n do begin a[i]:=1; inc(i,now); end; {定義第一個物品重的整數倍的重量a值為1,作為初值}for i:=2 to v dobeginread(now);update; {動態更新}end;writeln(a[n]);
四、排序算法
A.快速排序:
procedure qsort(l,r:integer);var i,j,mid:integer;begini:=l;j:=r; mid:=a[(l+r) div 2]; {將當前序列在中間位置的數定義為中間數}repeatwhile a[i]<mid do inc(i); {在左半部分尋找比中間數大的數}while a[j]>mid do dec(j);{在右半部分尋找比中間數小的數}if i<=j then begin {若找到一組與排序目標不一致的數對則交換它們}swap(a[i],a[j]);inc(i);dec(j); {繼續找}end;until i>j;if l<j then qsort(l,j); {若未到兩個數的邊界,則遞歸搜索左右區間}if i<r then qsort(i,r);end;{sort}
B.插入排序:
思路:當前a[1]..a[i-1]已排好序了,現要插入a[i]使a[1]..a[i]有序。
procedure insert_sort;var i,j:integer;beginfor i:=2 to n do begina[0]:=a[i];j:=i-1;while a[0]<a[j] do begina[j+1]:=a[j];j:=j-1;end;a[j+1]:=a[0];end;end;{inset_sort}
C.選擇排序:
procedure sort;var i,j,k:integer;beginfor i:=1 to n-1 do for j:=i+1 to n doif a[i]>a[j] then swap(a[i],a[j]);end;
D. 冒泡排序
procedure bubble_sort;var i,j,k:integer;beginfor i:=1 to n-1 dofor j:=n downto i+1 doif a[j]<a[j-1] then swap( a[j],a[j-1]); {每次比較相鄰元素的關系}end;
E.堆排序:
procedure sift(i,m:integer);{調整以i為根的子樹成為堆,m為結點總數}var k:integer;begina[0]:=a[i]; k:=2*i;{在完全二叉樹中結點i的左孩子為2*i,右孩子為2*i+1}while k<=m do beginif (k<m) and (a[k]<a[k+1]) then inc(k);{找出a[k]與a[k+1]中較大值}if a[0]<a[k] then begin a[i]:=a[k];i:=k;k:=2*i; endelse k:=m+1;end;a[i]:=a[0]; {將根放在合適的位置}end; procedure heapsort;varj:integer;beginfor j:=n div 2 downto 1 do sift(j,n);for j:=n downto 2 do beginswap(a[1],a[j]);sift(1,j-1);end;end;
F. 歸并排序
{a為序列表,tmp為輔助數組}
procedure merge(var a:listtype; p,q,r:integer);
{將已排序好的子序列a[p..q]與a[q+1..r]合并為有序的tmp[p..r]}
var I,j,t:integer;tmp:listtype;begint:=p;i:=p;j:=q+1;{t為tmp指針,I,j分別為左右子序列的指針}while (t<=r) do beginif (i<=q){左序列有剩余} and ((j>r) or (a[i]<=a[j])) {滿足取左邊序列當前元素的要求}then begintmp[t]:=a[i]; inc(i);endelse begintmp[t]:=a[j];inc(j);end;inc(t);end;for i:=p to r do a[i]:=tmp[i];end;{merge} procedure merge_sort(var a:listtype; p,r: integer); {合并排序a[p..r]}var q:integer;beginif p<>r then beginq:=(p+r-1) div 2;merge_sort (a,p,q);merge_sort (a,q+1,r);merge (a,p,q,r);end;end;{main}beginmerge_sort(a,1,n);end.
G.基數排序
思想:對每個元素按從低位到高位對每一位進行一次排序
五、高精度計算
高精度數的定義:
type
hp=array[1..maxlen] of integer;
1.高精度加法
procedure plus ( a,b:hp; var c:hp);var i,len:integer;beginfillchar(c,sizeof(c),0);if a[0]>b[0] then len:=a[0] else len:=b[0];for i:=1 to len do begininc(c[i],a[i]+b[i]);if c[i]>10 then begin dec(c[i],10); inc(c[i+1]); end; {進位}end;if c[len+1]>0 then inc(len);c[0]:=len;end;{plus}
2.高精度減法
procedure substract(a,b:hp;var c:hp); var i,len:integer;beginfillchar(c,sizeof(c),0);if a[0]>b[0] then len:=a[0] else len:=b[0];for i:=1 to len do begininc(c[i],a[i]-b[i]);if c[i]<0 then begin inc(c[i],10);dec(c[i+1]); end;while (len>1) and (c[len]=0) do dec(len);c[0]:=len;end;
3.高精度乘以低精度
procedure multiply(a:hp;b:longint;var c:hp);var i,len:integer;beginfillchar(c,sizeof(c),0);len:=a[0];for i:=1 to len do begininc(c[i],a[i]*b);inc(c[i+1],(a[i]*b) div 10);c[i]:=c[i] mod 10;end;inc(len);while (c[len]>=10) do begin {處理最高位的進位}c[len+1]:=c[len] div 10;c[len]:=c[len] mod 10;inc(len);end;while (len>1) and (c[len]=0) do dec(len); {若不需進位則調整len}c[0]:=len;end;{multiply}
4.高精度乘以高精度
procedure high_multiply(a,b:hp; var c:hp}var i,j,len:integer;beginfillchar(c,sizeof(c),0);for i:=1 to a[0] dofor j:=1 to b[0] do begininc(c[i+j-1],a[i]*b[j]);inc(c[i+j],c[i+j-1] div 10);c[i+j-1]:=c[i+j-1] mod 10;end;len:=a[0]+b[0]+1;while (len>1) and (c[len]=0) do dec(len);c[0]:=len;end;
5.高精度除以低精度
procedure devide(a:hp;b:longint; var c:hp; var d:longint);{c:=a div b; d:= a mod b}var i,len:integer;beginfillchar(c,sizeof(c),0);len:=a[0]; d:=0;for i:=len downto 1 do begind:=d*10+a[i];c[i]:=d div b;d:=d mod b;end;while (len>1) and (c[len]=0) then dec(len);c[0]:=len;end;
6.高精度除以高精度
procedure high_devide(a,b:hp; var c,d:hp);vari,len:integer;beginfillchar(c,sizeof(c),0);fillchar(d,sizeof(d),0);len:=a[0];d[0]:=1;for i:=len downto 1 do beginmultiply(d,10,d);d[1]:=a[i];while(compare(d,b)>=0) do {即d>=b}beginSubtract(d,b,d);inc(c[i]);end;end;while(len>1)and(c.s[len]=0) do dec(len);c.len:=len;end;
六、 樹的遍歷
1.已知前序中序求后序
procedure Solve(pre,mid:string);var i:integer;beginif (pre='''') or (mid='''') then exit;i:=pos(pre[1],mid);solve(copy(pre,2,i),copy(mid,1,i-1));solve(copy(pre,i+1,length(pre)-i),copy(mid,i+1,length(mid)-i));post:=post+pre[1]; {加上根,遞歸結束后post即為后序遍歷}end;
2.已知中序后序求前序
procedure Solve(mid,post:string);var i:integer;beginif (mid='''') or (post='''') then exit;i:=pos(post[length(post)],mid);pre:=pre+post[length(post)]; {加上根,遞歸結束后pre即為前序遍歷}solve(copy(mid,1,I-1),copy(post,1,I-1));solve(copy(mid,I+1,length(mid)-I),copy(post,I,length(post)-i));end;
3.已知前序后序求中序的一種
function ok(s1,s2:string):boolean;var i,l:integer; p:boolean;beginok:=true;l:=length(s1);for i:=1 to l do beginp:=false;for j:=1 to l doif s1[i]=s2[j] then p:=true;if not p then begin ok:=false;exit;end;end;end; procedure solve(pre,post:string);var i:integer;beginif (pre='''') or (post='''') then exit;i:=0;repeatinc(i);until ok(copy(pre,2,i),copy(post,1,i));solve(copy(pre,2,i),copy(post,1,i));midstr:=midstr+pre[1];solve(copy(pre,i+2,length(pre)-i-1),copy(post,i+1,length(post)-i-1));end;
七 進制轉換
1.任意正整數進制間的互化
除n取余
2.實數任意正整數進制間的互化
乘n取整
3.負數進制:
設計一個程序,讀入一個十進制數的基數和一個負進制數的基數,并將此十進制數轉換為此負 進制下的數:-R∈{-2,-3,-4,....-20}
八 全排列與組合的生成
1.排列的生成:(1..n)
procedure solve(dep:integer);vari:integer;beginif dep=n+1 then begin writeln(s);exit; end;for i:=1 to n doif not used[i] then begins:=s+chr(i+ord(''0''));used[i]:=true;solve(dep+1);s:=copy(s,1,length(s)-1); used[i]:=false;end;end;
2.組合的生成(1..n中選取k個數的所有方案)
procedure solve(dep,pre:integer);vari:integer;beginif dep=k+1 then begin writeln(s);exit; end;for i:=1 to n doif (not used[i]) and (i>pre) then begins:=s+chr(i+ord(''0''));used[i]:=true;solve(dep+1,i);s:=copy(s,1,length(s)-1); used[i]:=false;end;end;
九.查找算法
1.折半查找
function binsearch(k:keytype):integer;var low,hig,mid:integer;beginlow:=1;hig:=n;mid:=(low+hig) div 2;while (a[mid].key<>k) and (low<=hig) do beginif a[mid].key>k then hig:=mid-1else low:=mid+1;mid:=(low+hig) div 2;end;if low>hig then mid:=0;binsearch:=mid;end;
2.樹形查找
二叉排序樹:每個結點的值都大于其左子樹任一結點的值而小于其右子樹任一結點的值。
查找
function treesrh(k:keytype):pointer;var q:pointer;beginq:=root;while (q<>nil) and (q^.key<>k) doif k<q^.key then q:=q^.leftelse q:=q^.right;treesrh:=q;end;
十、貪心
*會議問題
(1) n個活動每個活動有一個開始時間和一個結束時間,任一時刻僅一項活動進行,求滿足活動數最多的情況。
解:按每項活動的結束時間進行排序,排在前面的優先滿足。
(2)會議室空閑時間最少。
(3)每個客戶有一個愿付的租金,求最大利潤。
(4)共R間會議室,第i個客戶需使用i間會議室,費用相同,求最大利潤。
十一、回溯法框架
1. n皇后問題
procedure try(i:byte);var j:byte;beginif i=n+1 then begin print;exit;end;for j:=1 to n doif a[i] and b[j+i] and c[j-i] then beginx[i]:=j;a[j]:=false; b[j+i]:=false; c[j-i]:=false;try(i+1);a[j]:=true; b[i+j]:=true; c[j-i]:=true;end;end;
2.Hanoi Tower 漢諾塔
h(n)=2*h(n-1)+1 h(1)=1初始所有銅片都在a柱上procedure hanoi(n,a,b,c:byte); {將第n塊銅片從a柱通過b柱移到c柱上}beginif n=0 then exit;hanoi(n-1,a,c,b); {將上面的n-1塊從a柱通過c柱移到b柱上}write(n,'moved from',a,'to',c);hanoi(n-1,b,a,c);{ 將b上的n-1塊從b柱通過a柱移到c柱上end;
初始銅片分布在3個柱上,給定目標柱goal
h[1..3,0..n]存放三個柱的狀態,now與nowp存最大的不到位的銅片的柱號和編號,h[I,0]存第I個柱上的個數。
Procedure move(k,goal:integer); {將最大不到位的k移到目標柱goal上}BeginIf k=0 then exit;For I:=1 to 3 doFor j:=1 to han[I,0] doIf h[I,j]=k then begin now:=I;nowp:=j; end; {找到k的位置}If now<>goal then begin {若未移到目標}Move(k-1,6-now-goal); {剩下的先移到沒用的柱上}Writeln(k moved from now to goal);H[goal,h[goal,0]+1]:=h[now,nowp]; h[now,nowp]:=0;Inc(h[goal,0]); dec(h[now,0]);Move(k-1,goal); {剩下的移到目標上}End;
十二、DFS框架
NOIP2001 數的劃分
procedure work(dep,pre,s:longint); {入口為work(1,1,n)}{dep為當前試放的第dep個數,pre為前一次試放的數,s為當前剩余可分的總數}var j:longint;begin if dep=n then begin if s>=pre then inc(r); exit; end; for j:=pre to s div 2 do work(dep+1,j,s-j); end;類似:procedure try(dep:integer);var i:integer;beginif dep=k then beginif tot>=a[dep-1] then inc(sum);exit; end;for i:=a[dep-1] to tot div 2 do begina[dep]:=i; dec(tot,i); try(dep+1);inc(tot,i);end;end;{try}
十三、BFS框架
IOI94 房間問題
head:=1; tail:=0;while tail<head do begininc(tail);for k:=1 to n doif k方向可擴展 then begininc(head);list[head].x:=list[tail].x+dx[k]; {擴展出新結點list[head]}list[head].y:=list[tail].y+dy[k];處理新結點list[head];end;end;
十五、數據結構相關算法
1.鏈表的定位函數
loc(I:integer):pointer; {尋找鏈表中的第I個結點的指針}procedure loc(L:linklist; I:integer):pointer;var p:pointer;j:integer;beginp:=L.head; j:=0;if (I>=1) and (I<=L.len) then while j<I do begin p:=p^.next; inc(j); end;loc:=p;end;
2.單鏈表的插入操作
procedure insert(L:linklist; I:integer; x:datatype);var p,q:pointer;beginp:=loc(L,I);new(q);q^.data:=x;q^.next:=p^.next;p^.next:=q;inc(L.len);end;
3.單鏈表的刪除操作
procedure delete(L:linklist; I:integer);var p,q:pointer;beginp:=loc(L,I-1);q:=p^.next;p^.next:=q^.next;dispose(q);dec(L.len);end;
4.雙鏈表的插入操作(插入新結點q)
p:=loc(L,I);new(q);q^.data:=x;q^.pre:=p;q^.next:=p^.next;p^.next:=q;q^.next^.pre:=q;
5.雙鏈表的刪除操作
p:=loc(L,I); {p為要刪除的結點}p^.pre^.next:=p^.next;p^.next^.pre:=p^.pre;dispose(p);
新聞熱點
疑難解答