《劍指offer》66:矩陣中的路徑

題目:請設計一個函數,用來判斷在一個矩陣中是否存在一條包含某字符串所有字符的路徑。路徑可以從矩陣任意一格開始,每一步可以在矩陣中向上下左右移動一格。如果一條路徑經過了矩陣的某一格,那麼該路徑不能再次進入該格子。例如在下面的3*4矩陣中包含一條字符串‘`bcced',的路徑(路徑中的字母用斜體表示)。但矩陣中不包含字符串“abcd”的路徑,因爲字符串的第一個字符b佔據了矩陣中的第一行第二個格子之後,路徑不能再次進入這個格子。

a b c e

s f c s 

a d e e 

自己的解題代碼,主要部分就一個遞歸的函數,十行不到:

#include <iostream>
#include <new>
#include <string>

class findpath{
private:
    char* Matrix;
    char* target_string;
    bool* had_searched;
    unsigned int W;
    unsigned int H;
    bool string_had_find;
    void recursion(unsigned int col, unsigned int row, unsigned int depth);
public:
    findpath(){
        init_W_and_H();
        init_Matrix();
    };
    ~findpath(){
        delete[] Matrix;
        delete[] had_searched;
    }
    void init_W_and_H();
    void init_Matrix();
    void set_had_searched(unsigned int col, unsigned int row);
    void unset_had_searched(unsigned int col, unsigned int row);
    bool is_searched(unsigned int col, unsigned int row);
    void get_target_string();
    bool find_string();

};
void findpath::init_W_and_H()
{
    using namespace std;
    cout << "please input w and H:" << endl<<"W = ";
    cin >> W;
    cout << "H = ";
    cin >> H;
    if (W <= 0 || H <= 0)
        init_W_and_H();
}
void findpath::init_Matrix()
{
    Matrix = new char[W*H];
    if (Matrix == NULL){
        std::cout << "alloc Matrix error!" << std::endl;
        exit(1);
    }
    had_searched = new bool[W*H]{false};
    if (had_searched == NULL){
        std::cout << stderr << "alloc had_searched error!"<<std::endl;
        exit(1);
    }
    std::cout<<"please input the member of matrix:";
    for (int i = 0; i < H; i++)
        for (int j = 0; j < W; j++)
            std::cin >> Matrix[i*W + j];
}
bool findpath::is_searched(unsigned int col, unsigned int row)
{
    return had_searched[row*W + col] == true;
}
void findpath::recursion(unsigned int col, unsigned int row, unsigned int depth)
{
    if (col<0 || col>W || row<0 || row>H || depth > (strlen(target_string) - 1)||is_searched(col, row))
        return;
    set_had_searched(col, row);
    if (Matrix[row*W + col] == target_string[depth]){
        if (depth == (strlen(target_string) - 1))
            string_had_find = true;
        recursion(col, row-1, depth + 1);
        recursion(col, row+1, depth + 1);
        recursion(col-1, row, depth + 1);
        recursion(col+1, row, depth + 1);
    }
    unset_had_searched(col, row);
}
void findpath::set_had_searched(unsigned int col, unsigned int row)
{
    had_searched[row*W + H] = true;
}
void findpath::unset_had_searched(unsigned int col, unsigned int row)
{
    had_searched[row*W + H] = false;
}
void findpath::get_target_string()
{
    std::cout << "input target string :";
    target_string = new char[W*H];
    std::cin >> target_string;
    string_had_find = false;
}
bool findpath::find_string()
{
    for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++){
        recursion(j, i, 0);
        if (string_had_find == true)
            return true;
    }
    return false;
}
int main()
{
    findpath matrix;
    matrix.get_target_string();
    if (matrix.find_string())
        std::cout << "find the string";
    std::cout << "no this string";
    system("pause");
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章