hdu1035

#include<stdio.h>
#include<string.h>
int step,n,m;
int a[1010][1010];
char map[11][11];
void DFS(int x,int y)
{
while(x>=0&&y>=0&&x<n&&y<m&&map[x][y]!=‘O‘)
{
if(map[x][y]==‘N‘)
{
map[x][y]=‘O‘;
a[x][y]=++step;
x--;
}
else if(map[x][y]==‘S‘)
{
map[x][y]=‘O‘;
a[x][y]=++step;
x++;
}
else if(map[x][y]==‘E‘)
{
map[x][y]=‘O‘;
a[x][y]=++step;
y++;
}
else if(map[x][y]==‘W‘)
{
map[x][y]=‘O‘;
a[x][y]=++step;
y--;
}
}
if(map[x][y]==‘O‘)
printf("%d step(s) before a loop of %d step(s)\n",a[x][y]-1,step+1-a[x][y]);
else
printf("%d step(s) to exit\n",step);
}
int main()
{
int i,k,x,y;
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
if(n==0||m==0)
break;
for(i=0;i<n;i++)
scanf("%s",map[i]);
x=0;
y=k-1;
step=0;
DFS(x,y);
}
return 0;
}

时间: 2024-08-05 15:23:07

hdu1035的相关文章

HDU1035深搜

/* HDU1035 题意: 给定一个字符矩阵,N S W E分别代表向上,下,左,右前进 模拟搜索,判断: 若能走出字符矩阵,则Yes,输出步数 若走不出矩阵,那么必定有圈存在,必定在矩阵中存在一个点会访问第二次 */ #include <iostream> #include <algorithm> #include <stdio.h> #include <math.h> #include <set> #include <vector&g

poj1573&amp;&amp;hdu1035 Robot Motion(模拟题)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1035 POJ:   http://poj.org/problem?id=1573 Description A robot has been programmed to follow the instructions in its path. Instructions for

POJ1573 ZOJ1708 UVA10116 UVALive5334 HDU1035 Robot Motion【DFS+BFS】

Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16166 Accepted: 7643 Description A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a g

hdu1035 Robot Motion (DFS)

Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8180    Accepted Submission(s): 3771 Problem Description A robot has been programmed to follow the instructions in its path. Instruc

HDU1035 Robot Motion【链式前向星】

Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7039    Accepted Submission(s): 3244 Problem Description A robot has been programmed to follow the instructions in its path. Instruc