码迷,mamicode.com
首页 > 其他好文 > 详细

棋盘问题(DFS)& Dungeon Master (DFS)

时间:2018-08-14 23:29:23      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:inpu   unsigned   rmi   lock   bre   tput   The   break   其他   

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)

标签:inpu   unsigned   rmi   lock   bre   tput   The   break   其他   

原文地址:https://www.cnblogs.com/zhgyki/p/9478626.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!