fork(1) download
  1. /*"The Woods are lovely dark and deep,
  2. But i have promises to keep,
  3. Miles to go before i sleep and Miles to go before i Sleep"
  4. */
  5. #include<bits/stdc++.h>
  6. #define pb(n) push_back(n)
  7. unsigned long long mod=1000000007;
  8. using namespace std;
  9. #define GI ({long int t;scanf("%ld",&t);t;})
  10. #define all(x) x.begin(),x.end() //sort(all(x))
  11. #define sz(h1) h1.size()
  12. int m,n;
  13. int visited[100][100];
  14. char ch[100][100]; //made the charater array as a global array.In this array only i give the input
  15. void DFS(int x,int y)
  16. {
  17. if(visited[x][y]==1 || ch[x][y] == '|')
  18. return ;
  19. visited[x][y]=1;
  20. if(ch[x][y]=='J')//SUPPOSE 'J' represent JACK and the 'R' represent ROBot and the robert wants to reach the jack.
  21. return;
  22. if((x+1<m)) //'|' represnts a block and '.' represnts a path-here i am checking the limits
  23. DFS(x+1,y);
  24. if((x-1>=0))
  25. DFS(x-1,y);
  26. if((y+1<n))
  27. DFS(x,y+1);
  28. if((y-1>=0))
  29. DFS(x,y-1);
  30. // CALLING THE FUNCTION RECURSIVELY
  31.  
  32. }
  33. int main()
  34. {
  35. cout<<"ENter the number of rows and columns:";
  36. cin>>m>>n;
  37. cout<<"Enter the elements:\n";
  38. for(int i=0;i<m;i++)
  39. for(int j=0;j<n;j++)
  40. cin>>ch[i][j];
  41.  
  42. int x=-1,y=-1;
  43. for(int i=0;i<m;i++)
  44. {
  45. for(int j=0;j<n;j++)
  46. {
  47. if(ch[i][j]=='R')
  48. {
  49. x=i,y=j;
  50. break;
  51. }
  52. }
  53. if(x>=0)
  54. break;
  55. }
  56.  
  57. DFS(x,y);
  58. x=-1,y=-1;
  59. for(int i=0;i<m;i++)
  60. {
  61. for(int j=0;j<n;j++)
  62. {
  63. if(ch[i][j]=='J')
  64. {
  65. x=i,y=j;
  66. break;
  67. }
  68. }
  69. if(x>=0) //just using x as flag variable
  70. break;
  71. }
  72. if(visited[x][y]==1)
  73. cout<<"ROBOT CAN REACH THE BOSS\n";
  74. else
  75. cout<<"ROBOT CANNOT REACH THE BOSS\n";
  76. return 0;
  77. }
  78.  
Success #stdin #stdout 0s 3392KB
stdin
3 3
J|.
|..
..R
stdout
ENter the number of rows and columns:Enter the elements:
ROBOT CANNOT REACH THE BOSS