标签:namespace mit typedef generate eof cst mod nsis class
Input
Output
Escaped in x minute(s).
Trapped!
Sample Input
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
分析:三维数组+BFS,luo 跑一遍就行
第一份代码是做标记,第二份是改变位置的性质(从可走变为不可走
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
#define ull unsigned long long
#define lli long long
#define pq priority_queue<int>
#define pql priority_queue<ll>
#define pqn priority_queue<node>
#define v vector<int>
#define vl vector<ll>
#define read(x) scanf("%d",&x)
#define lread(x) scanf("%lld",&x);
#define pt(x) printf("%d\n",(x))
#define yes printf("YES\n");
#define NO printf("NO\n");
#define gcd __gcd
#define out(x) cout<<x<<endl;
#define over cout<<endl;
#define rep(j,k) for (int i = (int)(j); i <= (int)(k); i++)
#define input(k) for (int i = 1; i <= (int)(k); i++) {scanf("%d",&a[i]) ; }
#define mem(s,t) memset(s,t,sizeof(s))
#define ok return 0;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod(x) ((x)%9973)
#define test cout<<" ++++++ "<<endl;
//二叉树
#define lson rt<<1, l, m
#define rson rt<<1|1, m+1, r
//线段树
#define ls now<<1
#define rs now<<1|1
int dir[6][3] = {0,0,1,0,0,-1,1,0,0,-1,0,0,0,1,0,0,-1,0};
//int dir[4][2] = {1,0,-1,0,0,1,0,-1}; //单位移动
//int dir[8][2] = {2,1,2,-1,-2,1,-2,-1,1,2,1,-2,-1,2,-1,-2};
int t,n,m,k,x,y,col,ex,ey,ans,cnt,ly;
int a[202][202],b[202][202];
int vis[35][35][35];
char dp[35][35][35];
typedef struct node
{
int lv,x,y,z,t;
} node;
node no,now,next;
queue<node>q;
void BFS()
{
q.push(no);
dp[no.z][no.x][next.y]=‘#‘;
while(!q.empty())
{
now = q.front();
q.pop();
//cout<<now.t<<endl;
if(now.x==ex && now.y==ey && now.z==ly)
{
cnt = now.t;
return ;
}
for(int i=0; i<6; i++)
{
next.z = now.z+dir[i][0];
next.x = now.x+dir[i][1];
next.y = now.y+dir[i][2];
if(next.x<0 || next.x>=m || next.y<0 || next.y>=k || next.z<0 || next.z>=n) continue ;
if(dp[next.z][next.x][next.y]!=‘#‘ && !vis[next.z][next.x][next.y])
{
vis[next.z][next.x][next.y] = 1;
next.t=now.t+1;
q.push(next);
}
}
}
}
int main()
{
TLE;
while(cin>>n>>m>>k)
{
while(!q.empty()) q.pop();
if(!n&&!m&&!k) break;
mem(vis,0);
cnt=-1;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
for(int kk=0; kk<k; kk++)
{
cin>>dp[i][j][kk];
if(dp[i][j][kk]==‘S‘)
no.z=i,no.x=j,no.y=kk,no.t=0,vis[i][j][kk]=1;
if(dp[i][j][kk]==‘E‘)
ly=i,ex=j,ey=kk;
}
BFS();
if(cnt==-1)
cout<<"Trapped!"<<endl;
else
cout<<"Escaped in "<<cnt<<" minute(s)."<<endl;
}
ok;
}
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
#define ull unsigned long long
#define lli long long
#define pq priority_queue<int>
#define pql priority_queue<ll>
#define pqn priority_queue<node>
#define v vector<int>
#define vl vector<ll>
#define read(x) scanf("%d",&x)
#define lread(x) scanf("%lld",&x);
#define pt(x) printf("%d\n",(x))
#define yes printf("YES\n");
#define NO printf("NO\n");
#define gcd __gcd
#define out(x) cout<<x<<endl;
#define over cout<<endl;
#define rep(j,k) for (int i = (int)(j); i <= (int)(k); i++)
#define input(k) for (int i = 1; i <= (int)(k); i++) {scanf("%d",&a[i]) ; }
#define mem(s,t) memset(s,t,sizeof(s))
#define ok return 0;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod(x) ((x)%9973)
#define test cout<<" ++++++ "<<endl;
//二叉树
#define lson rt<<1, l, m
#define rson rt<<1|1, m+1, r
//线段树
#define ls now<<1
#define rs now<<1|1
int dir[6][3] = {0,0,1,0,0,-1,1,0,0,-1,0,0,0,1,0,0,-1,0};
//int dir[4][2] = {1,0,-1,0,0,1,0,-1}; //单位移动
//int dir[8][2] = {2,1,2,-1,-2,1,-2,-1,1,2,1,-2,-1,2,-1,-2};
int t,n,m,k,x,y,col,ex,ey,ans,cnt,ly;
int a[202][202],b[202][202];
int vis[35][35][35];
char dp[35][35][35];
typedef struct node
{
int lv,x,y,z,t;
} node;
node no,now,next;
/*
void DFS(int r,int c)
{
if(ans>=n*m) {cnt++;return ;}
for(int i=0;i<8;i++)
{
int nx = r + dir[i][0];
int ny = c + dir[i][1];
if(nx<0 || ny<0 ||nx >=n ||ny >=m) continue;
if(!vis[nx][ny])
{
vis[nx][ny] = 1;
ans++;
DFS(nx,ny);
ans--;
vis[nx][ny] = 0;
}
}
}
*/
queue<node>q;
void BFS()
{
q.push(no);
dp[no.z][no.x][next.y]=‘#‘;
while(!q.empty())
{
now = q.front();
q.pop();
//cout<<now.t<<endl;
if(now.x==ex && now.y==ey && now.z==ly)
{
cnt = now.t;
return ;
}
for(int i=0; i<6; i++)
{
next.z = now.z+dir[i][0];
next.x = now.x+dir[i][1];
next.y = now.y+dir[i][2];
if(next.x<0 || next.x>=m || next.y<0 || next.y>=k || next.z<0 || next.z>=n) continue ;
if(dp[next.z][next.x][next.y]!=‘#‘)
{
dp[next.z][next.x][next.y]=‘#‘;
next.t=now.t+1;
q.push(next);
}
}
}
}
int main()
{
TLE;
while(cin>>n>>m>>k)
{
while(!q.empty()) q.pop();
if(!n&&!m&&!k) break;
mem(vis,0);
cnt=-1;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
for(int kk=0; kk<k; kk++)
{
cin>>dp[i][j][kk];
if(dp[i][j][kk]==‘S‘)
no.z=i,no.x=j,no.y=kk,no.t=0;
if(dp[i][j][kk]==‘E‘)
ly=i,ex=j,ey=kk;
}
BFS();
if(cnt==-1)
cout<<"Trapped!"<<endl;
else
cout<<"Escaped in "<<cnt<<" minute(s)."<<endl;
}
ok;
}
标签:namespace mit typedef generate eof cst mod nsis class
原文地址:https://www.cnblogs.com/Shallow-dream/p/11561511.html