• string类的dfs


    Source Code

    Problem: 3050        
    Memory: 756K        Time: 235MS
    Language: C++        Result: Accepted
    Source Code
    #include <iostream>
    #include <string>
    #include <set>
    using namespace std;
    const int MAX = 5;
    set<string> result;
    char data[MAX][MAX];
    int a[]={-1,0,1,0}, b[]={0,1,0,-1};

    void DFS(int x, int y,int l,string s)
    {
        if(l==6)
        {
            result.insert(s);
            return ;
        }
        for(int i=0; i <4; i++)
        {
            int tempx = x+a[i], tempy=y+b[i];
            if(tempx>=0 && tempx < 5 && tempy >=0 && tempy < 5)
            {
                string str = s;
                s += data[tempx][tempy];
                DFS(tempx, tempy,l+1, s);
                s = str;//建一个空的str
            }
        }
        return ;
    }

    void solveCase()
    {

        for(int i=0; i < 5; i++)
        {
            for(int j=0; j<5; j++)
            {
                DFS(i, j, 0, "");                
            }
        }
        cout << result.size() << endl;
    }

    int main()
    {
        for(int i=0; i < 5; i++)
            for(int j=0; j < 5; j++)
                cin >> data[i][j];

        solveCase();

        return 0;
    }
  • 相关阅读:
    4.23计算机网络
    CF436F Banners
    CF1467C Three Bags
    LG P3247 [HNOI2016]最小公倍数
    LG P5473 [NOI2019] I 君的探险
    LG P3261 [JLOI2015]城池攻占
    LG P4149 [IOI2011]Race
    LG P3181 [HAOI2016]找相同字符
    SP7258 SUBLEX
    SP1811 LCS
  • 原文地址:https://www.cnblogs.com/wlxtuacm/p/5712295.html
Copyright © 2020-2023  润新知