博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【搜索】POJ-3050 基础DFS
阅读量:4625 次
发布时间:2019-06-09

本文共 2199 字,大约阅读时间需要 7 分钟。

一、题目

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 11 1 1 1 11 1 1 1 11 1 1 2 11 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
#include
#define MAX_SIZE 5using namespace std;set
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;}

转载于:https://www.cnblogs.com/CSLaker/p/7281237.html

你可能感兴趣的文章
Sublime Text 3 build 3103 注册码
查看>>
删与改
查看>>
SAP 中如何寻找增强
查看>>
spi驱动无法建立spidev问题
查看>>
ANDROID开发之SQLite详解
查看>>
如何依靠代码提高网络性能
查看>>
Zookeeper要安装在奇数个节点,但是为什么?
查看>>
discuz 微社区安装记录
查看>>
[BZOJ4824][Cqoi2017]老C的键盘 树形dp+组合数
查看>>
配置的热更新
查看>>
MySQL事务的开启与提交,autocommit自动提交功能
查看>>
PriorityQueue
查看>>
CODEVS1403 新三国争霸
查看>>
iOS 环信离线推送
查看>>
WPFTookit Chart 高级进阶
查看>>
雷云Razer Synapse2.0使用测评 -第二次作业
查看>>
django上传文件
查看>>
CVPR2013-papers
查看>>
PHP之时间函数
查看>>
Python open()完整参数
查看>>