Leetcode79词搜索(深度遍历解)给定一个mxn的二维字符网格板和一个字符串单词word。如果单词存在于网格中,则返回真;否则,返回false。单词必须按字母顺序排列,通过相邻单元格中的字母,其中“相邻”单元格是水平或垂直相邻的单元格。同一单元格中的字母不允许重复使用。/***@param{character[][]}board*@param{string}word*@return{boolean}*/constpoint=[0,-1,0,1,0];varexist=function(board,word){//如果word中有字母没有出现在board中,直接返回false;conststr=[...newSet(board.flat())].join('');for(letchofword){if(!str.includes(ch))返回false;}constlenRow=board.length;constlenCol=board[0].length;constlength=word.length;functiondfs(i,j,step){if(step===length)returntrue//如果步数可以取length步,则returntrue;if(i<0||j<0||i>=lenRow||j>=lenCol||board[i][j]===0)返回falseconstcur=board[i][j]//如果当前字母与步骤的字母不一致,则返回false;if(cur!==word[step])返回false;board[i][j]=0letflag=false;for(letk=0;k<=3;k++){const[ni,nj]=[i+point[k],j+point[k+1]];标志||=dfs(ni,nj,step+1);//看四个方向是否至少有一个为真,可以结束;}board[i][j]=cur返回标志}constfirst=word[0];for(leti=0;i
