POJ 3050

题意:

1.5*5的方阵中,随意挑一格,记住这个格子的数字

2.可以上下左右走,走5次,每走一次记录下所走格子的数字

3.经过以上步骤,把所得6个数字连起来,形成一串数字。求共可以形成多少种不同的数字串

思路:

网格大小只有5*5,用穷举法,不会超时。

这里利用了stl中的set容器来防止重复。最终只要输出set的size就是结果。

注意dfs回溯时的编程规范

#include<iostream>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<vector>
#include<set>
#include<stack>
using namespace std; int a[5][5];
vector<int> v;
set<vector<int> >s;
int dir[][2]={0,1,1,0,0,-1,-1,0}; void dfs(int x,int y,int step)
{
if(step==5)
{
s.insert(v);
return;
}
for(int i=0;i<4;i++)
{
int nx=x+dir[i][0];
int ny=y+dir[i][1];
if(nx<0||ny<0||nx>=5||ny>=5)continue;
v.push_back(a[nx][ny]);
dfs(nx,ny,step+1);
v.pop_back();
} } int main()
{
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
cin>>a[i][j];
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
{
v.clear();
v.push_back(a[i][j]);
dfs(i,j,0);
}
cout<<s.size();
return 0;
}

POJ 3050 Hopscotch【DFS带回溯】的更多相关文章

  1. poj 3050 Hopscotch DFS+暴力搜索+set容器

    Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2774 Accepted: 1940 Description ...

  2. 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 ...

  3. POJ 3050 Hopscotch(dfs,stl)

    用stack保存数字,set判重.dfs一遍就好.(或者编码成int,快排+unique #include<cstdio> #include<iostream> #includ ...

  4. POJ -3050 Hopscotch

    http://poj.org/problem?id=3050 给定一个5×5矩阵,问选6个数的不同排列总数是多少! 二维的搜索,注意要判重,数据量很小,直接用map就好. #include<cs ...

  5. POJ 3050 Hopscotch 水~

    http://poj.org/problem?id=3050 题目大意: 在一个5*5的格子中走,每一个格子有个数值,每次能够往上下左右走一格,问走了5次后得到的6个数的序列一共同拥有多少种?(一開始 ...

  6. POJ 3050 Hopscotch 四方向搜索

    Hopscotch Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6761   Accepted: 4354 Descrip ...

  7. POJ 3050 枚举+dfs+set判重

    思路: 枚举+搜一下+判个重 ==AC //By SiriusRen #include <set> #include <cstdio> using namespace std; ...

  8. POJ.3172 Scales (DFS)

    POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...

  9. 洛谷1378 油滴扩展 dfs进行回溯搜索

    题目链接:https://www.luogu.com.cn/problem/P1378 题目中给出矩形的长宽和一些点,可以在每个点放油滴,油滴会扩展,直到触碰到矩形的周边或者其他油滴的边缘,求出剩余面 ...

随机推荐

  1. 史上最全 40 道 Dubbo 面试题及答案

    https://blog.csdn.net/zl1zl2zl3/article/details/83721147

  2. 转--python 基础

    核心库 1.NumPy 当我们用python来处理科学计算任务时,不可避免的要用到来自SciPy  Stack的帮助.SciPy Stack是一个专为python中科学计算而设计的软件包,注意不要将它 ...

  3. vue错误记录

    启动时报错如下 D:\QQFile\\FileRecv\industry_vue>cnpm run dev > vue_demo@ dev D:\QQFile\\FileRecv\indu ...

  4. 如何写一个好的缺陷(Defect)报告

    编写缺陷报告是测试人员的日常工作,好的缺陷报告能够让开发人员更容易理解,更快速的定位问题:不好的缺陷报告可能会误导调查方向,增加沟通成本.那么一个好的缺陷报告应该包括哪些方面呢? 请看我的mindma ...

  5. C# redis简单的使用

    1.项目一:用于在Redis中添加数据 using System; using System.Collections.Generic; using System.Linq; using System. ...

  6. 后端python基础

  7. E - tower HYSBZ - 4657 (网络流割点)

    题目链接:https://cn.vjudge.net/contest/281959#problem/E 题目大意:中文题目 具体思路:首先,有矛盾的时候就是两个导弹的运动轨迹会相交的时候,那么我们可以 ...

  8. python - isinstance/issubclass 函数

    #isinstance(obj,cls) #检查是否obj是否是object的类cls的对象 #判断一个对象是否是一个类的实例 class F00(object): pass obj = F00() ...

  9. python - class类 (二) 静态属性/类方法/静态方法

    静态属性: #静态属性 = 数据属性 (@property) class mianji(): def __init__(self,x,y): self.x = x self.y = y #类的函数方法 ...

  10. shiro与Web项目整合-Spring+SpringMVC+Mybatis+Shiro(八)

    Jar包