标签:printf struct name cout 建立 tor push flag 包含
思路:
暴力搜索. 建立一个数组alpha,与26个字母相对应. 每个数组包含一个Vector,Vector中保存每个字母的位置.
例如alpha[0] 代表存储字母‘A‘ 的Vector;
代码:
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <cstdio> 5 using namespace std; 6 7 struct Node { 8 int x, y; 9 }; 10 string str[110]; 11 // 数组的数组 12 vector<Node> alpha[26]; 13 int main() { 14 int T; 15 cin >> T; 16 for (int t = 1; t <= T; ++t) { 17 for (int j = 0; j < 26; ++j) { 18 alpha[t].clear(); 19 } 20 int R, C; 21 cin >> R >> C; 22 Node temp; 23 for (int i = 0; i < R; ++i) { 24 cin >> str[i]; 25 for (int j = 0; j < C; ++j) { 26 temp.x = i, temp.y = j; 27 alpha[ str[i][j] - ‘A‘ ].push_back( temp ); 28 } 29 } 30 31 printf("Word search puzzle #%d:\n", t); 32 int n; 33 cin >> n; 34 string query; 35 for (int i = 0; i < n; ++i) { 36 cin >> query; 37 int cnt = alpha[ query[0] - ‘A‘ ].size(); 38 39 for (int j = 0; j < cnt; ++j) { 40 Node head = alpha[ query[0] - ‘A‘ ][j]; 41 // 向上查找 42 bool flag = true; 43 for (int e = (head.x-1+2*R)%R, f = 1; f < query.length(); e = (e-1+2*R)%R, ++f) { 44 if (query[f] != str[e][head.y]) { 45 flag = false; 46 break; 47 } 48 } 49 if (flag) { 50 printf("U %d %d ", head.x+1, head.y+1); 51 cout << query << endl; 52 break; 53 } 54 55 // 向下查找 56 flag = true; 57 for (int e = (head.x+1)%R, f = 1; f < query.length(); e = (e+1)%R, ++f) { 58 if (query[f] != str[e][head.y]) { 59 flag = false; 60 break; 61 } 62 } 63 if (flag) { 64 printf("D %d %d ", head.x+1, head.y+1); 65 cout << query << endl; 66 break; 67 } 68 69 // 向左查找 70 flag = true; 71 for (int e = (head.y-1+2*C)%C, f = 1; f < query.length(); e = (e-1+2*C)%C, ++f) { 72 if (query[f] != str[head.x][e]) { 73 flag = false; 74 break; 75 } 76 } 77 if (flag) { 78 printf("L %d %d ", head.x+1, head.y+1); 79 cout << query << endl; 80 break; 81 } 82 83 // 向右查找 84 flag = true; 85 for (int e = (head.y+1)%C, f = 1; f < query.length(); e = (e+1)%C, ++f) { 86 if (query[f] != str[head.x][e]) { 87 flag = false; 88 break; 89 } 90 } 91 if (flag) { 92 printf("R %d %d ", head.x+1, head.y+1); 93 cout << query << endl; 94 break; 95 } 96 } 97 } 98 cout << endl; 99 } 100 101 return 0; 102 }
标签:printf struct name cout 建立 tor push flag 包含
原文地址:https://www.cnblogs.com/ZhengQC/p/12593613.html