bfs板子题
-
- 1101.献给阿尔吉侬的花束
- 1113.红与黑(能踏过的最多黑瓷砖)
- 地牢大师(三维bfs
1101.献给阿尔吉侬的花束
- scanf 不要忘记给变量加 &
- fill初始化二维数组
fill(vis[0],vis[0]+N*N,false);
- 多组输入不要忘记清空 Q队列,不要忘记初始化vis数组
- 节点放进队列后不要忘记标记vis数组
#include
#include
#include
using namespace std;
int T,m,n;
const int N=205;
string s[N];
struct node{
int x,y,step;
node(int x,int y,int step):x(x),y(y),step(step){}
};
int vis[N][N];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
bool inside(int cx,int cy){
return cx>=0&&cx<m&&cy>=0&&cy<n;
}
queue<node> Q;
int bfs(){
while(!Q.empty()){
node t=Q.front();
Q.pop();
int x=t.x;
int y=t.y;
int step=t.step;
if(s[x][y]=='E'){
return step;
}
for(int i=0;i<4;i++){
int cx=x+dx[i];
int cy=y+dy[i];
if(inside(cx,cy)&&!vis[cx][cy]&&s[cx][cy]!='#'){
Q.push(node(cx,cy,step+1));
vis[cx][cy]=true;
}
}
}
return -1;
}
signed main(){
scanf("%d",&T);
while(T--){
scanf("%d%d",&m,&n);
fill(vis[0],vis[0]+N*N,false);
1113.红与黑(能踏过的最多黑瓷砖)
#include
#include
#include
using namespace std;
int T,m,n;
const int N=25;
string s[N];
struct node{
int x,y;
node(int x,int y):x(x),y(y){}
};
int vis[N][N];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
bool inside(int cx,int cy){
return cx>=0&&cx<m&&cy>=0&&cy<n;
}
queue<node> Q;
int res;
void bfs(){
while(!Q.empty()){
node t=Q.front();
Q.pop();
int x=t.x;
int y=t.y;
if(s[x][y]=='.'|| s[x][y]=='@')res++;
for(int i=0;i<4;i++){
int cx=x+dx[i];
int cy=y+dy[i];
if(inside(cx,cy)&&!vis[cx][cy]&&s[cx][cy]!='#'){
Q.push(node(cx,cy));
vis[cx][cy]=true;
}
}
}
}
signed main(){
while(scanf("%d%d",&n,&m)!=EOF){
if(!m&&!n)return 0;
fill(vis[0],vis[0]+N*N,false);
res=0;
while(!Q.empty())Q.pop();
for(int i=0;i<m;i++){
cin>>s[i];
for(int j=0;j<n;j++){
if(s[i][j]=='@'){
Q.push(node(i,j));
vis[i][j]=true;
}
}
}
bfs();
cout<<res<<<endl;
}
return 0;
}
地牢大师(三维bfs
string s[N][N];
for(int k=0;k<d;k++){
for(int i=0;i<m;i++){
cin>>s[k][i];
for(int j=0;j<n;j++){
- 其次,输入顺序,d,m,n不要搞错了TT,要跟题目表示的含一顺序一致
#include
#include
#include
#include
#include
using namespace std;
int d,m,n;
const int N=105;
string s[N][N];
struct node{
int x,y,z,step;
node(int x,int y,int z,int step):x(x),y(y),z(z),step(step){}
};
bool vis[N][N][N];
int dx[6]={0,0,1,-1,0,0};
int dy[6]={1,-1,0,0,0,0};
int dz[6]={0,0,0,0,1,-1};
bool inside(int cx,int cy,int cz){
return cx>=0&&cx<m&&cy>=0&&cy<n&&cz>=0&&cz<d;
}
queue<node> Q;
int res;
int bfs(){
while(!Q.empty()){
node t=Q.front();
Q.pop();
int x=t.x;
int y=t.y;
int z=t.z;
int step=t.step;
if(s[z][x][y]=='E') return step;
for(int i=0;i<6;i++){
int cx=x+dx[i];
int cy=y+dy[i];
int cz=z+dz[i];
if(inside(cx,cy,cz)&&!vis[cz][cx][cy]&&s[cz][cx][cy]!='#'){
Q.push(node(cx,cy,cz,step+1));
vis[cz][cx][cy]=true;
}
}
}
return -1;
}
signed main(){
while(scanf("%d%d%d",&d,&m,&n)!=EOF){
if(!d&&!m&&!n)return 0;
fill(vis[0][0],vis[0][0]+N*N*N,false);
res=-1;
while(!Q.empty())Q.pop();
for(int k=0;k<d;k++){
for(int i=0;i<m;i++){
cin>>s[k][i];
for(int j=0;j<n;j++){
if(s[k][i][j]=='S'){
Q.push(node(i,j,k,0));
vis[k][i][j]=true;
}
}
}
}
res=bfs();
if(res==-1)cout<<"Trapped!";
else cout<<"Escaped in "<<res<<" minute(s).";
cout<<endl;
}
return 0;
}