Advertisement
iamyeasin

Untitled

May 8th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define sf scanf
  3. #define pf printf
  4. #define inf INT_MAX
  5. #define dbg cout << "Debug" << endl;
  6. #define MAX 200
  7. #define block -1
  8. #define white +0
  9. #define noway -3
  10. #define source -4
  11. #define goal -5
  12. #define MP make_pair
  13. #define PB push_back
  14. #define F first
  15. #define S second
  16. #define REP(i,a,b) for( int i=a; i<=b; i++ )
  17.  
  18. using namespace std;
  19.  
  20. int dx[] = { -1,-1,+0,+1, +1,+1,+0,-1 };
  21. int dy[] = { +0,-1,-1,-1, +0,+1,+1,+1 };
  22.  
  23. typedef vector < int > vi;
  24. typedef pair < int, int > pi;
  25. typedef long long ll;
  26.  
  27. int n,m,start,flag=0,px,py;
  28. int cnt[MAX][MAX];
  29. char grid[MAX][MAX];
  30.  
  31. bool isOk( int i, int j ){
  32. return ( i>=1 && i<=n && j>=1 && j<=m) ;
  33. }
  34.  
  35. void clr(){
  36. REP(i,1,MAX){
  37. REP(j,1,MAX){
  38. cnt[i][j] = -1;
  39. grid[i][j] = '\0';
  40. }
  41. }
  42. }
  43.  
  44. void PG(){
  45. REP(i,1,n){
  46. REP(j,1,m){
  47. pf("%2d ",cnt[i][j]);
  48. }
  49. cout << endl;
  50. }
  51. flag= 0 ;
  52. }
  53.  
  54. void rec( int i, int j, int c ){
  55. if( flag ) return;
  56. if( !isOk( i, j ) ){
  57. flag = 1;
  58. // PG();
  59. pf( "%d step(s) to exit\n", c );
  60. return;
  61. }
  62.  
  63. if( cnt[i][j] >= 0 ){
  64. flag = 1;
  65. int out1 = cnt[i][j];
  66. int out2 = abs( cnt[i][j] - cnt[px][py] + 1 );
  67. // PG();
  68. pf("%d step(s) before a loop of 8 step(s)\n",out1, out2 );
  69.  
  70. return ;
  71. }
  72. px = i, py = j;
  73.  
  74. if( grid[i][j] == 'N' ) {
  75. cnt[i][j] = c;
  76. rec( i-1,j,c+1 );
  77. }
  78. else if( grid[i][j] == 'W' ){
  79. cnt[i][j] = c;
  80. rec( i,j-1 ,c+1);
  81. }
  82. else if( grid[i][j] == 'S' ){
  83. cnt[i][j] = c;
  84. rec( i+1,j ,c+1);
  85. }
  86. else {
  87. cnt[i][j] = c;
  88. rec( i,j+1,c+1 );
  89. }
  90.  
  91.  
  92.  
  93. }
  94.  
  95.  
  96. int main(){
  97. #ifndef ONLINE_JUDGE
  98. freopen("in.txt","rt",stdin);
  99. freopen("out.txt","wt",stdout);
  100. #endif
  101.  
  102. while( sf("%d %d %d\n",&n,&m, &start), (n+m+start) ){
  103. clr();
  104. REP(i,1,n){
  105. REP(j,1,m){
  106. cin >> grid[i][j];
  107. }
  108. }
  109.  
  110. rec(1,start,0);
  111.  
  112.  
  113.  
  114. }
  115.  
  116.  
  117. return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement