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) ",ans[sx][sy]-1,temp-ans[sx][sy]+1);
else
printf("%d step(s) to exit ",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

原文地址:https://www.cnblogs.com/james1207/p/3260332.html