【搜索】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 ...
随机推荐
- Echarts根据数据长度变换柱状图柱状的颜色
//查询图表数据 function GetData() { var qs = $("#qs").val(); ...
- 【OS X系统】Xcode中搭建Python环境。
虽然按照网上教程一步一步来,但还是遇到了几个错误点,现整理出来,主要是自己做个笔记,同时也希望能帮助到其他像我一样第一次在Xcode上搭建Python环境的人.首先感谢原作者:https://zhid ...
- Matlab中的“prod”函数
B = prod(A)将A矩阵不同维的元素的乘积返回到矩阵B. 如果A是向量,prod(A)返回A向量的乘积.如果A是矩阵,prod(A)将A看作列向量,返回每一列元素的乘积并组成一个行向量B.如果A ...
- JQuery第一天——入门概述与选择器
一.什么是JQuery 一个流行的js库 核心理念:write less , do more 优势: 轻量级 强大的选择器 出色的 DOM 操作的封装 可靠的事件处理机制 完善的 Ajax 出色的浏 ...
- 最新的Veil3.0的安装和使用
首先安装 ┌─[root@sch01ar]─[~] └──╼ #cd /sch01ar/Veil/ ┌─[root@sch01ar]─[/sch01ar/Veil] └──╼ #cd setup/ ┌ ...
- CF 1110 D. Jongmah
D. Jongmah 链接 题意: 一些数字,有两种方式组成一个三元组,[x,x,x],[x,x+1,x+2],每个数字只能用一次,求最多组成多少三元组. 分析: 因为每三个[x,x+1,x+2]是可 ...
- Java Swing:JPanel添加边框
一.JPanel添加不同边框的效果图如下所示 二. 不同边框样式的代码实现 JPanel jpanel = new JPanel(); jpanel.setBorder(BorderFactory.你 ...
- 传统路由和OVS区别
本文主要描述了一种将三层路由变成二层交换转发(以及二层转发变成三层路由)的实现方式,以应对OVS(OpenFlow)跨网段路由复杂的问题:当然技术本身是客观的,具体应用还要看场景. 随着SDN技术不断 ...
- 自动化运维工具saltstack04 -- 之jinja模板
jinjia模板 需求:想让saltstack的file模块分发到minion端的配置文件监听minion端的IP和端口,如何用变量实现?看下面!! 1.jinja模板加grains使apache监听 ...
- 关于Unity中OnGUI()的简单使用
有时候想要输出一些数据到屏幕上方便查看,新建一个UI对象又挺麻烦,用OnGUI()在屏幕上直接绘制UI比较方便. GUI.Label(, , , ), “aaa", style); 这条语句 ...