• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

hdu 1035 Robot Motion(dfs)

虽然做出来了,还是很失望的!!!

加油!!!还是慢慢来吧!!!

》》》》》》》》》》》》》》》》》》》》》》》》》》《《《《《《《《《《《《《《《《《《《《《《《《《《《

》》》》《《《《

很简单的一道题,一步步的走,走出矩阵则说明没有环,若走到已经走过的地方,说明有环,按格式输出结果即可!!!


#include<stdio.h>

#include<string.h>


int n,m,temp;
int ans[1010][1010];
char map[1010][1010];


void dfs(int sx,int sy)
{


while(sx>=0&&sx<n&&sy>=0&&sy<m&&map[sx][sy]!='o')
{
if(map[sx][sy]=='S')
{

// temp++;
map[sx][sy]='o';
ans[sx][sy]=++temp;
sx++;
}
else if(map[sx][sy]=='N')
{
//temp++;
map[sx][sy]='o';
ans[sx][sy]=++temp;
sx--;
}
else if(map[sx][sy]=='E')
{
//temp++;
map[sx][sy]='o';
ans[sx][sy]=++temp;
sy++;
}
else if(map[sx][sy]=='W')
{
//temp++;
map[sx][sy]='o';
ans[sx][sy]=++temp;
sy--;
}
}
if(map[sx][sy]=='o')
printf("%d step(s) before a loop of %d step(s)\n",ans[sx][sy]-1,temp-ans[sx][sy]+1);
else
printf("%d step(s) to exit\n",temp);
}
int main()
{
int k,i;
while(scanf("%d %d %d",&n,&m,&k),n+m+k)
{
memset(ans,0,sizeof(ans));
for(i=0;i<n;i++)
scanf("%s",map[i]);
temp=0;
dfs(0,k-1);
}
return 0;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1035

posted @ 2013-08-15 18:11  Class Xman  阅读(118)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3