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;
}
posted @ 2015-12-21 16:49  adfae  阅读(132)  评论(0编辑  收藏  举报