zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    【HDOJ】2102 A计划
    this关键字
    static(静态、修饰符)
    手势抽取过程&代码复用
    Android:padding和android:layout_margin的区别
    平移动画
    读取系统联系人
    获取sim卡序列号
    图片选择器
    md5加密过程
  • 原文地址:https://www.cnblogs.com/wlxtuacm/p/5712295.html
Copyright © 2011-2022 走看看