POJ3050-Hopscotch
来源:互联网 发布:oracle数据库本地连接 编辑:程序博客网 时间:2024/05/18 13:27
题意:给定一个5*5的地图,每个格子上有一个数字。从一个格子出发(上下左右4个方向),走5步将数字连起来可以构造出一个6位数。问该地图可以构造出多少个不同的6位数。
分析:可以对每个格子做深度优先遍历,构造出所有数字,但要注意不要重复计数。在这里,我使用了set来保存已构造出的数字,结果就是set中的元素个数。
#include <cstdio>#include <set>using namespace std;int grid[6][6];int dx[4] = {-1, 0, 1, 0};int dy[4] = {0, -1, 0, 1};set<int> digit;void Dfs(int x, int y, int step, int num){ if (step == 5) { digit.insert(num); return; } for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < 5 && ny >= 0 && ny < 5) { Dfs(nx, ny, step + 1, num * 10 + grid[nx][ny]); } }}int main(){ for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { scanf("%d", &grid[i][j]); } } for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { Dfs(i, j, 0, grid[i][j]); } } printf("%lu\n", digit.size()); return 0;}
int main()
{
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
scanf("%d", &grid[i][j]);
}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
Dfs(i, j, 0, grid[i][j]);
}
}
printf("%lu\n", digit.size());
return 0;
}
阅读全文
0 0
- POJ3050 Hopscotch
- POJ3050--Hopscotch
- POJ3050-Hopscotch
- Hopscotch POJ3050
- POJ3050 Hopscotch
- POJ3050-Hopscotch
- POJ3050 Hopscotch 【DFS】
- poj3050 Hopscotch(dfs基础)
- [dfs]POJ3050 Hopscotch
- POJ3050-Hopscotch-穷竭搜索
- POJ3050——Hopscotch
- poj3050
- poj3050
- poj3050
- Hopscotch
- Hopscotch
- Hopscotch
- * Hopscotch
- 智能教育之电子积木的评测实验
- The only legal comparisons are between two numbers, two strings, or two dates
- python总结3
- 【canvas】使用canvas画出坐标和线
- 多语言融合开发
- POJ3050-Hopscotch
- 每日练习|Day017
- 富文本编辑器的使用
- 图的一些操作
- 遗传学自出题
- 鞍点计算
- 你真的理解机器学习中偏差
- 获取子类元素,FirstElementChild、children和ChildNode等等
- 【软工视频总结】-- 软件工程