poj 3050

bfs穷竭搜索,不是很难,死在string的清空上,不知道为什么内存错误,于是改了个格式

#include <iostream>
#include <cstdio>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
const int maxn=5;
int mapp[maxn][maxn];
int ans;
string a;
set<string> b;
int bb[5][3]= {{1,0},{0,1},{-1,0},{0,-1}};
void dfs(int x,int y,int len)
{
    if(len==5)
    {
        b.insert(a);
        return;
    }
    for(int i=0; i<4; i++)
    {
        int dx=x+bb[i][0],dy=y+bb[i][1];
        if(dx>=0&&dx<5&&dy>=0&&dy<5)
        {
            a.push_back(mapp[dx][dy]+'0');
            dfs(dx,dy,len+1);
            a.resize(a.size()-1);
        }
    }
}
int main()
{

    for(int i=0; i<maxn; i++)
        for(int j=0; j<maxn; j++)
            scanf("%d",&mapp[i][j]);
    ans=0;
    for(int i=0; i<maxn; i++)
        for(int j=0; j<maxn; j++)
        {
            a.clear();
            a.push_back(mapp[i][j]+'0');
            dfs(i,j,0);
        }
    printf("%d\n",b.size());

    return 0;
}

  

posted on 2017-03-27 22:21  发牌员  阅读(140)  评论(0)    收藏  举报

导航