/***
Md. Namzul Hasan
Shahjalal University of Science & technology,sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define new_line pf("\n")
int r ,c, n ;
char str[1001][1001],ch ;
int a,b ,l ;
int mat[1001][1001],k ;
void move_robot()
{
ch = str[a][b] ;
if(ch=='N') a-- ;
else if(ch=='W') b-- ;
else if(ch=='S') a++ ;
else if(ch=='E') b++ ;
}
int main()
{
while(cin>>r>>c>>n && r &&c &&n)
{
getchar() ;
l=0 ;
memset(mat,0 ,sizeof(mat)) ;
for(int i=0 ; i<r ; i++)
cin>>str[i] ;
b = n-1 ;
a =0 ;
mat[a][b] =1 ;
for( k=2 ; ; k++)
{
move_robot() ;
if(a<0 || a>=r || b<0 || b>=c)
{
l=1 ;
break ;
}
if(mat[a][b])
break ;
mat[a][b] = k ;
}
if(l==1)
{
pf("%d step(s) to exit\n",k-1) ;
}
else
pf("%d step(s) before a loop of %d step(s)\n",mat[a][b]-1 , k-mat[a][b]) ;
}
return 0;
}
Md. Namzul Hasan
Shahjalal University of Science & technology,sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define new_line pf("\n")
int r ,c, n ;
char str[1001][1001],ch ;
int a,b ,l ;
int mat[1001][1001],k ;
void move_robot()
{
ch = str[a][b] ;
if(ch=='N') a-- ;
else if(ch=='W') b-- ;
else if(ch=='S') a++ ;
else if(ch=='E') b++ ;
}
int main()
{
while(cin>>r>>c>>n && r &&c &&n)
{
getchar() ;
l=0 ;
memset(mat,0 ,sizeof(mat)) ;
for(int i=0 ; i<r ; i++)
cin>>str[i] ;
b = n-1 ;
a =0 ;
mat[a][b] =1 ;
for( k=2 ; ; k++)
{
move_robot() ;
if(a<0 || a>=r || b<0 || b>=c)
{
l=1 ;
break ;
}
if(mat[a][b])
break ;
mat[a][b] = k ;
}
if(l==1)
{
pf("%d step(s) to exit\n",k-1) ;
}
else
pf("%d step(s) before a loop of %d step(s)\n",mat[a][b]-1 , k-mat[a][b]) ;
}
return 0;
}
No comments:
Post a Comment