LeetCode-79.Word Search

来源:互联网 发布:时时彩彩票精准算法 编辑:程序博客网 时间:2024/05/17 10:39

https://leetcode.com/problems/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.

使用递归依次判断四个方向即可

代码使用的两个小技巧

1、为了省去边界的判断,在原始数组外嵌套一层

2、使用hashset存放访问过的位置(i*n+j)

public class Solution {    public bool Exist(char[,] board, string word)     {        int m = (int)board.GetLongLength(0);        int n = (int)board.GetLongLength(1);        char[,] map = new char[m + 2, n + 2];        for (int i = 0; i < m; i++)            for (int j = 0; j < n; j++)                map[i + 1, j + 1] = board[i, j];        for (int i = 1; i <= m; i++)        {            for (int j = 1; j <=n; j++)            {                if (map[i, j] == word[0])                {                    HashSet<int> set = new HashSet<int>();                    set.Add(i * (n+2) + j);                    Func(map, word, set, 1, i, j);                    if (set.Count == word.Length)                        return true;                }            }        }        return false;    }        private void Func(char[,] map, string word, HashSet<int> set, int index, int i, int j)    {        int n = (int)map.GetLongLength(1);        if (index == word.Length)            return;        if (map[i, j - 1] == word[index]&&!set.Contains(i*n+j-1))        {            set.Add(i * n + j-1);            Func(map, word, set, index + 1, i, j - 1);            if (set.Count == word.Length)                return;            set.Remove(i * n + j - 1);        }        if (map[i-1, j] == word[index] && !set.Contains(i * n + j - n))        {            set.Add(i * n + j - n);            Func(map, word, set, index + 1, i-1, j);            if (set.Count == word.Length)                return;            set.Remove(i * n + j -n);        }        if (map[i, j + 1] == word[index] && !set.Contains(i * n + j + 1))        {            set.Add(i * n + j + 1);            Func(map, word, set, index + 1, i, j + 1);            if (set.Count == word.Length)                return;            set.Remove(i * n + j + 1);        }        if (map[i + 1, j] == word[index] && !set.Contains(i * n + j + n))        {            set.Add(i * n + j + n);            Func(map, word, set, index + 1, i + 1, j);            if (set.Count == word.Length)                return;            set.Remove(i * n + j + n);        }    }}

优化

1、每次只判断新word的第一个字符,新word=旧的word去掉第一个字符

2、将board访问过的替换成特殊字符#,取代hashset

public class Solution {    public bool Exist(char[,] board, string word)     {        int m = (int)board.GetLongLength(0);        int n = (int)board.GetLongLength(1);        char[,] map = new char[m + 2, n + 2];        for (int i = 0; i < m; i++)            for (int j = 0; j < n; j++)                map[i + 1, j + 1] = board[i, j];        for (int i = 1; i <= m; i++)        {            for (int j = 1; j <=n; j++)            {                if (map[i, j] == word[0])                {                    map[i, j] = '0';                    if (Func(map, word.Substring(1),i, j))                        return true;                    map[i, j] = word[0];                }            }        }        return false;    }        private bool Func(char[,] map, string word,int i, int j)    {        if (word=="")            return true;        if (map[i, j - 1] == word[0])        {            map[i, j - 1] = '0';            if(Func(map, word.Substring(1), i, j - 1))                return true;            map[i, j - 1] = word[0];        }        if (map[i-1, j] == word[0])        {            map[i - 1, j] = '0';            if(Func(map, word.Substring(1), i-1, j))                return true;            map[i - 1, j] = word[0];        }        if (map[i, j + 1] == word[0])        {            map[i, j + 1] = '0';            if(Func(map, word.Substring(1), i, j + 1))                return true;            map[i, j + 1] = word[0];        }        if (map[i + 1, j] == word[0])        {            map[i + 1, j] = '0';            if(Func(map, word.Substring(1), i + 1, j))                return true;            map[i + 1, j] = word[0];        }        return false;    }}


0 0