Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define sf scanf
- #define pf printf
- #define inf INT_MAX
- #define dbg cout << "Debug" << endl;
- #define MAX 200
- #define block -1
- #define white +0
- #define noway -3
- #define source -4
- #define goal -5
- #define MP make_pair
- #define PB push_back
- #define F first
- #define S second
- #define REP(i,a,b) for( int i=a; i<=b; i++ )
- using namespace std;
- int dx[] = { -1,-1,+0,+1, +1,+1,+0,-1 };
- int dy[] = { +0,-1,-1,-1, +0,+1,+1,+1 };
- typedef vector < int > vi;
- typedef pair < int, int > pi;
- typedef long long ll;
- int n,m,start,flag=0,px,py;
- int cnt[MAX][MAX];
- char grid[MAX][MAX];
- bool isOk( int i, int j ){
- return ( i>=1 && i<=n && j>=1 && j<=m) ;
- }
- void clr(){
- REP(i,1,MAX){
- REP(j,1,MAX){
- cnt[i][j] = -1;
- grid[i][j] = '\0';
- }
- }
- }
- void PG(){
- REP(i,1,n){
- REP(j,1,m){
- pf("%2d ",cnt[i][j]);
- }
- cout << endl;
- }
- flag= 0 ;
- }
- void rec( int i, int j, int c ){
- if( flag ) return;
- if( !isOk( i, j ) ){
- flag = 1;
- // PG();
- pf( "%d step(s) to exit\n", c );
- return;
- }
- if( cnt[i][j] >= 0 ){
- flag = 1;
- int out1 = cnt[i][j];
- int out2 = abs( cnt[i][j] - cnt[px][py] + 1 );
- // PG();
- pf("%d step(s) before a loop of 8 step(s)\n",out1, out2 );
- return ;
- }
- px = i, py = j;
- if( grid[i][j] == 'N' ) {
- cnt[i][j] = c;
- rec( i-1,j,c+1 );
- }
- else if( grid[i][j] == 'W' ){
- cnt[i][j] = c;
- rec( i,j-1 ,c+1);
- }
- else if( grid[i][j] == 'S' ){
- cnt[i][j] = c;
- rec( i+1,j ,c+1);
- }
- else {
- cnt[i][j] = c;
- rec( i,j+1,c+1 );
- }
- }
- int main(){
- #ifndef ONLINE_JUDGE
- freopen("in.txt","rt",stdin);
- freopen("out.txt","wt",stdout);
- #endif
- while( sf("%d %d %d\n",&n,&m, &start), (n+m+start) ){
- clr();
- REP(i,1,n){
- REP(j,1,m){
- cin >> grid[i][j];
- }
- }
- rec(1,start,0);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement