1. 程式人生 > >棋盤問題(DFS)& Dungeon Master (DFS)

棋盤問題(DFS)& Dungeon Master (DFS)

tro 所有 define ostream return sig form output des

1棋盤問題 在一個給定形狀的棋盤(形狀可能是不規則的)上面擺放棋子,棋子沒有區別。要求擺放時任意的兩個棋子不能放在棋盤中的同一行或者同一列,請編程求解對於給定形狀和大小的棋盤,擺放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
簡單的dfs
#include<iostream>
#include<stdio.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef 
long long ll; typedef pair<int,int> PII; #define mod 1000000007 #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second //head int ans,n; int k; char a[15][15]; int vis[15]; void dfs(int cur,int e)//cur記錄的是目前能放的數目,e是當前的行數 { if(cur==k) { ans
++; return ; } if(e==n) { return ; } for(int i=0;i<n;i++) { if(!vis[i]&&a[e][i]==#) { vis[i]=1; dfs(cur+1,e+1); vis[i]=0; } } dfs(cur,e+1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); while(cin>>n>>k){ for(int i=0;i<n;i++) vis[i]=0; if(n==-1&&k==-1)break; for(int i=0;i<n;i++) cin>>a[i]; ans=0; dfs(0,0); cout<<ans<<endl; } return 0; }
B - Dungeon Master 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的最短路徑
題解:簡單的BFS,但要註意方向由四個變為六個,還有ans要賦初值(因為這個WA了好多發),其他的代碼上有說
代碼:
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> PII;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
//head
#define INF 0x3f3f3f3f
#define N 35
int n,m,o;
int sx,sy,sz,gx,gy,gz;
int ans=INF;
int vis[N][N][N];
char g[N][N][N];
int dx[]={1,0,-1,0,0,0};//六個方向
int dy[]={0,1,0,-1,0,0};
int dz[]={0,0,0,0,-1,1};
struct mask
{
    int x,y,z,step;
    mask(){}
    mask(int zz,int xx,int yy,int st)//這裏的zz,xx,yy要與下文的關系對好
    {
        z=zz,x=xx,y=yy,step=st;
    }
};
queue<mask>q;
bool check(int a,int b,int c){return 0<=a&&a<o&&0<=b&&b<n&&0<=c&&c<m;};
int bfs()
{
    while(q.size())q.pop();
    q.push(mask(sz,sx,sy,0));
    memset(vis,0,sizeof(vis));
    vis[sz][sx][sy]=1;
    while(q.size())
    {
        mask tmp=q.front();q.pop();//cout<<tmp.z<<" "<<tmp.x<<" "<<tmp.y<<" "<<endl;
        if(tmp.z==gz&&tmp.x==gx&&tmp.y==gy)
        {
            ans=min(ans,tmp.step);
            break;
        }
        for(int i=0;i<6;i++)
        {
            int nx=tmp.x+dx[i];
            int ny=tmp.y+dy[i];//
            int nz=tmp.z+dz[i];
            int nstep=tmp.step;//
             if(vis[nz][nx][ny]==0&&check(nz,nx,ny)&&g[nz][nx][ny]!=#)
             {
                 vis[nz][nx][ny]=1;
                 q.push(mask(nz,nx,ny,nstep+1));//cout<<nz<<" "<<nx<<" "<<ny<<" "<<endl;
           }
        }
    }
    return ans==INF?-1:ans;
}
int main()
{
     ios_base::sync_with_stdio(0); cin.tie(0);
     while(cin>>o>>n>>m){
        if(n+m+o==0)break;
        for(int i=0;i<o;i++)
        {
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {
                    cin>>g[i][j][k];
                    if(g[i][j][k]==S)
                    {
                        sz=i;
                        sx=j;
                        sy=k;
                    }
                     if(g[i][j][k]==E)
                    {
                        gz=i;
                        gx=j;
                        gy=k;
                    }
                }
            }
        }
      /*  for(int i=0;i<o;i++){
            for(int j=0;j<n;j++){
             for(int k=0;k<m;k++){
              cout<<g[i][j][k];
            }cout<<endl;
        }cout<<endl;
     }*/
     ans=INF;//記得這裏的ans要賦初值給它
       if(bfs()==-1)
        cout<<"Trapped!"<<endl;
       else printf("Escaped in %d minute(s).\n",bfs());
     }
    return 0;

}

 

棋盤問題(DFS)& Dungeon Master (DFS)