poj 3083 Children of the Candy Corn

http://poj.org/problem?id=3083

题意:迷宫问题,求从进入点到出口,分别按靠左边,右边和最短距离到达出口所学要的步数;

思路:用dfs求得靠左靠右走到达出口的步数,用bfs求最短最少到达出口的步数;

代码:

View Code
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std;
int xx[6] = {0,1,0,-1,0};
int yy[6] = {0,0,-1,0,1};
int w = 0;
int h = 0;
int xs = 0;
int ys = 0;
int xe = 0;
int ye = 0;
int l = 0;
int r = 0;
int mi = 0;
char map[45][45] = {NULL};
int s[45][45] = {0};
bool visit[45][45] = {0};
int foward = 0;
int ll(int i)
{
int te = (i+1)%4;
if(te == 0)
te = 4;
return te;
}
int rr(int i)
{
int te = (i-1)%4;
if(te == 0)
te = 4;
return te;
}
void dfsl(int x,int y,int step,int fow)
{
int te = ll(fow);
if(x == xe && y == ye)
{
l = step;
return ;
}
if(s[x+xx[te]][y+yy[te]])
dfsl(x+xx[te],y+yy[te],step+1,te);
else
if(s[x+xx[fow]][y+yy[fow]])
dfsl(x+xx[fow],y+yy[fow],step+1,fow);
else
{
te = rr(fow);
if(s[x+xx[te]][y+yy[te]])
dfsl(x+xx[te],y+yy[te],step+1,te);
else
{
te = (fow+2)%4;
if(te == 0)
te = 4;
dfsl(x+xx[te],y+yy[te],step+1,te);
}
}

}
void dfsr(int x,int y,int step,int fow)
{
int te = rr(fow);
if(x == xe && y == ye)
{
r = step;
return ;
}
if(s[x+xx[te]][y+yy[te]])
dfsr(x+xx[te],y+yy[te],step+1,te);
else
if(s[x+xx[fow]][y+yy[fow]])
dfsr(x+xx[fow],y+yy[fow],step+1,fow);
else
{
te = ll(fow);
if(s[x+xx[te]][y+yy[te]])
dfsr(x+xx[te],y+yy[te],step+1,te);
else
{
te = (fow+2)%4;
if(te == 0)
te = 4;
dfsr(x+xx[te],y+yy[te],step+1,te);
}
}

}
void bfs(int x,int y,int step,int fow)
{
queue<int>x3;
queue<int>y3;
queue<int>ste;
queue<int>fo;
x3.push(x);
y3.push(y);
ste.push(step);
fo.push(fow);
visit[x][y] = 1;
while(!x3.empty())
{
x = x3.front();
y = y3.front();
step = ste.front();
fow = fo.front();
x3.pop();
y3.pop();
ste.pop();
fo.pop();
if(x == xe&&y == ye)
{
mi =step;
return ;
}
mi = step;
int te;
te = rr(fow);
if(!visit[x+xx[te]][y+yy[te]]&&s[x+xx[te]][y+yy[te]])
{
visit[x+xx[te]][y+yy[te]] = 1;
x3.push(x+xx[te]);
y3.push(y+yy[te]);
ste.push(step+1);
fo.push(te);
}
if(!visit[x+xx[fow]][y+yy[fow]]&&s[x+xx[fow]][y+yy[fow]])
{
visit[x+xx[fow]][y+yy[fow]] = 1;
x3.push(x+xx[fow]);
y3.push(y+yy[fow]);
ste.push(step+1);
fo.push(fow);
}
te = ll(fow);
if(!visit[x+xx[te]][y+yy[te]]&&s[x+xx[te]][y+yy[te]])
{
visit[x+xx[te]][y+yy[te]] = 1;
x3.push(x+xx[te]);
y3.push(y+yy[te]);
ste.push(step+1);
fo.push(te);
}
}
}
int main()
{
int num = 0;
scanf("%d",&num);
while(num--)
{
memset(s,0,sizeof(s));
memset(visit,0,sizeof(visit));
scanf("%d%d",&w,&h);
getchar();
for(int i = h-1;i >= 0; --i)
{
scanf("%s",map[i]);
}
for(int i = 0;i < h; ++i)
for(int j = 0;j < w; ++j)
if(map[i][j] != '#')
{
s[i][j] = 1;
if(map[i][j] == 'S')
{
xs = i;
ys = j;
}
if(map[i][j] == 'E')
{
xe = i;
ye = j;
}
}
if(xs == 0)
foward = 1;
if(xs == h-1)
foward = 3;
if(ys == 0)
foward = 4;
if(ys == w-1)
foward = 2;
dfsl(xs,ys,1,foward);
dfsr(xs,ys,1,foward);
bfs(xs,ys,1,foward);
printf("%d %d %d\n",l,r,mi);
}
return 0;
}



posted @ 2012-02-29 22:04  LT-blogs  阅读(256)  评论(0编辑  收藏  举报