專題整理 搜尋

2021-10-22 15:45:04 字數 3044 閱讀 3531

#include

#include

#include

#include

#include

#include

#define n 500050using namespace std;int maxn,minn;void read(int & p)

intmain()

printf

("%d "

,minn)

;printf

("%d"

,maxn)

;return0;

}

#include

#include

#include

#include

#include

#include

#define n 500050#define ll long longusing namespace std;void read(long long & p)

intmain()

else l=mid+1;

}printf

("%lld"

,ans)

;return0;

}

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define maxn 100010using namespace std;int n,k;double l[maxn];int l[maxn];

inline

void

read

(int

&p)while

(ch>=

'0'&&ch<=

'9') p=p*10+

(ch-

'0')

,ch=

getchar()

; p*

=f;}

inline

bool

judge

(int len)

intmain()

int l=

0,r=inf;

while

(l<=r)

printf

("%.2f"

,r/100.0);

return0;

}

inline

void

dfs(

int nlim,

int num,

int w,

int p)

dfs(nlim+

1,num+

1,w+b[nlim+1]

,p+a[nlim+1]

);//選擇下乙個

dfs(nlim+

1,num,w,p)

;//不選擇下乙個

}

3-2-3、思路&&概念引入:

什麼是bfs(廣度優先搜尋)?

我們知道dfs的特性:撞死南牆不回頭,一路搜到底然後再回溯。相比之下,bfs就要聰明一些,它是利用佇列儲存當前搜尋狀態以達到一層一層搜尋到結果的一種演算法。

最值得討論的是如何儲存搜尋狀態。一般來說可以使用結構體將搜尋到的東西儲存下來,也可以使用hash的方法將狀態進行儲存。

這題我們使用hash的方法對當前狀態進行儲存。

從初始的狀態開始用乙個二維的vis陣列儲存當前數字和游標停留的位置,然後進行bfs。

3-2-4、**:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define n 1000000using namespace std;int n,t,m;

struct nodeu,v;

queueq1;

bool vis[n][7

];//數字,游標 int sig[8]=;//快讀

inline

void

read

(int

&p)int

main()

u.gb=

1;u.dep=

0;q1.

push

(u);

while

(!q1.

empty()

)//交換左端

v=u;v.dep++

; numberposfor=v.num/sig[1]

; v.num=v.num-numberposfor*sig[1]

+numberposnow*sig[1]

-numberposnow*sig[v.gb]

+numberposfor*sig[v.gb];if

(v.num==mission)if(

!vis[v.num]

[v.gb])}

if(u.gb!=6)

//游標交換

v=u;v.dep++

; numberposfor1=v.num%10;

v.num=v.num-numberposnow*sig[v.gb]

+numberposfor1*sig[v.gb]

-numberposfor1+numberposnow;

if(v.num==mission)}if

(!vis[v.num]

[v.gb])if

(numberposnow!=0)

if(!vis[v.num]

[v.gb])}

if(numberposnow!=9)

if(!vis[v.num]

[v.gb])}

}return0;

}

專題整理 搜尋技術

bfs 題解型別 hdu1253勝利大逃亡 題解簡單bfs poj3278 catch that cow 題解簡單bfs poj 3126 prime path 題解簡單bfs hdu 2102 a計畫 題解簡單bfs codeforces 520b two buttons 題解bfs 剪枝 fzu...

0825 0903搜尋專題

poj1321 棋盤問題 dfs,雖然並不知道書上或者模板裡的dfs是什麼樣子,但是大概了解。vj掛了 在上面下回再補poj2251 dungeon master 這道題很苦逼的做了好幾天,交了幾十發,用兩種方式均返回tle。然後搜題解,說是要用bfs,啃書,發現原來自己一直不會bfs。寬度優先搜尋...

搜尋專題 S

1 簡單描述 數獨,在乙個9x9的方格中,需要把數字1 9填寫到空格當中,並且使方格的每一行和每一列中都包含1 9這九個數字。同時還要保證,空格中用粗線劃分成9個3x3的方格也同時包含1 9這九個數字。2 思路 include using namespace std struct node node...