一、题目

Description

The cows play the child's game of hopscotch in a non-traditional way. Instead of a linear set of numbered boxes into which to hop, the cows create a 5x5 rectilinear grid of digits parallel to the x and y axes. 

They then adroitly hop onto any digit in the grid and hop forward, backward, right, or left (never diagonally) to another digit in the grid. They hop again (same rules) to a digit (potentially a digit already visited). 

With a total of five intra-grid hops, their hops create a six-digit integer (which might have leading zeroes like 000201). 

Determine the count of the number of distinct integers that can be created in this manner.

Input

* Lines 1..5: The grid, five integers per line

Output

* Line 1: The number of distinct integers that can be constructed

Sample Input

1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 2 1
1 1 1 1 1

Sample Output

15

Hint

OUTPUT DETAILS: 

111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111, and 212121 can be constructed. No other values are possible.

二、思路&心得

  • 利用set集合保证数据不重复,最后直接输出set的大小即可
  • 基础深搜,数据量较弱

三、代码

#include<cstdio>
#include<set>
#define MAX_SIZE 5
using namespace std; set<int> s; int N, M; int a[10]; int map[MAX_SIZE][MAX_SIZE]; int dirction[4][2] = {0, -1, -1, 0, 0, 1, 1, 0}; bool isLegal(int x, int y) {
if (x >= 0 && x < MAX_SIZE && y >= 0 && y < MAX_SIZE) return true;
else return false;
} void dfs(int x, int y, int k) {
if (k == 6) {
int num = a[0];
for (int j = 1; j < 6; j ++) {
num = num * 10 + a[j];
}
s.insert(num);
return;
}
for(int i = 0; i < 4; i ++) {
int tx = x + dirction[i][0], ty = y + dirction[i][1];
if (isLegal(tx, ty)) {
a[k] = map[tx][ty];
dfs(tx, ty, k + 1);
}
}
} int main() {
for (int i = 0; i < 5; i ++) {
for (int j = 0; j < 5; j ++) {
scanf("%d", &map[i][j]);
}
}
for (int i = 0; i < 5; i ++) {
for (int j = 0; j < 5; j ++) {
a[0] = map[i][j];
dfs(i, j, 1);
}
}
printf("%d", s.size());
return 0;
}

【搜索】POJ-3050 基础DFS的更多相关文章

  1. poj 3050 Hopscotch DFS+暴力搜索+set容器

    Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2774 Accepted: 1940 Description ...

  2. POJ 3050 Hopscotch(dfs,stl)

    用stack保存数字,set判重.dfs一遍就好.(或者编码成int,快排+unique #include<cstdio> #include<iostream> #includ ...

  3. POJ 3050 枚举+dfs+set判重

    思路: 枚举+搜一下+判个重 ==AC //By SiriusRen #include <set> #include <cstdio> using namespace std; ...

  4. POJ 3050 Hopscotch DFS

    The cows play the child's game of hopscotch in a non-traditional way. Instead of a linear set of num ...

  5. POJ 3050 Hopscotch【DFS带回溯】

    POJ 3050 题意: 1.5*5的方阵中,随意挑一格,记住这个格子的数字 2.可以上下左右走,走5次,每走一次记录下所走格子的数字 3.经过以上步骤,把所得6个数字连起来,形成一串数字.求共可以形 ...

  6. POJ.3172 Scales (DFS)

    POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...

  7. POJ 1088 滑雪 DFS 记忆化搜索

    http://poj.org/problem?id=1088 校运会放假继续来水一发^ ^ 不过又要各种复习,功课拉下了许多 QAQ. 还有呀,就是昨天被一个学姐教育了一番,太感谢了,嘻嘻^ ^ 好了 ...

  8. poj 1088 动态规划+dfs(记忆化搜索)

    滑雪 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Description Mi ...

  9. 题目--oil Deposits(油田) 基础DFS(深度搜索)

    上一次基本了解了下BFS,这次又找了个基本的DFS题目来试试水,DFS举个例子来说就是 一种从树的最左端开始一直搜索到最底端,然后回到原端再搜索另一个位置到最底端,也就是称为深度搜索的DFS--dep ...

随机推荐

  1. MongoDB相关操作

    1.  连接MongoDB <?php //1.连接到MongoDB $host = "127.0.0.1"; $port = 27017; $server = " ...

  2. Hadoop系列-MapReduce基础

    由于在学习过程中对MapReduce有很大的困惑,所以这篇文章主要是针对MR的运行机制进行理解记录,主要结合网上几篇博客以及视频的讲解内容进行一个知识的梳理. MapReduce on Yarn运行原 ...

  3. MIPS架构——汇编代码转机器代码编译器 Matlab GUI

    MIPS架构下的MCU,指令集包含R-Type.I-Type.J-Type三种,在数电课程设计时为了给MCU编写指令集,需要将汇编语言转化成机器代码,这里分享一下自己写的Matlab 的 GUI. 主 ...

  4. [arc072F]Dam-[单调队列]

    Description 传送门 Solution 首先我们肯定不能那么耿直地直接把水混合起来吧..不然分分钟完球. 那么怎么找到最优解呢?假如我们把水的体积和温度按顺序插入队列,这时我们插入第i天的水 ...

  5. CF 1138 E. Museums Tour

    E. Museums Tour 链接 分析: 按时间建出分层图,每个点形如(u,t),表示u在在t个时刻的点,tarjan缩点.每个强连通分量中的点都能经过,然后DAG上dp. 代码: #includ ...

  6. sso(single sign on)介绍

    1. 浏览器 > 2. 系统A(www.a.com) > 3. 系统B(www.b.com) > 4. 认证中心(www.sso.com) #### ** (1)首次访问** > ...

  7. mysql mtr写入数据

    BEGIN; --disable_query_log --let $rows= 100 WHILE($rows) { --eval INSERT INTO t1 (a) VALUES ( $rows ...

  8. 推荐一个娱乐化学习python的网站

    https://py.checkio.org/ 这个网站通过解决一些小任务引导初学者了解和使用python来处理一些实际需求.在coding的过程中还可以通过查看提示,帮助完成任务. 不过需要一点英文 ...

  9. jmeter—操作数据库

    添加JDBC Request,添加需要执行的sql语句 在这个界面需要配置Variabke Name,内容要与上表中的Name值相同:数据库的用户名.密码.URL.驱动这些基本信息要在这里配置:其他选 ...

  10. Python之NMAP详解

    一.NMAP简介 NMap,也就是Network Mapper,最早是Linux下的网络扫描和嗅探工具包. nmap是一个网络连接端扫描软件,用来扫描网上电脑开放的网络连接端.确定哪些服务运行在哪些连 ...