poj 3083 children of the candy corn
来源:互联网 发布:易吧进销存软件 编辑:程序博客网 时间:2024/06/06 19:37
题目大意:
给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走;
先输出左转优先时,从S到E的步数;
再输出右转优先时,从S到E的步数;
最后输出S到E的最短步数;
W为宽,列数;
H为高,行数;
在左转右转的时候需要标记好方向,最初的方向由起点S确定,而下一步的方向则由前一步的走向决定。
例如,左边优先搜索:
当前位置的方向指向左,(这同时说明前一步是在右边的位置走过来的),那么走下一步时,就要根据下左上右的顺序,先逐格确定当前位置周边的四格是否可行。
若第一次确认可行,就走到下,走过后的方向为下。
若向下不可行,则再确定向左,若向左可行,就走到左边,在左边时的方向为左。
若向左也不可行,则再确定向上,若向上可行,就走到上,在上边时的方向为上。
若向上也不可行,说明进入了迷宫的死胡同,要从原路返回,走回最右边的位置。
右边优先搜索也同理。
代码:
#include<stdio.h>
#include<string.h>
int n,m;
int r[4][2]= {{0,-1},{1,0},{0,1},{-1,0}};
int l[4][2]= {{0,1},{1,0},{0,-1},{-1,0}};
int vis[50][50];
char map[50][50];
int sx,sy,ex,ey,ans;
int que[2500][2];
int dfs1(int x,int y,int step)
{
if(x==ex&&y==ey)
return step+1;
if(x<0||x>=n||y<0||y>=m||map[x][y]=='#')
return 0;
ans=(ans+3)%4;
int temp=0;
while(1)
{
temp=dfs1(x+l[ans][0],y+l[ans][1],step+1);
if(temp>0)
break;
ans=(ans+1)%4;
}
return temp;
}
int dfs2(int x,int y,int step)
{
if(x==ex&&y==ey)
return step+1;
if(x<0||x>=n||y<0||y>=m||map[x][y]=='#')
return 0;
ans=(ans+3)%4;
int temp=0;
while(1)
{
temp=dfs2(x+r[ans][0],y+r[ans][1],step+1);
if(temp>0)
break;
ans=(ans+1)%4;
}
return temp;
}
int bfs()
{
int fir=0,sec=0;
que[sec][0]=sx;
que[sec++][1]=sy;
vis[sx][sy]=1;
int step=1;
while(fir<sec&&!vis[ex][ey])
{
int tmp=sec;
step++;
while(fir<tmp&&!vis[ex][ey])
{
int x=que[fir][0];
int y=que[fir++][1];
for(int i=0; i<4; i++)
{
int x1=x+r[i][0];
int y1=y+r[i][1];
if(x1>=0&&x1<n&&y1>=0&&y1<m&&!vis[x1][y1]&&map[x1][y1]!='#')
{
que[sec][0]=x1;
que[sec++][1]=y1;
vis[x1][y1]=1;
}
}
}
}
return step;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
memset(que,0,sizeof(que));
memset(vis,0,sizeof(vis));
for(int i=0; i<n; i++)
{
scanf("%s",map[i]);
for(int j=0; j<m; j++)
{
if(map[i][j]=='S')
{
sx=i;
sy=j;
}
if(map[i][j]=='E')
{
ex=i;
ey=j;
}
}
}
ans=0;
printf("%d",dfs1(sx,sy,0));
ans=0;
printf(" %d",dfs2(sx,sy,0));
printf(" %d\n",bfs());
}
return 0;
}
- poj 3083 Children of the Candy Corn
- poj 3083Children of the Candy Corn
- poj 3083 Children of the Candy Corn
- poj 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- poj 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- poj 3083 Children of the Candy Corn
- POJ 3083 - Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- poj 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- POJ 3083 Children of the Candy Corn
- poj 3083 Children of the Candy Corn
- 初探java 对象中wait(),notify(),notifyAll() 和线程中的synchronized
- Java基础笔记-第八记
- Linux 学习点滴
- poj1459(Power Network)网络最大流
- android自定义Seekbar
- poj 3083 children of the candy corn
- N!(1042)hdu(大数阶乘)
- [转]java中byte转换int时为何与0xff进行与运算
- iocp例子
- Hanlde消息处理机制的复习——网络图片下载
- Servlet编程基础概述-MVC控制层
- poj 1129 四色原理DFS
- 数据结构实验之链表五:单链表的拆分
- 如何用AHK脚本,把当前窗口最前端显示?有时候查看的时候方便