Advertisement
Guest User

ct4A

a guest
Mar 19th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int I = 1, J = 1, S = 1;
  8.     while (I != 0 && J != 0 && S != 0)
  9.     {
  10.         scanf("%d %d %d", &I, &J, &S);
  11.         //cout << I << " " << J << " " << S << endl;
  12.  
  13.         int pathN[10010][10010];        //Path nunber (row and collum)
  14.         char pathI[10010][10010];       //Path instruction (N || S || W || E)
  15.         int pathS[10010][10010];        //Path steps until now
  16.  
  17.         for (int i = 0; i < I; i++)
  18.         {
  19.             char linha[10010];
  20.             scanf("%c", linha);
  21.  
  22.             for (int j = 0; j < J; j++)
  23.             {
  24.                 pathN[i][j] = 0;
  25.                 pathI[i][j] = linha[j];
  26.                 pathS[i][j] = 0;
  27.             }
  28.  
  29.         }
  30.  
  31.         int end = 0;
  32.         int iNow = 0;
  33.         int jNow = S - 1;
  34.         int stepsNow = 0;
  35.         while (end == 0)
  36.         {
  37.             pathN[iNow][jNow]++;
  38.  
  39.             //Loop
  40.             if (pathN[iNow][jNow] > 1)
  41.             {
  42.                 int stepsBL = pathS[iNow][jNow];            //Before the loop
  43.                 int stepsL = stepsNow - stepsBL;            //In the loop
  44.                 printf("%d step(s) before a loop of %d step(s)", stepsBL, stepsL);
  45.                 end++;
  46.             }
  47.  
  48.             if (pathN[iNow][jNow] == 1)
  49.             {
  50.                 stepsNow++;
  51.                 pathS[iNow][jNow]++;
  52.  
  53.                 if (pathI[iNow][jNow] == 'N')
  54.                 {
  55.                     iNow--;
  56.                 }
  57.  
  58.                 else if (pathI[iNow][jNow] == 'S')
  59.                 {
  60.                     iNow++;
  61.                 }
  62.  
  63.                 else if (pathI[iNow][jNow] == 'W')
  64.                 {
  65.                     jNow--;
  66.                 }
  67.  
  68.                 else if (pathI[iNow][jNow] == 'E')
  69.                 {
  70.                     jNow++;
  71.                 }
  72.             }
  73.  
  74.             //Saiu da parada
  75.             if (iNow < 0 || iNow > I - 1 || jNow < 0 || jNow > J - 1)
  76.             {
  77.                 printf("%d step(s) to exit", stepsNow);
  78.                 end++;
  79.             }
  80.         }
  81.  
  82.     }
  83.  
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement