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;
    }
  • 相关阅读:
    learnyou 相关网站
    hdu 3038 How Many Answers Are Wrong
    hdu 3047 Zjnu Stadium 并查集高级应用
    poj 1703 Find them, Catch them
    poj 1182 食物链 (带关系的并查集)
    hdu 1233 还是畅通工程
    hdu 1325 Is It A Tree?
    hdu 1856 More is better
    hdu 1272 小希的迷宫
    POJ – 2524 Ubiquitous Religions
  • 原文地址:https://www.cnblogs.com/wlxtuacm/p/5712295.html
Copyright © 2011-2022 走看看