【搜索】POJ-3050 基础DFS
一、题目
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的更多相关文章
- poj 3050 Hopscotch DFS+暴力搜索+set容器
Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2774 Accepted: 1940 Description ...
- POJ 3050 Hopscotch(dfs,stl)
用stack保存数字,set判重.dfs一遍就好.(或者编码成int,快排+unique #include<cstdio> #include<iostream> #includ ...
- POJ 3050 枚举+dfs+set判重
思路: 枚举+搜一下+判个重 ==AC //By SiriusRen #include <set> #include <cstdio> using namespace std; ...
- 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 ...
- POJ 3050 Hopscotch【DFS带回溯】
POJ 3050 题意: 1.5*5的方阵中,随意挑一格,记住这个格子的数字 2.可以上下左右走,走5次,每走一次记录下所走格子的数字 3.经过以上步骤,把所得6个数字连起来,形成一串数字.求共可以形 ...
- POJ.3172 Scales (DFS)
POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...
- POJ 1088 滑雪 DFS 记忆化搜索
http://poj.org/problem?id=1088 校运会放假继续来水一发^ ^ 不过又要各种复习,功课拉下了许多 QAQ. 还有呀,就是昨天被一个学姐教育了一番,太感谢了,嘻嘻^ ^ 好了 ...
- poj 1088 动态规划+dfs(记忆化搜索)
滑雪 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Description Mi ...
- 题目--oil Deposits(油田) 基础DFS(深度搜索)
上一次基本了解了下BFS,这次又找了个基本的DFS题目来试试水,DFS举个例子来说就是 一种从树的最左端开始一直搜索到最底端,然后回到原端再搜索另一个位置到最底端,也就是称为深度搜索的DFS--dep ...
随机推荐
- 文本处理三剑客之 grep/egrep
grep:文本过滤工具 支持BRE egrep: 支持ERE fgrep: 不支持正则 作用:根据用户指定的“模式”,对目标文本逐行进行匹配检查,打印匹配到的行 模式:由正则表达式字符及文本字符所编写 ...
- rem布局简介
移动端常见布局: 1.流式布局 高度固定,宽度自适应 2.响应式布局 能够用一套代码适应不同尺寸屏幕 3.rem布局 宽高自适应,能实现整个页面像一张图片一样缩放且不失真的效果. rem布局: em: ...
- 安卓isEmpty()的注意事项,主要判断NULL
项目中服务器返回的字符串有可能为NULL或者没有内容,习惯性直接用String.isEmpty() 运行中发现字符串为NULL的时候就会出错,之前有查百度看到过正确的用法,但一直没在意, 就直接加多一 ...
- C语言学习记录_2019.02.06
break语句的作用:当执行到break,则跳出循环,免去不必要的循环次数,节省时间和资源.-----跳出循环,结束循环: continue:跳过这一次循环剩下的语句,进入到下一轮循环.-----跳到 ...
- Echarts插件
<%@ page contentType="text/html;charset=UTF-8" language="java" %><% Str ...
- 对Dataguard的三种模式的理解
模式1:最大可保护模式: 必须同步. 模式2:最大可用性模式: 能同步就同步,不能同步就不同步. 模式3:最大性能模式: 异步模式.
- jquery几秒钟之后跳转页面
<script> window.onload = function() { var el = document.getElementById('js-tip-timer'), i = 5; ...
- Flutter - 下载别人的Flutter项目,本地编译不过
如果直接下载了别人的Flutter项目,点击运行基本会不通过的,这是gradle版本差异造成的. 你需要修改android/gradle/wrapper/gradle-wrapper.properti ...
- 阿里云Linux的mysql安装,使用yum安装
1.下载 我下载的mysql5.7 rpm格式的,在Linux的根目录下下载(防止出现安装的问题) wget https://dev.mysql.com/get/mysql57-community-r ...
- Unity学习笔记(5):动态加载Prefab
第一种方法,从Resources文件夹读取Prefab Assets/Resources文件夹是Unity中的一个特殊文件夹,在博主当前的认知里,放在这个文件夹里的Prefab可以被代码动态加载 直接 ...