poj 3083 Children of the Candy Corn bfs
2017-03-29 19:26
337 查看
题意:
给你一个n*m的迷宫从起点到终点靠右行走、靠左行走、和最短行走的步数是多少
分析:
最短就直接bfs就好了,
对于靠右(左)行走的情况下我们可以想象每次选择的方向跟当前人面向方向有关,一开始我们假定人是面向前的然后开始模拟
注意对于迷宫中的每一个点都可以经过4次(即从上下左右4个方向经过)那么用vis【】【】【4】来判定是否走过这步
ACcode:///写的又臭又长 差点崩溃
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
using namespace std;
#define maxn 100007
char mapp[50][50];
bool vis[50][50];
bool visr[50][50][4];
bool visl[50][50][4];
int sx,sy,ex,ey,loop,n,m;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int dxr[]={-1,0,1,0};
int dyr[]={0,-1,0,1};
int dxl[]={-1,0,1,0};
int dyl[]={0,1,0,-1};
struct N{
int x,y;
int st;
int from;///0--向上 1--向左 2--向下 3--向右
int to;/// 0--向上 1--向右 2--向下 3--向左
};
int main(){
scanf("%d",&loop);
while(loop--){
int ans=0;
scanf("%d%d",&m,&n);
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j){
cin>>mapp[i][j];
if(mapp[i][j]=='S'){
sx=i;
sy=j;
}
if(mapp[i][j]=='E'){
ex=i;
ey=j;
}
}
memset(visl,0,sizeof(visl));
queue<N>q;
N now,next;
now.x=sx;
now.y=sy;
now.st=1;
now.to=0;
q.push(now);
visl[sx][sy][0]=true;
while(!q.empty()){
now=q.front();
q.pop();
if(mapp[now.x][now.y]=='E'){
ans=now.st;
break;
}
int be=(now.to-1+4)%4;
for(int i=0;i<4;++i){
int nx,ny,k;
k=(be+i)%4;
nx=next.x=now.x+dxl[k];
ny=next.y=now.y+dyl[k];
next.st=now.st+1;
next.to=k;
if(nx>0&&ny>0&&nx<=n&&ny<=m&&!visl[nx][ny][k]&&(mapp[nx][ny]=='.'||mapp[nx][ny]=='E')){
q.push(next);
visl[nx][ny][k]=1;
// cout<<"goto in "<<nx<<" "<<ny<<" from "<<k<<'\12';
break;
}
}
}
cout<<ans<<' ';
memset(visr,0,sizeof(visr));
while(!q.empty())q.pop();
now.x=sx;
now.y=sy;
now.st=1;
now.from=0;
q.push(now);
visr[sx][sy][0]=true;
while(!q.empty()){
now=q.front();
q.pop();
if(mapp[now.x][now.y]=='E'){
ans=now.st;
break;
}
int be=(now.from-1+4)%4;
for(int i=0;i<4;++i){
int nx,ny,k;
k=(be+i)%4;
nx=next.x=now.x+dxr[k];
ny=next.y=now.y+dyr[k];
next.st=now.st+1;
next.from=k;
if(nx>0&&ny>0&&nx<=n&&ny<=m&&!visr[nx][ny][k]&&(mapp[nx][ny]=='.'||mapp[nx][ny]=='E')){
q.push(next);
visr[nx][ny][k]=1;
//cout<<"goto in "<<nx<<" "<<ny<<" from "<<k<<'\12';
break;
}
}
}
cout<<ans<<' ';
memset(vis,0,sizeof(vis));
while(!q.empty())q.pop();
now.x=sx;
now.y=sy;
now.st=1;
q.push(now);
vis[sx][sy]=true;
while(!q.empty()){
now=q.front();
q.pop();
if(mapp[now.x][now.y]=='E'){
ans=now.st;
break;
}
for(int i=0;i<4;++i){
int nx,ny;
nx=next.x=now.x+dx[i];
ny=next.y=now.y+dy[i];
next.st=now.st+1;
if(nx>0&&ny>0&&nx<=n&&ny<=m&&!vis[nx][ny]&&(mapp[nx][ny]=='.'||mapp[nx][ny]=='E')){
q.push(next);
vis[nx][ny]=1;
}
}
}
cout<<ans<<'\12';
}
return 0;
}
给你一个n*m的迷宫从起点到终点靠右行走、靠左行走、和最短行走的步数是多少
分析:
最短就直接bfs就好了,
对于靠右(左)行走的情况下我们可以想象每次选择的方向跟当前人面向方向有关,一开始我们假定人是面向前的然后开始模拟
注意对于迷宫中的每一个点都可以经过4次(即从上下左右4个方向经过)那么用vis【】【】【4】来判定是否走过这步
ACcode:///写的又臭又长 差点崩溃
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
using namespace std;
#define maxn 100007
char mapp[50][50];
bool vis[50][50];
bool visr[50][50][4];
bool visl[50][50][4];
int sx,sy,ex,ey,loop,n,m;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int dxr[]={-1,0,1,0};
int dyr[]={0,-1,0,1};
int dxl[]={-1,0,1,0};
int dyl[]={0,1,0,-1};
struct N{
int x,y;
int st;
int from;///0--向上 1--向左 2--向下 3--向右
int to;/// 0--向上 1--向右 2--向下 3--向左
};
int main(){
scanf("%d",&loop);
while(loop--){
int ans=0;
scanf("%d%d",&m,&n);
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j){
cin>>mapp[i][j];
if(mapp[i][j]=='S'){
sx=i;
sy=j;
}
if(mapp[i][j]=='E'){
ex=i;
ey=j;
}
}
memset(visl,0,sizeof(visl));
queue<N>q;
N now,next;
now.x=sx;
now.y=sy;
now.st=1;
now.to=0;
q.push(now);
visl[sx][sy][0]=true;
while(!q.empty()){
now=q.front();
q.pop();
if(mapp[now.x][now.y]=='E'){
ans=now.st;
break;
}
int be=(now.to-1+4)%4;
for(int i=0;i<4;++i){
int nx,ny,k;
k=(be+i)%4;
nx=next.x=now.x+dxl[k];
ny=next.y=now.y+dyl[k];
next.st=now.st+1;
next.to=k;
if(nx>0&&ny>0&&nx<=n&&ny<=m&&!visl[nx][ny][k]&&(mapp[nx][ny]=='.'||mapp[nx][ny]=='E')){
q.push(next);
visl[nx][ny][k]=1;
// cout<<"goto in "<<nx<<" "<<ny<<" from "<<k<<'\12';
break;
}
}
}
cout<<ans<<' ';
memset(visr,0,sizeof(visr));
while(!q.empty())q.pop();
now.x=sx;
now.y=sy;
now.st=1;
now.from=0;
q.push(now);
visr[sx][sy][0]=true;
while(!q.empty()){
now=q.front();
q.pop();
if(mapp[now.x][now.y]=='E'){
ans=now.st;
break;
}
int be=(now.from-1+4)%4;
for(int i=0;i<4;++i){
int nx,ny,k;
k=(be+i)%4;
nx=next.x=now.x+dxr[k];
ny=next.y=now.y+dyr[k];
next.st=now.st+1;
next.from=k;
if(nx>0&&ny>0&&nx<=n&&ny<=m&&!visr[nx][ny][k]&&(mapp[nx][ny]=='.'||mapp[nx][ny]=='E')){
q.push(next);
visr[nx][ny][k]=1;
//cout<<"goto in "<<nx<<" "<<ny<<" from "<<k<<'\12';
break;
}
}
}
cout<<ans<<' ';
memset(vis,0,sizeof(vis));
while(!q.empty())q.pop();
now.x=sx;
now.y=sy;
now.st=1;
q.push(now);
vis[sx][sy]=true;
while(!q.empty()){
now=q.front();
q.pop();
if(mapp[now.x][now.y]=='E'){
ans=now.st;
break;
}
for(int i=0;i<4;++i){
int nx,ny;
nx=next.x=now.x+dx[i];
ny=next.y=now.y+dy[i];
next.st=now.st+1;
if(nx>0&&ny>0&&nx<=n&&ny<=m&&!vis[nx][ny]&&(mapp[nx][ny]=='.'||mapp[nx][ny]=='E')){
q.push(next);
vis[nx][ny]=1;
}
}
}
cout<<ans<<'\12';
}
return 0;
}
相关文章推荐
- POJ 3083-Children of the Candy Corn(dfs+bfs)
- POJ 3083 Children of the Candy Corn (BFS+顺时针逆时针DFS)
- POJ 3083 Children of the Candy Corn DFS及BFS搜索
- Poj 3083 Children of the Candy Corn (DFS+BFS)
- POJ 3083 Children of the Candy Corn DFS及BFS搜索
- POJ 3083 Children of theCandy Corn(DFS and BFS)
- poj 3083 Children of the Candy Corn dfs+bfs
- poj 3083 Children of the Candy Corn (DFS+BFS)
- poj 3083 children of the candy corn----DFS 和BFS (蛋疼的方向)
- poj 3083 Children of the Candy Corn 解题报告-- DFS BFS
- POJ 3083 Children of the Candy Corn(DFS + BFS)
- poj 3083 Children of the Candy Corn(dfs+bfs)
- POJ:3083 Children of the Candy Corn(bfs+dfs)
- poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】
- POJ 3083 Children of the Candy Corn (BFS+顺时针逆时针DFS)
- POJ3083——Children of the Candy Corn(DFS+BFS)
- POJ 3083 Children of the Candy Corn(顺时针DFS+逆时针DFS+BFS)
- POJ 3083 Children of the Candy Corn(DFS + BFS)
- POJ 3083 Children of the Candy Corn (BFS+顺时针逆时针DFS)
- poj-3083 Children of the Candy Corn-DFS+BFS