79. 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 =

[
  ['A','B','C','E'],
  ['S','F','C','S'],
  ['A','D','E','E']
]
word  =  "ABCCED" , -> returns  true ,
word  =  "SEE" , -> returns  true ,

word = "ABCB", -> returns false.

DFS算法

public class Solution {
    char [][] b;
    private boolean dfs(String word,int n,int [][] list,int i,int j){
        if(word.length()==n)return true;
        if(i-1>=0){
            // if(j-1>=0){
            //     if(list[i-1][j-1]==0&&word.charAt(n)==b[i-1][j-1]){
            //         list[i-1][j-1]=1;
            //         if(dfs(word,n+1,list,i-1,j-1)==true)return true;
            //         list[i-1][j-1]=0;
            //     }
            // }
            if(list[i-1][j]==0&&word.charAt(n)==b[i-1][j]){
                    list[i-1][j]=1;
                    if(dfs(word,n+1,list,i-1,j)==true)return true;
                    list[i-1][j]=0;
            }
            // if(j+1<list[0].length){
            //     if(list[i-1][j+1]==0&&word.charAt(n)==b[i-1][j+1]){
            //         list[i-1][j+1]=1;
            //         if(dfs(word,n+1,list,i-1,j+1)==true)return true;
            //         list[i-1][j+1]=0;
            //     }
            // }
        }
        
            if(j-1>=0){
                if(list[i][j-1]==0&&word.charAt(n)==b[i][j-1]){
                    list[i][j-1]=1;
                    if(dfs(word,n+1,list,i,j-1)==true)return true;
                    list[i][j-1]=0;
                }
            }
            // if(list[i][j]==0&&word.charAt(n)==b[i][j]){
            //         list[i][j]=1;
            //         if(dfs(word,n+1,list,i,j)==true)return true;
            //         list[i][j]=0;
            // }
            if(j+1<list[0].length){
                if(list[i][j+1]==0&&word.charAt(n)==b[i][j+1]){
                    list[i][j+1]=1;
                    if(dfs(word,n+1,list,i,j+1)==true)return true;
                    list[i][j+1]=0;
                }
            }
        if(i+1<list.length){
            // if(j-1>=0){
            //     if(list[i+1][j-1]==0&&word.charAt(n)==b[i+1][j-1]){
            //         list[i+1][j-1]=1;
            //         if(dfs(word,n+1,list,i+1,j-1)==true)return true;
            //         list[i+1][j-1]=0;
            //     }
            // }
            if(list[i+1][j]==0&&word.charAt(n)==b[i+1][j]){
                    list[i+1][j]=1;
                    if(dfs(word,n+1,list,i+1,j)==true)return true;
                    list[i+1][j]=0;
            }
            // if(j+1<list[0].length){
            //     if(list[i+1][j+1]==0&&word.charAt(n)==b[i+1][j+1]){
            //         list[i+1][j+1]=1;
            //         if(dfs(word,n+1,list,i+1,j+1)==true)return true;
            //         list[i+1][j+1]=0;
            //     }
            // }
        }
        return false;
    }
    public boolean exist(char[][] board, String word) {
        // if(word.equals("aaaaaaaaaaaaaaaaaaaa"))return false;
        int [][] list = new int [board.length][board[0].length];
        b=board;
        for(int i = 0;i<b.length;i++){
            for(int j = 0;j<b[0].length;j++){
                if(b[i][j]==word.charAt(0)){
                    list[i][j]=1;
                    if(dfs(word,1,list,i,j)==true)return true;
                    list[i][j]=0;
                }
            }
        }
        return false;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图像识别技术在病虫害检测中的应用是一个快速发展的领域,它结合了计算机视觉和机器学习算法来自动识别和分类植物上的病虫害。以下是这一技术的一些关键步骤和组成部分: 1. **数据收集**:首先需要收集大量的植物图像数据,这些数据包括健康植物的图像以及受不同病虫害影响的植物图像。 2. **图像预处理**:对收集到的图像进行处理,以提高后续分析的准确性。这可能包括调整亮度、对比度、去噪、裁剪、缩放等。 3. **特征提取**:从图像中提取有助于识别病虫害的特征。这些特征可能包括颜色、纹理、形状、边缘等。 4. **模型训练**:使用机器学习算法(如支持向量机、随机森林、卷积神经网络等)来训练模型。训练过程中,算法会学习如何根据提取的特征来识别不同的病虫害。 5. **模型验证和测试**:在独立的测试集上验证模型的性能,以确保其准确性和泛化能力。 6. **部署和应用**:将训练好的模型部署到实际的病虫害检测系统中,可以是移动应用、网页服务或集成到智能农业设备中。 7. **实时监测**:在实际应用中,系统可以实时接收植物图像,并快速给出病虫害的检测结果。 8. **持续学习**:随着时间的推移,系统可以不断学习新的病虫害样本,以提高其识别能力。 9. **用户界面**:为了方便用户使用,通常会有一个用户友好的界面,显示检测结果,并提供进一步的指导或建议。 这项技术的优势在于它可以快速、准确地识别出病虫害,甚至在早期阶段就能发现问题,从而及时采取措施。此外,它还可以减少对化学农药的依赖,支持可持续农业发展。随着技术的不断进步,图像识别在病虫害检测中的应用将越来越广泛。
这段代码存在一些问题: 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值