1. 程式人生 > >kuangbin系列【簡單搜索】

kuangbin系列【簡單搜索】

line body pac 好的 初始 ner nta list there

poj-1321棋盤問題【bfs/回溯】
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 36385 Accepted: 17950

Description

在一個給定形狀的棋盤(形狀可能是不規則的)上面擺放棋子,棋子沒有區別。要求擺放時任意的兩個棋子不能放在棋盤中的同一行或者同一列,請編程求解對於給定形狀和大小的棋盤,擺放k個棋子的所有可行的擺放方案C。

Input

輸入含有多組測試數據。
每組數據的第一行是兩個正整數,n k,用一個空格隔開,表示了將在一個n*n的矩陣內描述棋盤,以及擺放棋子的數目。 n <= 8 , k <= n

當為-1 -1時表示輸入結束。
隨後的n行描述了棋盤的形狀:每行有n個字符,其中 # 表示棋盤區域, . 表示空白區域(數據保證不出現多余的空白行或者空白列)。

Output

對於每一組數據,給出一行輸出,輸出擺放的方案數目C (數據保證C<2^31)。

Sample Input

2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1

Sample Output

2
1

【分析】:建議學習回溯的時候對N皇後還有N皇後的變式好好學習一下,類N皇後真是學習回溯非常好的例題。

如在第i行第j列,遇到‘#‘號。那麽接下來的處理就有兩種情況了。
第一種
:把i,j放入到一個數組C中,然後繼續向第i+1行進行搜索,直到找到m個位置或者到了棋盤的邊界 另一種:不選擇第i行第j列的位置,然後繼續向第i+1行進行搜索,直到找到m個位置或者到了棋盤的邊界

【代碼】:
技術分享
#include <cmath>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <set>
#include <map>
#include 
<list> #include <deque> #include <queue> #include <stack> #include <bitset> #include <string> #include <vector> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> #include <functional> using namespace std; typedef long long ll; #pragma comment(linker, "/STACK:102400000,102400000") #define Abs(x) ((x^(x >> 31))-(x>>31)) #define Swap(a,b) (a^=b,b^=a,a^=b) #define PI acos(-1.0) #define INF 0x3f3f3f3f #define EPS 1e-8 #define MOD 1000000007 #define max_ 505 #define maxn 200002 using namespace std; int n,m; char s[10][10];//表示棋盤 int c[10];//表示每一列有沒有擺放過棋子 int tot,cnt; void dfs(int cur)//cur表示當前所在行 { if(cnt == m)//cnt表示當前所擺放棋子數目 { tot++; return ; } if(cur >= n)//超出搜索範圍 return ; for(int j=0;j<n;j++) { if(!c[j] && s[cur][j]==#)//空白處並且還沒有擺放棋子 { c[j]=1; cnt++; dfs(cur+1);//搜索下一行 c[j]=0;//標記清除 cnt--; } } dfs(cur+1);//如果當前行沒有可以擺放的位置 或者cnt已經等於m 但是還沒有搜索完整個棋盤 將要繼續搜索下一行 } int main() { while(~scanf("%d%d",&n,&m)) { if(n==-1&&m==-1) break; memset(c,0,sizeof(c));//將標記初始化為0 tot=cnt=0; for(int i=0;i<n;i++) { scanf("%s",&s[i]); } dfs(0); printf("%d\n",tot); } }
View Code


POJ - 2251 Dungeon Master 【三維dfs】

Description
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#’ and empty cells are represented by a ‘.’. Your starting position is indicated by ‘S’ and the exit by the letter ‘E’. There’s a single blank line after each level. Input is terminated by three zeroes for L, R and C.

Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!

Sample Input

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

【題意】:給出一三維空間的地牢,要求求出由字符‘S‘到字符‘E‘的最短路徑

移動方向可以是上,下,左,右,前,後,六個方向

每移動一次就耗費一分鐘,要求輸出最快的走出時間。

不同L層的地圖,相同RC坐標處是連通的

【代碼】:

kuangbin系列【簡單搜索】