Leetcode#79 Word Search
依次枚举起始点,DFS+回溯
代码:
bool dfs(vector<vector<char> > &board, int r, int c, string word) {
int m = board.size();
int n = board[].size();
int dir[][] = {{-, }, {, }, {, -}, {, }};
if (word.empty())
return true;
if (r >= && r < m && c >= && c < n && board[r][c] == word[]) {
for (int i = ; i < ; i++) {
char tmp = board[r][c];
board[r][c] = ;
if (dfs(board, r + dir[i][], c + dir[i][], word.substr()))
return true;
board[r][c] = tmp;
}
}
return false;
}
bool exist(vector<vector<char> > &board, string word) {
if (board.empty() || board[].empty()) return false;
int m = board.size();
int n = board[].size();
for (int i = ; i < m; i++)
for (int j = ; j < n; j++)
if (dfs(board, i, j, word))
return true;
return false;
}
Leetcode#79 Word Search的更多相关文章
- [LeetCode] 79. Word Search 单词搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- [LeetCode] 79. Word Search 词语搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- LeetCode 79. Word Search(单词搜索)
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- LeetCode 79 Word Search(单词查找)
题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...
- LeetCode 79. Word Search单词搜索 (C++)
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...
- [LeetCode] 212. Word Search II 词语搜索 II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 刷题79. Word Search
一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...
随机推荐
- UI4_UITableViewEdit
// // AppDelegate.m // UI4_UITableViewEdit // // Created by zhangxueming on 15/7/13. // Copyright (c ...
- [javascript|基本概念]学习笔记
1/语法 a.区分大小写 b.标识符(首字符必须是字母/"_"/"$",其他可为字母/"_"/"$"/数字,不能用关键字 ...
- jQuery 手风琴侧边菜单
动手做了一个简单手风琴菜单,上图: 点击 B 可收缩 C 列表,点击 C 改变自身和父节点 B 的样式,悬浮时均有不同的样式改变. 先看页面代码,列表的嵌套: <div id="men ...
- SUID或SGID程序中能不能用system函数
system()函数的声明和说明如下: 注意它的描述那里,system()执行一个由command参数定义的命令,通过调用/bin/sh -c命令来实现这个功能.也就是说它的逻辑是这样的! 进程调用s ...
- linux查看硬件信息
1,查看CPU信息:cat /proc/cpuinfo2,查看板卡信息:cat /proc/pci3,查看USB设备:cat /proc/bus/usb/devices4,查看PCI信息:lspci ...
- Silverlight中嵌套html、swf、pdf
1.MainPage.xaml <UserControl x:Class="SilverlightClient.MainPage" xmlns="http://sc ...
- Unity3d 动态批处理的问题
这段时间做unity3d的优化,主要的入手是减少draw call. 1.代码上主要是把一些零碎的同材质的合并成一个大的mesh. 2.减少不必要的全屏后期处理.把摄像机的renderin ...
- PHP弱类型安全问题笔记
一.类型转换问题 intval(); var_dump(intval('1asdfasd')); //1 var_dump(intval('awqw12')); //0 var_dump(i ...
- struts2结构图
- openerp 经典收藏 通过view实现字段的只读、隐藏操作(转载)
通过view实现字段的只读.隐藏操作 原文地址:http://cn.openerp.cn/view_groups/ 在OpenERP V7视图(ir.ui.view)多了一个非常有用的字段(group ...