开心一下
大早上做了一道题,题目与之前的类似,就当温习了,有趣的事情来了
2021/11/15
1class Solution {
2public:
3 bool exist(vector<vector<char>>& board, string word) {
4 int row = board.size(),col = board[0].size();
5 for(int i = 0 ; i < row ; ++i){
6 for(int j = 0 ; j < col ; ++j){
7 if(dfs(board,word,&i,&j,0,&row,&col))return true;
8 }
9 }
10 return false;
11 }
12 bool dfs(vector<vector<char>>& board, string word,int* x,int* y,int key,int* row,int* col){
13 if(board[*x][*y] != word[key])return false;
14 if(key == word.size()-1)return true;
15 char visited = board[*x][*y];
16 board[*x][*y] = '*';
17 vector<pair<int,int>> location ={{0,1},{0,-1},{1,0},{-1,0}};
18 for(auto& loc : location){
19 int dx = *x + loc.first,dy = *y + loc.second;
20 if(dx < 0 || dx >= *row || dy < 0 || dy >= *col || board[dx][dy] == '*')continue;
21 if(dfs(board,word,&dx,&dy,key+1,&*row,&*col)) return true;
22 //if(dfs(board,word,&dx,&dy,key+1,row,col)) return true;
23 }
24 board[*x][*y] = visited;
25 return false;
26 }
27};
看这一行代码
1//if(dfs(board,word,&dx,&dy,key+1,&*row,&*col)) return true;
2if(dfs(board,word,&dx,&dy,key+1,row,col)) return true;
当我为 &*row,&*col
这代码觉得不错的时候,突然意识到我真是傻逼!
我的开心谁能懂,😂
吾心信其可行,
则移山填海之难,
终有成功之日!
——孙文
则移山填海之难,
终有成功之日!
——孙文
评论
0 评论