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

一、题目

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

SLANQIAO
ZOEXCCGB
MOAYWKHI
BCCIPLJQ
SLANQIAO
RSFWFNYA
XIFZVWAL
COAIQNAL

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

二、cpp解题

一个位置的空间搜索

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

  • 行方向的搜索: 搜索向右+n(搜索单词长度),查看正序和倒序是否和搜索结果一致
  • 列方向的搜索: 搜索向下+n(搜索单词长度),查看正序和倒序是否和搜索结果一致
  • 右上方向搜索: 搜索右上+n(搜索单词长度),查看正序和倒序是否和搜索结果一致
  • 右下方向搜索: 搜索右下+n(搜索单词长度),查看正序和倒序是否和搜索结果一致
#include <iostream>
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)   
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Scc_hy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值