一、题目

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. Hibernate第三天——表间关系与级联操作

    第三天,我们来使用Hibernate进行表之间一对多 多对多关系的操作: 这里我们先利用两个例子进行表关系的回顾: 一对多(重点): 例如分类和商品的关系,一个分类多个商品,一个商品属于一个分类 CR ...

  2. MySQL下建立表

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL杂记页     回到顶级页面:PostgreSQL索引页 [作者 高健@博客园  luckyjackgao@gmail. ...

  3. Why network port is open but no process attached?(为什么端口被打开,但是没有进程号)

    When I check my system today, I noticed a weird output from netstat’s output, joseph# sudo netstat - ...

  4. virtualenvwrapper安装和常用指令(mac)

    安装: .安装(要有python环境+pip): * sudo pip install virtualenvwrapper .配置: 执行:vi ~/.bash_profile 在~/.bash_pr ...

  5. 总结hibernate框架的常用检索方式

    1.hibernate框架的检索方式有以下几种: OID检索:根据唯一标识OID检索数据 对象导航检索:根据某个对象导航查询与该对象关联的对象数据 HQL检索:通过query接口对象查询 QBC检索: ...

  6. jmeter线程组介绍

    Jmeter中的测试计划是一直有的,但可以在右侧修改名字,要开始做具体测试设计前,都需要在测试计划下边添加一个线程组,添加路径为鼠标捕获测试计划后,点击鼠标右键->添加->Threads( ...

  7. webpack新手入门——配置及安装

    webpack 是一个现代 JavaScript 应用程序的静态模块打包器.当 webpack 处理应用程序时,它会递归地构建一个依赖关系图(dependency graph),其中包含应用程序需要的 ...

  8. 错误结果保存示例 - 【jmeter】

  9. mysql读写分离配置(整理)

    mysql读写分离配置 环境:centos7.2 mysql5.7 场景描述: 数据库Master主服务器:192.168.206.100 数据库Slave从服务器:192.168.206.200 M ...

  10. Linux系统中时间区域和API

    1.问题 在开发云平台程序的时候,经常会碰到时间区域转换的问题.比如,任何网络存储的文档的metadata都自己记录了编辑时间.但是,云平台记录时需要把这个时间转成标准时间,便于管理.但是用户使用的时 ...