POJ 3050 Hopscotch
来源:互联网 发布:开淘宝店考试答案 名字 编辑:程序博客网 时间:2024/05/17 08:15
题意:5 X 5的矩阵,可沿上下左右四个方向走,一共能走6步,由这6个数字组成一个新的数字,输出一共能组成多少种不同的数字,注意这里走过的地方可以再走
解题思路:深搜dfs.参数中还要记录当前数字的个数以及当前的字符串,用set存储由6个数字组成的字符串,然后返回上一层,继续往下搜,最后直接输出set中字符串的个数。这道题的起始点是任意的
代码:
#include <iostream>#include <algorithm>#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <set>using namespace std;char g[10][10];set<string> s;int dr[]= {0,0,1,-1};int dc[]= {1,-1,0,0};bool inside(int x,int y){ return x>=0&&x<5&&y>=0&&y<5;}void dfs(int x,int y,int cur,string ss){ if(cur==6) { s.insert(ss); return ; } ss+=g[x][y];//如果cur>6就不用加到ss中了,若放到if判断之前就会有7个数字 for(int i=0; i<4; i++) { int nx=x+dr[i],ny=y+dc[i]; if(inside(nx,ny)) { dfs(nx,ny,cur+1,ss); } }}int main(){ for(int i=0; i<5; i++) { for(int j=0; j<5; j++) { cin>>g[i][j]; } } for(int i=0; i<5; i++) { for(int j=0; j<5; j++) { dfs(i,j,0,"");//字符串初始化为""就行 } } cout<<s.size()<<endl; return 0;}
阅读全文
0 0
- POJ 3050 Hopscotch
- poj 3050 Hopscotch DFS
- poj 3050 Hopscotch
- POJ-3050-Hopscotch
- poj 3050 Hopscotch
- POJ 3050 Hopscotch 水~
- POJ 3050 Hopscotch
- POJ-3050 Hopscotch
- POJ 3050 Hopscotch DFS
- POJ 3050 Hopscotch
- POJ 3050 Hopscotch
- poj 3050 Hopscotch 【DFS】
- poj 3050 Hopscotch
- Hopscotch (poj 3050 dfs)
- POJ 3050 Hopscotch
- Hopscotch.(POJ-3050)
- Hopscotch - POJ 3050 DFS
- POJ 3050 Hopscotch
- Ubuntu 14.04中docker入门之docker安装
- xshell链接ubuntu虚拟主机
- udp多线程实现聊天室
- “cvc-complex-type.2.4.a: Invalid content was found starting with element 'taglib'”错误的解决办法
- POJ 3126 Prime Path (BFS) (F)
- POJ 3050 Hopscotch
- Ignite SQL网格
- libjingle ubuntu 16.04 下编程错误
- 【简记】Operating System—— I/O
- CMake学习(二)
- stable_sort的含义
- 习题- 二分法/牛顿迭代法 求算术平方根
- Siamese Network理解(附代码)
- Linux-Flash驱动(1)-块设备系统架构