kuangbin專題一 簡單搜尋 (完整)

2021-09-24 16:58:32 字數 4215 閱讀 3479

1. poj 1321 棋盤排列 類似八皇后

/*

poj 1321 棋盤排列

類似八皇后的排列搜尋。

*/#include #include #include using namespace std;

typedef long long ll;

int dp[11][11];

char mp[11][11];

int col[11];

int dfs(int i,int n,int k);

int dy=;

int dz=;

struct pos

pos(int xx,int yy,int zz)

};pos st,ed;

void bfs()

if(now*2#include #include using namespace std;

const int inf=1e9;

int n,m;

int mp[22][22],res[22][22],tmp[22][22];

int ansmp[22][22];

int dx=;

int dy=;

int ans=inf;

void fan(int x,int y)

}return;

}for(int j=0;j#include using namespace std;

typedef long long ll;

ll a[222];

void dfs(int wei,int mod,int n,ll val)

if(wei==19)

ll ans=0;

dfs(wei+1,(mod*10+1)%n,n,val*10+1);

if(!a[n])

dfs(wei+1,(mod*10)%n,n,val*10);

}int main()

return 0;

}

/**

poj 3126 prime path

素數+bfs

*/#include #include #include #include using namespace std;

const int nmax=10000;

int notp[nmax+100],prime[nmax],ptot;

int dist[nmax+10];

void init()

q.pop();

for(int i=1;i<=1000;i*=10)}}

}}int main()

return 0;

}

/**

poj 3087 shuffle'm up

洗牌模擬 不是搜尋。

sb的兩個錯。

1.ce:cin要加#include2.wa:cas沒有++

*/#include #include #include #include using namespace std;

mapmp;

int main()

//1 fill(1)

if ( dist[a][ny]<0 || dist[a][ny]>dist[nx][ny]+1 )

//2 fill(2)

if ( dist[nx][b]<0 || dist[nx][b]>dist[nx][ny]+1 )

//3 drop(1)

if ( dist[0][ny]<0 || dist[0][ny]>dist[nx][ny]+1 )

//4 drop(2)

if ( dist[nx][0]<0 || dist[nx][0]>dist[nx][ny]+1 )

//5 pull(1,2)

int bneed=min(b-ny,nx);

if ( dist[nx-bneed][ny+bneed]<0 || dist[nx-bneed][ny+bneed] > dist[nx][ny]+1 )

//6 pull(2,1)

int aneed=min(ny,a-nx);

if ( dist[nx+aneed][ny-aneed]<0 || dist[nx+aneed][ny-aneed] > dist[nx][ny]+1 )

}}void print()

for(int i=ans.size()-1;i>=0;i--)

}int main()

/**

fzu 2150

雙起點bfs。

注意起點重複位置不用算兩次,否則tle。

*/#include #include #include #include using namespace std;

typedef pairp;

char mp[12][12];

int tag[12][12];

int dist[12][12],inq[12][12];

int dx=;

int dy=;

int n,m;

void dfs(int nx,int ny,int cc)}}

}}

}int judge()

else}}

}}

printf("case %d: %d\n",cas++,ans);}}

return 0;

}

/**

uva 11624 火災逃生

火災蔓延+逃生雙向bfs通過預處理,兩次跑完。

當fdist>dist||fdist<0時可以跑

wa點:

1.fire不止乙個。不好用曼哈頓。

2.fire可能到不了你逃跑位置。fdist<0也是可以通行。

*/#include #include #include #include #include using namespace std;

const int nmax=1011;

typedef pairp;

int n,m,sx,sy,fx,fy;

char mp[nmax][nmax];

int dist[nmax][nmax],fdist[nmax][nmax];

int dx=;

int dy=;

void bfs()

for(int i=0;i<4;i++)}}

}}

printf("impossible\n");

}void firebfs();

int dy=;

void bfs()

for(int i=0;i<4;i++)}}

}}void print()

printf("(0, 0)\n");

for(int i=ans.size()-1;i>=0;i--)

}int main()

}bfs();

print();

return 0;

}

#include using namespace std;

const int nmax=111;

int n,m;

char mp[nmax][nmax];

int dx=;

int dy=;

void dfs(int nx,int ny)

int main()

else

}}

轉換角度bfs->人少,kfc多。搜人到kfc距離。

/**

hdu 2612 find a way

尋找kfc

轉換角度->人少,kfc多。搜人到kfc距離。

*/#include #define x first

#define y second

using namespace std;

typedef pairp;

const int nmax=222;

int dx=;

int dy=;

int n,m;

char mp[nmax][nmax];

int dist[2][nmax][nmax];

void bfs(int sx,int sy,int tag)}}

}}int main()

printf("%d\n",mx*11);

}return 0;

}

kuangbin專題一 簡單搜尋

poj 1426 找出乙個由1和0組成的十進位制數m滿足m為n的倍數。有兩種做法,一種是bfs從低位數往高位數搜,另一種是用二叉樹陣列的結構存每次的求出來的餘數,當餘數為0時返回下標,然後根據下標求出其對應的數。1.bfs include includeusing namespace std con...

kuangbin專題一簡單搜尋總結

e find the multiple 題意 找乙個能整除n的數m,對m的要求是只有0和1組成。n不超過200,m長度不超過100.思路 很多人都寫了乙個假演算法,就是在unsigned long long 的範圍裡面找就行了,雖然也a了,但博主想在這裡提一下正確的思路。首先用到了同餘定理 即以任意...

KuangBin 專題一 簡單搜尋 Pots

include include include include include using namespace std cubagea是第乙個瓶子的容量 cubageb是第二個瓶子的容量 stdc是要達到的容量 int used 105 105 cubagea,cubageb,stdc 用於儲存兩個...