很简单的题目我又犯了很愚蠢的错误,行的越界检查写成列的了,我发现我写程序有一个问题,很多地方想当然的写,如果可以多想想很多简单的错误在写的时候应该就可以避免的。
#include <iostream> #include <sstream> #include <fstream> #include <string> #include <vector> #include <queue> #include <map> #include <set> #include <stack> #include <assert.h> #include <algorithm> #include <math.h> #include <ctime> #include <functional> #include <string.h> #include <stdio.h> #include <numeric> #include <float.h> using namespace std; const int MAXROW = 51, MAXCOL = 51; bool left(vector<string> &v, string &s, int r, int c) { if (s.size() > (c + 1)) return false; for (int i = 0; i < s.size(); i++) { if (s[i] != v[r][c - i]) return false; } return true; } bool right(vector<string> &v, string &s, int r, int c) { if (c + s.size() > v[0].size()) return false; for (int i = 0; i < s.size(); i++) { if (s[i] != v[r][c + i]) return false; } return true; } bool up(vector<string> &v, string &s, int r, int c) { if (s.size() >(r + 1)) return false; for (int i = 0; i < s.size(); i++) { if (s[i] != v[r-i][c]) return false; } return true; } bool down(vector<string> &v, string &s, int r, int c) { if (r + s.size() > v.size()) return false; for (int i = 0; i < s.size(); i++) { if (s[i] != v[r+i][c]) return false; } return true; } bool leftup(vector<string> &v, string &s, int r, int c) { if (s.size() >(c + 1) || s.size() > (r+1)) return false; for (int i = 0; i < s.size(); i++) { if (s[i] != v[r-i][c - i]) return false; } return true; } bool rightdown(vector<string> &v, string &s, int r, int c) { if (c + s.size() > v[0].size() || r + s.size() > v.size()) return false; for (int i = 0; i < s.size(); i++) { if (s[i] != v[r+i][c + i]) return false; } return true; } bool leftdown(vector<string> &v, string &s, int r, int c) { if (s.size() >(c + 1) || r + s.size() > v.size()) return false; for (int i = 0; i < s.size(); i++) { if (s[i] != v[r+i][c - i]) return false; } return true; } bool rightup(vector<string> &v, string &s, int r, int c) { if (s.size() > (r + 1) || c + s.size() > v[0].size()) return false; for (int i = 0; i < s.size(); i++) { if (s[i] != v[r-i][c + i]) return false; } return true; } pair<int, int> solution(vector<string> &v, string &s) { for (int i = 0; i < v.size(); i++) { for (int j = 0; j < v[i].size(); j++) { if (left(v, s, i, j)) return make_pair(i, j); if (right(v, s, i, j)) return make_pair(i, j); if (leftup(v, s, i, j)) return make_pair(i, j); if (leftdown(v, s, i, j)) return make_pair(i, j); if (rightup(v, s, i, j)) return make_pair(i, j); if (rightdown(v, s, i, j)) return make_pair(i, j); if (up(v, s, i, j)) return make_pair(i, j); if (down(v, s, i, j)) return make_pair(i, j); } } return make_pair(0, 0); } int main() { int TC = 0; cin >> TC; bool blank = false; for (int tc = 1; tc <= TC; tc++) { int m, n; cin >> m >> n; vector<string> v; for (int i = 0; i < m; i++) { string s; cin >> s; for (int j = 0; j < s.size(); j++) { s[j] = tolower(s[j]); } v.push_back(s); } if (blank) cout << endl; blank = true; int sn = 0; cin >> sn; for (int i = 0; i < sn; i++) { string s; cin >> s; for (int j = 0; j < s.size(); j++) { s[j] = tolower(s[j]); } pair<int, int> p = solution(v, s); cout << p.first + 1 << " " << p.second + 1 << endl; } } return 0; }