word search

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/freedom098/article/details/79493779

leetcode 第79题,图上的深搜题。
第一步,先要写个遍历循环,找到搜索的起点,也就是首字母。
第二步,从起点出发开始搜索,搜索时主要要记录已经走过的地方,这里可以使用一个二维的数组单独来记录,或者走过的地方标识为‘#’,搜索后恢复这个位置的字符。
注意,每次搜索时,都要比较word和当前路径已经遍历到的字符,这里可以使用增量式的比较方法,每次比较一个字符即可。

class Solution:
    def exist(self, board, word):
        """
        :type board: List[List[str]]
        :type word: str
        :rtype: bool
        """
        row = len(board)
        col = len(board[0])

        def dfs(current_x, current_y, board, index, word):

            if index == len(word)-1:
                return True
            tmp = board[current_x][current_y]
            board[current_x][current_y] = '#'
            if current_x+1 < row and board[current_x+1][current_y] == word[index+1]:
                if dfs(current_x+1, current_y, board, index+1, word):
                    return True
            if current_x-1 >= 0 and board[current_x-1][current_y] == word[index+1]:
                if dfs(current_x-1, current_y, board, index+1, word):
                    return True
            if current_y+1 < col and board[current_x][current_y+1] == word[index+1]:
                if dfs(current_x, current_y+1, board, index+1, word):
                    return True 
            if current_y-1 >= 0 and board[current_x][current_y-1] == word[index+1]:
                if dfs(current_x, current_y-1, board, index+1, word):
                    return True
            board[current_x][current_y] = tmp

            return False

        for i in range(row):
            for j in range(col):
                if board[i][j] == word[0]:
                    if dfs(i, j, board, 0, word):
                        return True
        return False
展开阅读全文

c word search segmentation fault

02-28

[code=C/C++][/code]rnrn 在putty 下用 ./square < data1 运行 从data1中的字母矩阵和单词从找出所给单词,横向纵向斜向正反都行。rnrnrn输入文件如下rnrnS T E L B M T F E Y D E E P S R T C I A E E (后一个空格加转行符)rnN N E L I T R L B D E T A R E M U N E T Y LrnN O I T A N I M I R C N I F L E S S E N T ArnA U I D E W A R R A N T N U P R U S S E R PrnP G S G E A L P A P B A N P S A S S N M E ArnC O N S I T U T I O N D E E C W S O O H P DrnS V W D E L A N E E J A M E S M A D I S O NrnA E D E S N E G R J C U L T N O H L T I R ArnA R C E R R T R E E S B O N E E I D N N P RrnS N J U D I C I A L A S S E C O R P E U D IrnS M R A R A E B W B E S S M E O A U V P E MrnO E O I A I L N O U C D O D S S E N N I G RrnL N I D G Y T R C O M P E N S A T I O N N DrnD T O Z E H P Y N D R L E E A O H S C O I BrnI T P S U E T G O L U Z M M R B E H P I R TrnE O I E A R R S U U I B H A Y L L M S T F ArnR I N R E E E F U T L V Q U A R T E R I N GrnS I D B S R R D I Y E N I G M I A N A T I RrnS Q I S E B S C N S P E E C H R O T A E Y NrnD L C M I L I T I A F L R N C A T S S P S ErnR U T E D Y L E B I L C O H M L E T E S Y YrnL S T R T E W Z L I O S A E N S A E I Y A LrnAMENDMENTrnASSEMBLYrnBAILrnBEARARMSrnCITIZENrnCIVILrnCOMPENSATIONrnCONGRESSrnCONSITUTIONrnCONVENTIONSrnDELEGATEDrnDOUBLEJEOPARDYrnDUEPROCESSrnENUMERATEDrnFREEDOMrnGOVERNMENTrnILLEGALrnINDICTrnINFRINGEDrnJAMESMADISONrnJUDICIALrnLAWSUITrnLIBELrnLIBERTYrnLIFErnMILITIArnMIRANDArnNECESSARYrnPEACEABLYrnPEERSrnPETITIONrnPOWERrnPRESSrnPROBABLECAUSErnPROPERTYrnPUNISHMENTSrnQUARTERINGrnRELIGIONrnRIGHTSrnSEARCHrnSECURITYrnSEIZURErnSELFINCRIMINATIONrnSLANDERrnSOLDIERSrnSPEECHrnSPEEDYrnTRIALrnUNREASONABLErnWARRANTrnWITNESSrnrn代码如下:rn#includern#includern#includern#define M 50rn#define S 20rnrnint mark[M][M]=0;rnrnint getData(char matr[][M],int *size);rnint isit(char matr[][M],int r,int c, int rd, int cd,char buf[S]);rnvoid printWord(char matr[][M],int *size);rnrnvoid printWord(char matr[][M], int *size)rn int i,j;rn for(i=0;i<*size;i++)rn for(j=0;j<*size;j++)rn if (mark[i][j]==1)rn printf("%c",matr[i][j]);rn elsern printf("%c",'0');rn rn printf("%c",'\n');rn rnrnrnrnint isit(char matr[][M],int r,int c, int rd, int cd,char buf[S])rnrn int i,j;rn int length=strlen(buf);rn int step=0;rn while(steplength || r>length )rn return 0;rn else if (matr[r][c]!=buf[step])rn return 0;rn elsern r+=rd;rn c+=cd;rn step++;rn printf("%d",step);rn rn rn if (step==length)rn mark[r][c]=1;rn for(i=0;i 论坛

search

05-28

我想從數據庫中同時查找符合幾個條件的記錄,而且黨有一個條件沒有填的時候就查找符合除了這個條件之外的條件。即要實現綜合查詢,代碼如下所示:rn rn rn Customer Name : rn ">rn Please selectrn <%rnWhile (NOT customer.EOF)rn%>rn "><%=(customer.Fields.Item("Account").Value)%>rn <%rn customer.MoveNext()rnWendrnIf (customer.CursorType > 0) Thenrn customer.MoveFirstrnElsern customer.RequeryrnEnd Ifrn%>rn rn rn rn rn rn rn rn Supplier Name:rn rn Please selectrn <%rnWhile (NOT supplier.EOF)rn%>rn<%if (supplier.Fields.Item("supplier").Value)<>"" then%>rn "><%=(supplier.Fields.Item("supplier").Value)%><%end if%>rn <%rn supplier.MoveNext()rnWendrnIf (supplier.CursorType > 0) Thenrn supplier.MoveFirstrnElsern supplier.RequeryrnEnd Ifrn%>rn rn rn rn rn rn rn Part Number:rn rn rn rn rn rn rn PO input date:rn rn rn rn rn rn rn Waiting:rn rn rn rn Yesrn rn rn Norn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn 結果頁面的查詢語句:rn<%rnDim Recordset1__MMColParamrnRecordset1__MMColParam = "%"rnIf (Session("customer") <> "") Then rn Recordset1__MMColParam = Session("customer") rnEnd Ifrn%>rn<%rnDim Recordset1__MMColParam1rnRecordset1__MMColParam1 = "%"rnIf (Session("supplier") <> "") Then rn Recordset1__MMColParam1 = Session("supplier")rnEnd Ifrn%>rn<%rnDim Recordset1__MMColParam2rnRecordset1__MMColParam2 = "%"rnIf (Session("part") <> "") Then rn Recordset1__MMColParam2 = Session("part")rnEnd Ifrn%>rn<%rnDim Recordset1__MMColParam4rnRecordset1__MMColParam4 = "2"rnIf (Session("waiting") <> "") Then rn Recordset1__MMColParam4 = Session("waiting") rnEnd Ifrn%>rn<%rnDim Recordset1__MMColParam3rnRecordset1__MMColParam3 = "1900/01/02"rnIf (Session("podate") <> "") Then rn Recordset1__MMColParam3 = Session("podate") rnEnd Ifrn%>rnrnSELECT *rnFROM dbo.txtdatarnWHERE (customer = 'MMColParam' and supplier='MMColParam1' and Orderinputdate='MMColParam3' and partNo='MMColParam2' and response='MMColParam4')rnrn可是不知道為什麼找不到記錄,黨and把改成or後可以找到一部分記錄,但是,不是符合條件的。 论坛

Crazy Search

09-03

Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could be finding a hidden prime number in a given text. Such number could be the number of different substrings of a given size that exist in the text. As you soon will discover, you really need the help of a computer and a good algorithm to solve such a puzzle.nnYour task is to write a program that given the size, N, of the substring, the number of different characters that may occur in the text, NC, and the text itself, determines the number of different substrings of size N that appear in the text.nnAs an example, consider N=3, NC=4 and the text "daababac". The different substrings of size 3 that can be found in this text are: "daa", "aab", "aba", "bab", "bac". Therefore, the answer should be 5.nnnInputnnThe first line of input consists of two numbers, N and NC, separated by exactly one space. This is followed by the text where the search takes place. You may assume that the maximum number of substrings formed by the possible set of characters does not exceed 16 Millions.nnnOutputnnThe program should output just an integer corresponding to the number of different substrings of size N found in the given text.nnnThis problem contains multiple test cases!nnThe first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.nnThe output format consists of N output blocks. There is a blank line between output blocks.nnnSample Inputnn1nn3 4ndaababacnnnSample Outputnn5 问答

没有更多推荐了,返回首页