算法技能树——字母矩阵(动态数组)

一、题目

仔细寻找,会发现:在下面的8x8的方阵中,隐藏着字母序列:“LANQIAO”。

SLANQIAO
ZOEXCCGB
MOAYWKHI
BCCIPLJQ
SLANQIAO
RSFWFNYA
XIFZVWAL
COAIQNAL

我们约定: 序列可以水平,垂直,或者是斜向;并且走向不限(实际上就是有一共8种方向)。
上面一共有4个满足要求的串。

二、cpp解题

一个位置的空间搜索

总共存在8个方向,但是其中两两是相反的,所以我们有四种模式的搜索:

  • 行方向的搜索: 搜索向右+n(搜索单词长度),查看正序和倒序是否和搜索结果一致
  • 列方向的搜索: 搜索向下+n(搜索单词长度),查看正序和倒序是否和搜索结果一致
  • 右上方向搜索: 搜索右上+n(搜索单词长度),查看正序和倒序是否和搜索结果一致
  • 右下方向搜索: 搜索右下+n(搜索单词长度),查看正序和倒序是否和搜索结果一致
#include 
using namespace std;

int spFind(char **arr, int st_row, int st_col, int max_row, int max_col){
    // one row-find 
    int cnt = 0, pos_idx=0, neg_idx=6;
    char judge[] = {'L', 'A', 'N', 'Q', 'I', 'A', 'O'};
    if(st_col + 7 <= max_col){
        for(int i=st_col; i < max_col && i < st_col+7; i++){
            if(arr[st_row][i] == judge[pos_idx]) pos_idx++;
            if(pos_idx == 7){
                cnt++;
                pos_idx=0;
            }
            if(arr[st_row][i] == judge[neg_idx]) neg_idx--;
            if(neg_idx == -1){
                cnt++;
                neg_idx = 6;
            }
        }
    }
    pos_idx=0, neg_idx=6;
    // one col-find 
    if(st_row + 7 <= max_row){
        for(int i=st_row; i < max_row && i < st_row+7; i++){
            if(arr[i][st_col] == judge[pos_idx]) pos_idx++;
            if(pos_idx == 7){
                cnt++;
                pos_idx = 0;
            }
            if(arr[i][st_col] == judge[neg_idx]) neg_idx--;
            if(neg_idx == -1){
                cnt++;
                neg_idx = 6;
            }
        }
    }
    pos_idx=0, neg_idx=6;
    // right-down 
    if(st_row + 7 <= max_row && st_col + 7 <= max_col){
        for(int i=st_row, j=st_col; \
            i < max_row && j < max_col && i < st_row + 7 && j < st_col + 7;\
            i++, j++){
            if(arr[i][j] == judge[pos_idx]) pos_idx++;
            if(pos_idx == 7){
                cnt++;
                pos_idx=0;
            }
            if(arr[i][j] == judge[neg_idx]) neg_idx--;
            if(neg_idx == -1){
                cnt++;
                neg_idx = 6;
            }
        }
    }
    pos_idx=0, neg_idx=6;
    // right-up 
    if(st_col + 7 <= max_col && st_row - 7 >= 0){
        for(int i=st_row, j=st_col; \
            i > 0 && j < max_col && i > st_row - 7 && j < st_col + 7;\
            i--, j++){
            if(arr[i][j] == judge[pos_idx]) pos_idx++;
            if(pos_idx == 7){
                cnt++;
                pos_idx=0;
            }
            if(arr[i][j] == judge[neg_idx]) neg_idx--;
            if(neg_idx == -1){
                cnt++;
                neg_idx = 6;
            }
        }
    }
    return cnt;
}

动态数组


int main(){
    int n, m;
    cin >> n;
    m = n;
    // 动态数组定义
    char **str_matrix = new char*[n];
    for(int i=0; i < n; i++) str_matrix[i] = new char[m];
    int out = 0;
    for(int i=0; i < n; i++){
        for(int j=0; j < n; j++){
            cin >> str_matrix[i][j];
        }
    }
    for(int i=0; i < n; i++){
        for(int j=0; j < n; j++){
            out += spFind(str_matrix,  i, j, n, n);
        }
    }
    cout << out << endl;
    // 动态数组释放
    for(int i=0; i < n; i++) delete [] str_matrix[i];
    delete [] str_matrix;
    return 0;
}

三、python解题



def sp_find(arr, st_row, st_col, max_row, max_col):
    # one row-find 
    cnt = 0
    pos_idx=0
    neg_idx=6
    judge = ['L', 'A', 'N', 'Q', 'I', 'A', 'O']
    if(st_col + 7 <= max_col):
        for i in range(st_col, st_col+7):
            if(arr[st_row][i] == judge[pos_idx]):
                pos_idx += 1
            if(pos_idx == 7):
                cnt += 1
                pos_idx=0
            if(arr[st_row][i] == judge[neg_idx]):
                neg_idx-=1
            if(neg_idx == -1):
                cnt += 1;
                neg_idx = 6;
    pos_idx=0
    neg_idx=6
    # one col-find 
    if(st_row + 7 <= max_row):
        for i in range(st_row, st_row + 7):
            if(arr[i][st_col] == judge[pos_idx]):
                pos_idx += 1
            if(pos_idx == 7):
                cnt += 1
                pos_idx = 0
            if(arr[i][st_col] == judge[neg_idx]):
                neg_idx-=1
            if(neg_idx == -1):
                cnt += 1;
                neg_idx = 6;
    pos_idx=0
    neg_idx=6
    # right-down 
    if(st_row + 7 <= max_row) and (st_col + 7 <= max_col):
        for i, j in zip(range(st_row, st_row+7), range(st_col, st_col+7)):
            if(arr[i][j] == judge[pos_idx]):
                pos_idx += 1
            if(pos_idx == 7):
                cnt += 1
                pos_idx = 0
            if(arr[i][j] == judge[neg_idx]):
                neg_idx-=1
            if(neg_idx == -1):
                cnt += 1;
                neg_idx = 6;
                
    pos_idx=0
    neg_idx=6
    # right-up 
    if(st_row - 7 >= 0) and (st_col + 7 <= max_col):
        for i, j in zip(range(st_row, st_row-7, -1), range(st_col, st_col+7)):
            if(arr[i][j] == judge[pos_idx]):
                pos_idx += 1
            if(pos_idx == 7):
                cnt += 1
                pos_idx = 0
            if(arr[i][j] == judge[neg_idx]):
                neg_idx-=1
            if(neg_idx == -1):
                cnt += 1
                neg_idx = 6
    return cnt



if __name__ == '__main__':
    str_matrix = []
    n = int(input('输入矩阵的大小(如 2 3 4)'))
    for i in range(n):
        a = input()
        str_matrix.append([i for i in a])


    out = 0
    for i in range(n):
        for j in range(n):
            out += sp_find(str_matrix,  i, j, n, n);

    print(out)   

你可能感兴趣的:(笔记,算法,矩阵,c++)