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[][];
int vis[];
void dfs(int cur,int e)//cur记录的是目前能放的数目,e是当前的行数
{
if(cur==k)
{
ans++;
return ;
}
if(e==n)
{
return ;
}
for(int i=;i<n;i++)
{
if(!vis[i]&&a[e][i]=='#')
{
vis[i]=;
dfs(cur+,e+);
vis[i]=;
}
}
dfs(cur,e+);
}
int main()
{
ios_base::sync_with_stdio(); cin.tie();
while(cin>>n>>k){
for(int i=;i<n;i++)
vis[i]=;
if(n==-&&k==-)break;
for(int i=;i<n;i++)
cin>>a[i];
ans=;
dfs(,);
cout<<ans<<endl;
}
return ; }
 
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[]={,,-,,,};//六个方向
int dy[]={,,,-,,};
int dz[]={,,,,-,};
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 <=a&&a<o&&<=b&&b<n&&<=c&&c<m;};
int bfs()
{
while(q.size())q.pop();
q.push(mask(sz,sx,sy,));
memset(vis,,sizeof(vis));
vis[sz][sx][sy]=;
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=;i<;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]==&&check(nz,nx,ny)&&g[nz][nx][ny]!='#')
{
vis[nz][nx][ny]=;
q.push(mask(nz,nx,ny,nstep+));//cout<<nz<<" "<<nx<<" "<<ny<<" "<<endl;
}
}
}
return ans==INF?-:ans;
}
int main()
{
ios_base::sync_with_stdio(); cin.tie();
while(cin>>o>>n>>m){
if(n+m+o==)break;
for(int i=;i<o;i++)
{
for(int j=;j<n;j++)
{
for(int k=;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()==-)
cout<<"Trapped!"<<endl;
else printf("Escaped in %d minute(s).\n",bfs());
}
return ; }
 

棋盘问题(DFS)& Dungeon Master (BFS)的更多相关文章

  1. hdu 2251 Dungeon Master bfs

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17555   Accepted: 6835 D ...

  2. POJ2251 Dungeon Master —— BFS

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  3. Dungeon Master bfs

    time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u POJ 2251 Descriptio ...

  4. POJ 2251 Dungeon Master (BFS最短路)

    三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  5. poj 2251 Dungeon Master (BFS 三维)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  6. [poj] Dungeon Master bfs

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  7. poj 2251 Dungeon Master( bfs )

    题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A,     上代码 #include <iostream> #include<cst ...

  8. POJ2251 Dungeon Master(bfs)

    题目链接. 题目大意: 三维迷宫,搜索从s到e的最小步骤数. 分析: #include <iostream> #include <cstdio> #include <cs ...

  9. POJ 2251 Dungeon Master bfs 难度:0

    http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...

  10. E - Dungeon Master BFS

    [NWUACM] 你被困在一个三维的空间中,现在要寻找最短路径逃生!空间由立方体单位构成你每次向上下前后左右移动一个单位需要一分钟你不能对角线移动并且四周封闭是否存在逃出生天的可能性?如果存在,则需要 ...

随机推荐

  1. javascript的继承模式

    在javascript里面看到javascript的继承模式和传统的继承模式是有区别的,就想查资料看一下到底有区别,就看到了这篇文章,觉得讲得还可以,暂时先放上来,以后有别的东西再补充: http:/ ...

  2. Git配置全局账号密码避免每次拉取、提交输入账号密码

    前言 在大家使用github的过程中,一定会碰到这样一种情况,就是每次要push 和pull时总是要输入github的账号和密码,这样不仅浪费了大量的时间且降低了工作效率.在此背景下,本文在网上找了两 ...

  3. Spark- Spark从SFTP中读取zip压缩文件数据做计算

    我们遇到个特别的需求,一个数据接入的流程跑的太慢,需要升级为用大数据方式去处理,提高效率. 数据: 数据csv文件用Zip 压缩后放置在SFTP中 数据来源: SFTP 数据操作: 文件和它的压缩包一 ...

  4. PeStudio读取pe信息

    https://blog.csdn.net/x_xx_xxx_xxxx/article/details/79867928 PeStudio  主要利用此界面工具 https://blog.csdn.n ...

  5. docker:python与docker

    一:环境准备 pycharm:专业版(windows) docker ce 免费版(ubantu16.04) os: os:防火墙 二:开发流程 pycharm中开发环境搭建的工作原理: 1. pyc ...

  6. json序列化反序列

    json只能处理简单的数据类型:字典 列表等... 文件只能存字符串和二进制 序列化:把内存的对象变为字符串 反序列化:将字符串变回为内存对象

  7. BLUEHOST香港主机FTP连接不上解决办法

    昨天购买了BLUEHOST的香港主机后,以为一切顺风顺水,结果FTP却连接不上,用了多种FTP工具都不行,​​​按官方博客要求开启了TSL仍然不行​.​经过一晚上的测试后终于成功,现分享出来. 方法一 ...

  8. spring boot jar的支持

  9. STM32输入捕获TIM2四通道

    相比于一通道,原子的例程里因为清了计数时间,所以要对程序进行修改. 记录上升沿后的计数,然后记录下降沿的计数.相减后计算高电平时间,对于定时器中断间隔的边界要分开处理. 这里因为我的接收机时间是1ms ...

  10. 【CF1210D】Konrad and Company Evaluation(vector,图论)

    题意:有i个人,m对两两之间的关系,第i个人初始的薪水为i,有q次操作,第i次操作会把v[i]号的薪水提升成n+i 如果两个人之间存在关系,薪水高的会向薪水低的炫耀 定义u,v,w为一个三元组,当u向 ...