码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3050 Hopscotch

时间:2015-10-18 16:47:04      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

用stack保存数字,set判重。dfs一遍就好。(或者编码成int,快排+unique

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;

const int maxn = 5;
int g[maxn][maxn];

bool inside(int x,int y)
{
    return x>=0&&x<maxn&&y>=0&&y<maxn;
}

set<stack<int> > S;
stack<int> stk;
const int dx[] = {0,0,-1,1}, dy[] = {1,-1,0,0};
void dfs(int x,int y)
{
    if(!inside(x,y)) return;
    if(stk.size() == 6){
        S.insert(stk);
        return ;
    }
    for(int i = 0; i < 4; i++){
        int nx = x+dx[i], ny = y+dy[i];
        if(inside(nx,ny)){
            stk.push(g[nx][ny]);
            dfs(nx,ny);
            stk.pop();
        }
    }
}

//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    for(int i = 0; i < maxn; i++){
        for(int j = 0; j < maxn; j++){
            scanf("%d",g[i]+j);
        }
    }
   for(int i = 0; i < maxn; i++)
        for(int j = 0; j < maxn; j++)
            dfs(i,j);
    printf("%d\n",S.size());
    return 0;
}

 

POJ 3050 Hopscotch

标签:

原文地址:http://www.cnblogs.com/jerryRey/p/4889654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!