fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. void findEle(vector<vector<char>> locationMap,int leftC,int topC,int bottomC,int rightC)
  6. {
  7. int rows = locationMap.size();
  8. int cols = locationMap[0].size();
  9. vector<vector<int>> left(rows,vector<int>(cols,0));
  10. vector<vector<int>> right(rows,vector<int>(cols,0));
  11. vector<vector<int>> top(rows,vector<int>(cols,0));
  12. vector<vector<int>> bottom(rows,vector<int>(cols,0));
  13.  
  14. for(int i=0;i<rows;i++)
  15. {
  16. if(locationMap[i][0]=='E' || locationMap[i][0]=='O')
  17. {
  18. left[i][0]=1;
  19. }
  20. for(int j=1;j<cols;j++)
  21. {
  22. if(locationMap[i][j]=='E' || locationMap[i][j]=='O')
  23. {
  24. left[i][j]=left[i][j-1]+1;
  25. }
  26. else
  27. {
  28. left[i][j]=0;
  29. }
  30.  
  31. }
  32. }
  33. for(int i=0;i<rows;i++)
  34. {
  35. if(locationMap[i][cols-1]=='E' || locationMap[i][cols-1]=='O')
  36. {
  37. right[i][cols-1]=1;
  38. }
  39. for(int j=cols-2;j>=0;j--)
  40. {
  41. if(locationMap[i][j]=='E' || locationMap[i][j]=='O')
  42. {
  43. right[i][j]=right[i][j+1]+1;
  44. }
  45. else
  46. {
  47. right[i][j]=0;
  48. }
  49.  
  50. }
  51. }
  52.  
  53. for(int j=0;j<cols;j++)
  54. {
  55. if(locationMap[0][j]=='E' || locationMap[0][j]=='O')
  56. {
  57. top[0][j]=1;
  58. }
  59. for(int i=1;i<rows;i++)
  60. {
  61. if(locationMap[i][j]=='E' || locationMap[i][j]=='O')
  62. {
  63. top[i][j]=top[i-1][j]+1;
  64. }
  65. else
  66. {
  67. top[i][j]=0;
  68. }
  69.  
  70. }
  71. }
  72.  
  73. for(int j=0;j<cols;j++)
  74. {
  75. if(locationMap[rows-1][j]=='E' || locationMap[rows-1][j]=='O')
  76. {
  77. bottom[rows-1][j]=1;
  78. }
  79. for(int i=rows-2;i>=0;i--)
  80. {
  81. if(locationMap[i][j]=='E' || locationMap[i][j]=='O')
  82. {
  83. bottom[i][j]=bottom[i+1][j]+1;
  84. }
  85. else
  86. {
  87. bottom[i][j]=0;
  88. }
  89.  
  90. }
  91. }
  92. int found =0;
  93. for(int i=0;i<rows;i++)
  94. {
  95. for(int j=0;j<cols;j++)
  96. {
  97. if(left[i][j]==leftC && right[i][j]==rightC && bottom[i][j]==bottomC && top[i][j]==topC && locationMap[i][j]=='O' )
  98. {
  99. cout<<i<<"->"<<j;
  100. found=1;
  101. break;
  102. }
  103. }
  104. }
  105. if(found==0)
  106. {
  107. cout<<"Not found";
  108. }
  109. }
  110. int main() {
  111. // your code goes here
  112. vector<vector<char>> locationMap = {
  113. {'O', 'E', 'E', 'E', 'X'},
  114. {'E', 'O', 'X', 'X', 'X'},
  115. {'E', 'E', 'E', 'E', 'E'},
  116. {'X', 'E', 'O', 'E', 'E'},
  117. {'X', 'E', 'X', 'E', 'X'}
  118. };
  119. findEle(locationMap,2,2,1,3);
  120. return 0;
  121. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
3->2