POJ 3050 Hopscotch (穷竭搜索)
2015-08-09 10:20
357 查看
题目链接:http://poj.org/problem?id=3050
题面:
Hopscotch
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
Sample Output
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.
Source
USACO 2005 November Bronze
题目大意:
一只牛在5*5的方格内可以从任意点开始跳,每次可以往4个方向跳,共跳5次,根据走过的方格,形成一个长度为6的序列,问这样的序列有多少种?(方格可以重复跳。)
解题:
题目挂的时候写的是穷竭搜索,看到图这么小,估计就是暴搜的意思吧。枚举从每个点开始跳,跳到5步后停止,并记录此时形成的路径,用set排重计数即可。
代码:
题面:
Hopscotch
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2563 | Accepted: 1823 |
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.
Source
USACO 2005 November Bronze
题目大意:
一只牛在5*5的方格内可以从任意点开始跳,每次可以往4个方向跳,共跳5次,根据走过的方格,形成一个长度为6的序列,问这样的序列有多少种?(方格可以重复跳。)
解题:
题目挂的时候写的是穷竭搜索,看到图这么小,估计就是暴搜的意思吧。枚举从每个点开始跳,跳到5步后停止,并记录此时形成的路径,用set排重计数即可。
代码:
#include <iostream> #include <cstdio> #include <string> #include <set> #include <vector> using namespace std; char map[6][6],path[10]; //四个方向 int dir[4][2]={0,1,-1,0,0,-1,1,0}; //计数 set <string> cnt; string tmp; //判断是否方格内 bool inside(int x,int y) { if(x>=1&&x<=5&&y>=1&&y<=5) return true; else return false; } //深搜 void dfs(int x,int y,int step) { int tx,ty; path[step]=map[x][y]; if(step==5) { //记录路径 tmp=""; for(int i=0;i<=5;i++) tmp+=path[i]; cnt.insert(tmp); return; } //往四个方向跳 for(int i=0;i<4;i++) { tx=x+dir[i][0]; ty=y+dir[i][1]; if(inside(tx,ty)) dfs(tx,ty,step+1); } } int main() { //读入 for(int i=1;i<=5;i++) for(int j=1;j<=5;j++) scanf(" %c",&map[i][j]); //枚举从每个点开始 for(int i=1;i<=5;i++) for(int j=1;j<=5;j++) dfs(i,j,0); //输出 printf("%d\n",cnt.size()); return 0; }
相关文章推荐
- Hadop使用Partitioner后,结果还是一个文件,如何解决??
- Linux 利用管道父子进程间传递数据
- 如何查询注册表的值及 Powershell 应用
- 1044. Shopping in Mars (25)
- shell中的计算
- linux下一个C语言要求CPU采用
- Linux基础知识全面总结
- Hadoop的家族成员
- Linux内核模块编译----Makefile 模板
- Cgroup maintainer丽泽范:解剖Linux核心容器技术
- 10 个免费的服务器监控工具
- 轻松使用“Explain Shell”脚本来理解 Shell 命令
- Linux的特殊文件权限
- scala 学习笔记(03) 参数缺省值、不定个数参数、类的属性(Property)、泛型初步
- Linux下如何实现备份和还原
- Linux内核makefile解析
- 有哪些高效但是不常见的Linux命令?
- Linux下批量修改文件名
- Linux log日志路径
- 使用Amazon免费云主机和Docker技术,快速搭建Squid服务器!