79. Word Search单词搜索

艾恩凝

 个人博客  https://aeneag.xyz/ 

论文不想看,就多做道算法题吧,回溯深度搜索

欢迎关注微信公众号 “技术乱舞”

目录

题目链接

分析

题解

欢迎关注公众号 “技术乱舞”

一起成长


题目链接

https://leetcode-cn.com/problems/word-search/

分析

首先选择第一个字母匹配的,然后开始进行深度遍历,以该节点位置开始从上下左右延伸,直到找到适配所有的

题解

class Solution {
public:
    bool exist(vector<vector<char>>& board, string word) {
        int row = board.size(),col = board[0].size();
        for(int i = 0 ; i < row ; ++i){
            for(int j = 0 ; j < col ; ++j){
                if(dfs(board,word,i,j,0,row,col))return true;
            }
        }
        return false;
    }
    bool dfs(vector<vector<char>>& board, string word,int x,int y,int key,int row,int col){
        if(board[x][y] != word[key])return false;
        if(key == word.size()-1) return true;
        vector<pair<int,int>> location = {{0,1},{0,-1},{1,0},{-1,0}};
        char visited = board[x][y];
        board[x][y] = '*';
        for(auto & dir : location){
            int dx = x + dir.first,dy = y + dir.second;
            if(dx < 0 || dx >= row || dy < 0 || dy >= col || board[dx][dy]=='*')continue;
            if(dfs(board,word,dx,dy,key+1,row,col))return true;
        }
        board[x][y] = visited;
        return false;
    }
};

欢迎关注公众号 “技术乱舞”

一起成长

                       ​​​​​​​      

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这段代码存在一些问题: 1. EnglishLearningApp 类没有定义完整,缺少 quiz_page 函数实现。 2. 在 word_consult 函数中,创建了一个名为 word_entry 的 Entry 对象,但是它没有被放置到任何容器中,应该使用 self.word_entry。 3. 在 add_word 函数中,使用了 search_entry.get(),但是没有定义 search_entry 对象,应该使用 self.word_entry.get()。 4. 在 add_word 函数中,使用了 cur.execute(),但是应该使用 self.cursor.execute()。 5. 在 add_word 函数中,应该把获取的 word 对象转换为字符串,即使用 word[0]。 6. 在 add_word 函数中,应该加入 try-except 结构,以处理插入或删除单词时可能发生的异常情况。 下面是修改后的代码: ``` import tkinter as tk import sqlite3 import random from tkinter import messagebox class EnglishLearningApp: def __init__(self, master): self.master = master master.title("英语学习软件") self.database_button = tk.Button(master, text="单词库", command=self.word_consult) self.database_button.pack(pady=10) self.quiz_button = tk.Button(master, text="英语默写", command=self.quiz_page) self.quiz_button.pack(pady=10) self.conn = sqlite3.connect('words.db') self.cursor = self.conn.cursor() self.cursor.execute('''CREATE TABLE IF NOT EXISTS words (word text PRIMARY KEY)''') self.conn.commit() def word_consult(self): self.word_entry = tk.Entry(self.master) self.word_entry.pack(padx=10, pady=10) self.add_button = tk.Button(self.master, text="添加/删除单词", command=self.add_word) self.add_button.pack(pady=5) def add_word(self): word = self.word_entry.get() if not word: return try: self.cursor.execute("SELECT * FROM data WHERE name=?", (word,)) word = self.cursor.fetchone() if word: self.cursor.execute("INSERT INTO words VALUES (?)", (str(word[0]),)) self.conn.commit() messagebox.showinfo("提示", "添加成功") else: self.cursor.execute("DELETE FROM words WHERE word=?", (word[0],)) self.conn.commit() messagebox.showinfo("提示", "删除成功") except Exception as e: messagebox.showerror("错误", str(e)) def quiz_page(self): pass if __name__ == '__main__': root = tk.Tk() app = EnglishLearningApp(root) root.mainloop() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

艾恩凝

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

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

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

打赏作者

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

抵扣说明:

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

余额充值