这道题 我是 用 map实现的
具体步骤如下:
-
首先定义结构为:map<pair<int,int>,string>[4]
key(pair<int,int>):对应为:坐标
value(string):对应的 该坐标的各个方向的 所练成的字符串
这里的数组【4】表示一个坐标四个方向(上、左、右上、左上)的所对应的字符串
-
在数据输入的时候 ,去判断该字符是否为 ‘y’、‘g’(分别对应yizhong 的首位 和最后一位 ),若条件成立 ,将数据 压入map中
最后在遍历一遍 map中的数据,得到相等的str,就记录坐标
最后将记录 过坐标的值打印即可
C++
#include<bits/stdc++.h> using namespace std; #define OBJ_STR “yizhong” #define OBJ_STR_REVERSE “gnohziy” bool isShow[101][101]; int main() { map<pair<int, int>, string>data[4]; int dir[4][2] = { { 0,-1 },{ -1,0 },{ 1,1 },{ -1,-1 } }; //上 、左 、右上 、左上 char wordTable[101][101] = { 0 }; int n; cin >> n; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { cin >> wordTable[i][j]; if (wordTable[i][j] == ‘y’ || wordTable[i][j] == ‘g’) for (int k = 0; k < 4; data[k++][pair<int, int>(j, i)].push_back(wordTable[i][j])); for (int k = 0; k < 4; ++k) { if (!data[k][pair<int, int>(j + dir[k][0], i + dir[k][1])].empty()) { data[k][pair<int, int>(j, i)] = data[k][pair<int, int>(j + dir[k][0], i + dir[k][1])]; data[k][pair<int, int>(j, i)].push_back(wordTable[i][j]); } } } for (int i = 0; i < 4; ++i) for (auto itor = data[i].begin(); itor != data[i].end(); ++itor) { if (itor->second.empty()||itor->second.size()<7)continue; if (itor->second.substr(itor->second.size()- 7) == OBJ_STR || itor->second.substr(itor->second.size()- 7) == OBJ_STR_REVERSE) for (int x = itor->first.first, y = itor->first.second, j = 0; j < 7; ++j, isShow[y][x] = 1, x += dir[i][0], y += dir[i][1]); } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; printf(“%c”,(isShow[i][j]==true)?wordTable[i][j]:’*’), j++); cout << endl; } return 0; }