棋盘问题(DFS)& Dungeon Master (BFS)
Input
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output
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 ; }
Is an escape possible? If yes, how long will it take?
Input
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
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)的更多相关文章
- hdu 2251 Dungeon Master bfs
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17555 Accepted: 6835 D ...
- POJ2251 Dungeon Master —— BFS
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- Dungeon Master bfs
time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u POJ 2251 Descriptio ...
- POJ 2251 Dungeon Master (BFS最短路)
三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- 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 ...
- [poj] Dungeon Master bfs
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- poj 2251 Dungeon Master( bfs )
题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A, 上代码 #include <iostream> #include<cst ...
- POJ2251 Dungeon Master(bfs)
题目链接. 题目大意: 三维迷宫,搜索从s到e的最小步骤数. 分析: #include <iostream> #include <cstdio> #include <cs ...
- POJ 2251 Dungeon Master bfs 难度:0
http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...
- E - Dungeon Master BFS
[NWUACM] 你被困在一个三维的空间中,现在要寻找最短路径逃生!空间由立方体单位构成你每次向上下前后左右移动一个单位需要一分钟你不能对角线移动并且四周封闭是否存在逃出生天的可能性?如果存在,则需要 ...
随机推荐
- [转载]Linux上使用ssl进行端口转发
原文地址:Linux上使用ssl进行端口转发 作者:呼延十 背景介绍 作为一个后端程序员,经常要和别人联调接口,每当这时,总是被公司的各种,dev,qa,pre,prod环境搞得头疼,,,我真的只是想 ...
- MySQL 授权用户 ; 存储过程的DEFINER; 命令分隔符DELIMITER
最近项目中遇到有人使用DEFINER这样的关键字,找了半天没有怎么理解这个意思.以为是限制谁使用这个存储过程,后来测试发现并不是这样. 搜索网上发现很多说法都不正确.看到一篇博客,做了如下介绍,才有所 ...
- 13-H.264编码解码器的无线应用:1080P60 3D无线影音传输器
H.264编码解码器的无线应用:1080P60 3D无线影音传输器 一.应用领域 家庭媒体娱乐中心 新闻现场采访 无线3D投影机 高清视频会议终端无线延长器 教学,医疗示教 考古,高档商业区域,监狱等 ...
- Kvm04 kvm虚拟机热迁移,可视化管理
目录 1.kvm虚拟机热迁移 1). 冷迁移 修改配置文件,将定义的格式raw,改成qcow2格式! 修改完成,重启服务 2).热迁移 2.Virt-manager管理Kvm虚拟机 1.kvm虚拟机热 ...
- gvfs错误导致tilda和thunar启动缓慢问题的解决
tilda是一个非常轻便的下拉终端,但是安装之后启动发现要过十几秒才会出现界面.命令行启动发现报错如下: 用这条信息到处搜索也找不到有用的解答. 后来终于发现这是一个dbus超时的问题,虽然原因和这个 ...
- 解决python中转化成json的方法不能序列化datetime类型数据(转)
Python自带的json.dumps方法序列化数据时候如果格式化的数据中有datetime类型数据时候会提示错误TypeError: datetime.datetime(2012, 12, 12, ...
- zuul 详解
zuul的简单使用与分析: https://www.jb51.net/article/106716.htm https://www.jianshu.com/p/ff863d532767 zuul自定义 ...
- C#基础知识之类和结构
虽然项目中一直在使用类.结构体等类型,仔细琢磨,还真无法系统的说出个所以然.记录一下类.结构体.类和结构体区别 一.类 对于类,大家都特别熟悉.简单的介绍一下类的结构,然后记录一下Class需要注意的 ...
- 前后端分离下的CAS跨域流程分析
写在最前 前后端分离其实有两类: 开发阶段使用dev-server,生产阶段是打包成静态文件整个放入后端项目中. 开发阶段使用dev-server,生产阶段是打包成静态文件放入单独的静态资源服务器中, ...
- python读文件的4种方式
1.直接打开就读 with open('filepath','r') as f: for line in f: print(line) print('一行数据') 虽然f是一个文件实例,但可以通过以上 ...