fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <tuple>
  5.  
  6. typedef std::vector<std::tuple<int, int, std::string>> DType;
  7.  
  8. DType FindWord(const std::vector<std::string>& Vec, std::string Word){
  9. DType Dir = {
  10. std::make_tuple(-1, 1, "LD"), std::make_tuple(0, 1, "D"),std::make_tuple(1, 1, "RD"),
  11. std::make_tuple(-1, 0, "L"), std::make_tuple(1, 0, "R"),
  12. std::make_tuple(-1, -1, "LU"), std::make_tuple(0, -1, "U"),std::make_tuple(1, -1, "RU"),
  13. };
  14. DType Ret;
  15. int px = 0;
  16. int py = 0;
  17. int Cur = 0;
  18.  
  19. for (int i = 0; i < Vec.size(); i++){
  20. for (int j = 0; j < Vec[i].size(); j++){
  21. for (auto& o : Dir){
  22. for (std::size_t k = 0; k < Word.size(); k++){
  23. px = j + std::get<0>(o)*k;
  24. py = i + std::get<1>(o)*k;
  25. if (py < 0)break;
  26. if (py >= Vec.size()) break;
  27. if (px < 0)break;
  28. if (px >= Vec[py].size())break;
  29.  
  30. if (Vec[py][px] == Word[Cur]){
  31. Cur++;
  32. }
  33. else{
  34. Cur = 0;
  35. }
  36.  
  37. }
  38. if (Cur == Word.size()){
  39. Ret.push_back(std::make_tuple(i, j, std::get<2>(o)));
  40. }
  41. Cur = 0;
  42. }
  43. }
  44. }
  45.  
  46. return Ret;
  47. }
  48.  
  49.  
  50. int main(){
  51. std::vector<std::string> vec = {
  52. "WVERTICALL",
  53. "ROOAFFLSAB",
  54. "ACRILIATOA",
  55. "NDODKONWDC",
  56. "DRKESOODDK",
  57. "OEEPZEGLIW",
  58. "MSIIHOAERA",
  59. "ALRKRRIRER",
  60. "KODIDEDRCD",
  61. "HELWSLEUTH",
  62. };
  63. std::vector<std::string> Words = {
  64. "WEEK",
  65. "FIND",
  66. "RANDOM",
  67. "SLEUTH",
  68. "BACKWARD",
  69. "VERTICAL",
  70. "DIAGONAL",
  71. "WIKIPEDIA",
  72. "HORIZONTAL",
  73. "WORDSEARCH",
  74. };
  75. std::vector<std::string> vec2 = {
  76. "MAMEMAM",
  77. "AAoMoAA",
  78. "MoMAMoM",
  79. "EMAEAME",
  80. "MoMAMoM",
  81. "AAoMoAA",
  82. "MAMEMAM",
  83. };
  84.  
  85. std::string MAME = "MAME";
  86.  
  87. for (std::size_t i = 0; i < Words.size(); i++){
  88. auto R = FindWord(vec, Words[i]);
  89.  
  90. if (R.size() == 0){
  91. std::cout << Words[i] << " is NotFound!" << std::endl;
  92. continue;
  93. }
  94. std::cout << Words[i] << " is ";
  95. for (auto& o : R) std::cout << std::get<0>(o)+1 << ',' << std::get<1>(o)+1 << "@" << std::get<2>(o) << ' ';
  96. std::cout << std::endl;
  97. }
  98. auto R = FindWord(vec2, MAME);
  99.  
  100. if (R.size() == 0){
  101. std::cout << MAME << " is NotFound!" << std::endl;
  102. }
  103. std::cout << MAME << " is ";
  104. for (auto& o : R) std::cout << std::get<0>(o)+1 << ',' << std::get<1>(o)+1 << "@" << std::get<2>(o) << ' ';
  105. std::cout << std::endl;
  106.  
  107. return 0;
  108. }
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
WEEK is NotFound!
FIND is 2,5@RD 
RANDOM is 2,1@D 
SLEUTH is 10,5@R 
BACKWARD is 2,10@D 
VERTICAL is 1,2@R 
DIAGONAL is 9,7@U 
WIKIPEDIA is 10,4@U 
HORIZONTAL is 10,1@RU 
WORDSEARCH is 1,1@RD 
MAME is 1,1@D 1,1@RD 1,1@R 1,7@LD 1,7@D 1,7@L 7,1@R 7,1@U 7,1@RU 7,7@L 7,7@LU 7,7@U