hdu 1242 广搜总是wa求高手帮助。。

JustSteps 2012-08-04 09:48:32
我的思路是用广搜 但是不设定标记。直到找出最小的http://acm.hdu.edu.cn/showproblem.php?pid=1242我没学过C++,我试了好几组数据都对可是总是wa~弄了一天了,~请高手赐教支持错误或者指点思路,本人不甚感激。
详情看代码吧:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define max 201
#define maxsize 40005
typedef struct
{
int x,y;
}Queue;
char graph[max][max];
int step[max][max];
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int bfs(int n,int m,Queue np);
int main()
{
int n,m;
Queue np;
while(scanf("%d%d%*c",&n,&m)!=EOF)
{
memset(graph,0,sizeof(graph));
memset(step,0,sizeof(step));

int i,j;
for(i=0;i<n;i++)
{
gets(graph[i]);
for(j=0;j<m;j++)
if(graph[i][j]=='a')
{
np.x=i;
np.y=j;
}
}
int res=bfs(n,m,np);
if(res)
printf("%d\n",res);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}
int bfs(int n,int m,Queue np)
{
Queue q[maxsize],cp;
int res=0,front,rear,i;
front=rear=0;
q[rear++]=np;

step[np.x][np.y]=0;
while(front<rear)
{
np=q[front++];
for(i=0;i<4;i++)
{
cp.x=np.x+dir[i][0];
cp.y=np.y+dir[i][1];
if(cp.x>=0&&cp.x<n&&cp.y>=0&&cp.y<m&&graph[cp.x][cp.y]!='#')
{

if((step[cp.x][cp.y]>step[np.x][np.y]+1)||step[cp.x][cp.y]==0)
{
step[cp.x][cp.y]=step[np.x][np.y]+1;
q[rear++]=cp;//入队
}
if(graph[cp.x][cp.y]=='r')
{
res=step[cp.x][cp.y];
}
if(graph[cp.x][cp.y]=='x')
{
step[cp.x][cp.y]++;
}

}
}
}
return res;
}
...全文
60 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
JustSteps 2012-08-04
  • 打赏
  • 举报
回复
有人么~~

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧