Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 以int数组保存方向的Wordsearch_C++ - Fatal编程技术网

C++ 以int数组保存方向的Wordsearch

C++ 以int数组保存方向的Wordsearch,c++,C++,我正试图开发一个搜索词,在保存方向(8)的整数一维数组中查找单词“OIE”(表示出现了多少次),但运行该搜索时会出现奇怪的错误(以及不正确的输出) 代码如下: int arrf[8] = {0, -1, -1, -1, 0, 1, 1, 1}; int arrc[8] = {-1, -1, 0, 1, 1, 1, 0,-1}; char s[] = "OIE"; int main() { int n, m; while (cin >> n >>

我正试图开发一个搜索词,在保存方向(8)的整数一维数组中查找单词“OIE”(表示出现了多少次),但运行该搜索时会出现奇怪的错误(以及不正确的输出)

代码如下:

    int arrf[8] = {0, -1, -1, -1, 0, 1, 1, 1};
int arrc[8] = {-1, -1, 0, 1, 1, 1, 0,-1};
char s[] = "OIE";

int main() {
    int n, m;
    while (cin >> n >> m) {
        int res = 0;
        vector<vector<char> > S(n, vector<char>(m));
        for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) cin >> S[i][j];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                for (int d = 0; d < 8; ++d) {
                    bool trobat = true;
                    for (int h = 0; h < 3 and trobat; ++h) {
                        int f = i + arrf[d], c = j + arrc[d];
                        if (f < 0 || f >= n || c < 0 || c >= m || S[f][c] != s[h])
                            trobat = false;
                    }
                    if (trobat) res++;
                }
            }
        }
        cout << res << endl;
    }
}
intarrf[8]={0,-1,-1,0,1,1,1};
int arrc[8]={-1,-1,0,1,1,1,0,-1};
字符s[]=“OIE”;
int main(){
int n,m;
而(cin>>n>>m){
int res=0;
向量S(n,向量(m));
对于(inti=0;i>S[i][j];
对于(int i=0;i=n | c<0 | c>=m | S[f][c]!=S[h])
trobat=假;
}
if(trobat)res++;
}
}
}

一个错误是这条线

int f = i + arrf[d], c = j + arrc[d];
应该是

int f = i + h*arrf[d], c = j + h*arrc[d];

对于你的代码来说,不管你绕了多少圈,你仍然在检查同一个位置。

你得到了什么错误?你为什么要用“或”和“和”这个词而不是它们的布尔对应项?@bwtrent我想你指的是逻辑对应项。@bwtrent我猜是外置键盘。在这种情况下,当单词多次出现时,所有输出都是0。例如,尝试输入:[2 OIEEIO]或任何其他输入。非常感谢你的回答。晚安。问候:-)