LeetCode OJ Word Search

本文介绍了一种用于在二维网格中查找指定单词的算法。通过相邻单元格的顺序连接构建单词,确保同一字母单元格仅使用一次。具体实现了搜索函数,包括初始化变量、方向数组、标记矩阵和访问矩阵等,确保高效地定位目标单词。

Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

For example,
Given board =

[
  ["ABCE"],
  ["SFCS"],
  ["ADEE"]
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,

word = "ABCB", -> returns false.

class Solution {
public:
    bool exist(vector<vector<char> > &board, string word) {
        if (board.size() == 0 || (board.size() * board[0].size() < word.size())) {
            return false;
        }
        dir[0][0] = -1;
        dir[0][1] = 0;
        dir[1][0] = 1;
        dir[1][1] = 0;
        dir[2][0] = 0;
        dir[2][1] = 1;
        dir[3][0] = 0;
        dir[3][1] = -1;
        ans = false;
        
        t_length = word.size();
        target = new char[word.size()];
        for (int i = 0; i < word.size(); i++) target[i] = word[i];
        
        h = board.size();
        w = board[0].size();
        
        mapp = new char*[h];
        for (int i = 0; i < h; i++) mapp[i] = new char[w];
        
        vis = new short*[h];
        for (int i = 0; i < h; i++) vis[i] = new short[w];
        
        
        for (int ii = 0; ii < h; ii++) {
            for (int jj = 0; jj < w; jj++) {
                mapp[ii][jj] = board[ii][jj];
                vis[ii][jj] = 0;
            }
        }
        
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (board[i][j] == word[0]) {
                    
                    vis[i][j] = 1;
                    search(1, i, j);
                    if (ans) {
                        return ans;
                    }
                    vis[i][j] = 0;
                }
            }
        }
        
        for (int i = 0; i < h; i++) delete []mapp[i];
        delete []mapp;
        
        for (int i = 0; i < h; i++) delete []vis[i];
        delete []vis;
        
        return ans;
    }
    void search(int pos, int i, int j) {
        if (pos == t_length) {
            ans = true;
            return;
        }
        for (int k = 0; k < 4 && ans == false; k++) {
            if (0 <= i + dir[k][0] && i + dir[k][0] < h && 0 <= j + dir[k][1] && j + dir[k][1] < w && (!vis[i + dir[k][0]][j + dir[k][1]]) && (target[pos] == mapp[i + dir[k][0]][j + dir[k][1]])) {
                vis[i + dir[k][0]][j + dir[k][1]] ^= 1;
                search(pos + 1, i + dir[k][0], j + dir[k][1]);
                vis[i + dir[k][0]][j + dir[k][1]] ^= 1;
            }
        }
    }
private:
    int dir[4][2];
    int w, h;
    short **vis;
    char *target;
    int t_length;
    char **mapp;
    bool ans;
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值