[挑战程序设计竞赛] POJ 3050 - Hopscotch

题意:

给定5x5的矩阵。矩阵中的每个点都可以作为起点,每个点可以跳到附近上、下、左、右四个位置。每个位置可以重复跳跃。

问:执行6次操作,可以组合出多少种不同的序列?

DFS简单题~ 把每次搜到的结果存到set容器内,最后输出set元素个数即可。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <time.h>
//#define _Test
typedef long long LL;
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;
int graph[10][10], dir[4][2] = {-1,0, 1,0, 0,1, 0,-1};
set<int> ss;
void dfs(int deep, int x, int y, int val)
{
    if(deep == 6)
    {
        ss.insert(val);
    }
    else
    for(int i = 0; i < 4; ++i)
    {
        int xx = x + dir[i][0];
        int yy = y + dir[i][1];
        if(graph[xx][yy] != -1)
        {
            dfs(deep + 1, xx, yy, val * 10 + graph[x][y]);
        }
    }

}
int main()
{
    #ifdef _Test
        freopen("test0.in", "r", stdin);
        freopen("test0.out", "w", stdout);
        srand(time(NULL));
    #endif
    memset(graph, -1, sizeof(graph));
    while(~scanf("%d", &graph[1][1]))
    {

        for(int i = 1; i <= 5; ++i)
        {
            for(int j = 1; j <= 5; ++j)
            {
                if(j != 1 || i != 1)
                    scanf("%d", &graph[i][j]);
            }
        }
        for(int i = 1; i <= 5; ++i)
        {
            for(int j = 1; j <= 5; ++j)
            {
                dfs(0, i, j, 0);
            }
        }
        printf("%d\n", ss.size());
        ss.clear();
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值