poj 1321 棋盘问题 - DFS 2251 Dungeon Master - BFS



链接:戳这里


棋盘问题
Time Limit: 1000MS Memory Limit: 10000K
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


思路:

( 由于要应付某个比赛所以最近在练一些基础的爆搜

枚举每一行去爆搜,当前行的j列为'#' 标记这一列,终止条件即为 num==k || r>n


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,k;
int a[110][110],vis[110],ans;
void DFS(int r,int num){
    if(num==k) {
        ans++;
        return ;
    }
    if(r>n) return ;
    for(int i=1;i<=n;i++){
        if(a[r][i]==1 && !vis[i]){
            vis[i]=1;
            DFS(r+1,num+1);
            vis[i]=0;
        }
    }
    DFS(r+1,num);
}
int main(){
    while(scanf("%d%d",&n,&k)!=EOF){
        if(n==-1 && k==-1) break;
        mst(a,0);
        mst(vis,0);
        ans=0;
        for(int i=1;i<=n;i++){
            getchar();
            for(int j=1;j<=n;j++){
                char c;
                scanf("%c",&c);
                if(c=='#') a[i][j]=1;
            }
        }
        DFS(1,0);
        cout<<ans<<endl;
    }
    return 0;
}


链接:戳这里


Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
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  就算逃出,问最少需要多少步  逃不出就Trapped!


思路:

典型的优先队列+BFS


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
char mp[31][31][31];
int l,r,c;
int vis[31][31][31];
struct node{
    int z,x,y,step;
    node(int z=0,int x=0,int y=0,int step=0):z(z),x(x),y(y),step(step){}
    bool operator < (const node &a) const{
        return step>a.step;
    }
};
int xx[6]={0,0,1,-1,0,0};
int yy[6]={1,-1,0,0,0,0};
int zz[6]={0,0,0,0,1,-1};
bool pd(node t){
    if(t.z<1 || t.z>l || t.x<1 || t.x>r || t.y<1 || t.y>c)
        return false;
    if(vis[t.z][t.x][t.y]==1 || mp[t.z][t.x][t.y]=='#')
        return false;
    return true;
}
priority_queue<node> qu;
void BFS(node st){
    while(!qu.empty()) qu.pop();
    mst(vis,0);
    qu.push(st);
    vis[st.z][st.x][st.y]=1;
    while(!qu.empty()){
        node now=qu.top(),next;
        qu.pop();
        for(int i=0;i<6;i++){
            next.z=now.z+zz[i];
            next.x=now.x+xx[i];
            next.y=now.y+yy[i];
            if(pd(next)){
                next.step=now.step+1;
                if(mp[next.z][next.x][next.y]=='E'){
                    printf("Escaped in %d minute(s).\n",next.step);
                    return ;
                }
                vis[next.z][next.x][next.y]=1;
                qu.push(next);
            }
        }
    }
    printf("Trapped!\n");
}
char s[33];
int main(){
    while(scanf("%d%d%d",&l,&r,&c)!=EOF){
        if(l==0 && r==0 && c==0) break;
        node st;
        for(int i=1;i<=l;i++){
            for(int j=1;j<=r;j++){
                scanf("%s",s);
                for(int k=0;k<strlen(s);k++){
                    mp[i][j][k+1]=s[k];
                    if(mp[i][j][k+1]=='S') st=node(i,j,k+1,0);
                }
            }
        }
        /*for(int i=1;i<=l;i++){
            for(int j=1;j<=r;j++){
                for(int k=1;k<=c;k++){
                    printf("%c ",mp[i][j][k]);
                }
                cout<<endl;
            }
            cout<<endl;
        }*/
        BFS(st);
    }
    return 0;
}
/*
3 4 5
S....
.###.
.##..
###.#

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

E.###
#####
#.###
####E
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值