POJ 3050 Hopscotch【DFS带回溯】
题意:
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带回溯】的更多相关文章
- poj 3050 Hopscotch DFS+暴力搜索+set容器
Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2774 Accepted: 1940 Description ...
- 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 ...
- POJ 3050 Hopscotch(dfs,stl)
用stack保存数字,set判重.dfs一遍就好.(或者编码成int,快排+unique #include<cstdio> #include<iostream> #includ ...
- POJ -3050 Hopscotch
http://poj.org/problem?id=3050 给定一个5×5矩阵,问选6个数的不同排列总数是多少! 二维的搜索,注意要判重,数据量很小,直接用map就好. #include<cs ...
- POJ 3050 Hopscotch 水~
http://poj.org/problem?id=3050 题目大意: 在一个5*5的格子中走,每一个格子有个数值,每次能够往上下左右走一格,问走了5次后得到的6个数的序列一共同拥有多少种?(一開始 ...
- POJ 3050 Hopscotch 四方向搜索
Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6761 Accepted: 4354 Descrip ...
- POJ 3050 枚举+dfs+set判重
思路: 枚举+搜一下+判个重 ==AC //By SiriusRen #include <set> #include <cstdio> using namespace std; ...
- POJ.3172 Scales (DFS)
POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...
- 洛谷1378 油滴扩展 dfs进行回溯搜索
题目链接:https://www.luogu.com.cn/problem/P1378 题目中给出矩形的长宽和一些点,可以在每个点放油滴,油滴会扩展,直到触碰到矩形的周边或者其他油滴的边缘,求出剩余面 ...
随机推荐
- 转 -- 详解python的super()的作用和原理
原文地址 Python中对象方法的定义很怪异,第一个参数一般都命名为self(相当于其它语言的this),用于传递对象本身,而在调用的时候则不必显式传递,系统会自动传递. 今天我们介绍的主角是supe ...
- Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=128m; support was removed in 8.0
1 启动hbase的时候爆出警告 Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=128m; suppor ...
- jQuery - 几种异步提交方式
$.post(url,params,callback); $.post("${ctx}/role/grant", {userId : $("#userId"). ...
- js数组的操作push,pop,shift,unshift
push(args)可以每次压入多个元素,并返回更新后的数组长度. var oldArr=[1,2,3]; alert(oldArr.push(4,[5,6]))–>5(这里只会将[5,6]当做 ...
- NSURLResponse下载
// // ViewController.m // 05-NSURLConnestion(下载) // // Created by jerry on 15/10/24. // Copyright (c ...
- shiroWeb项目-授权(十一)
使用PermissionsAuthorizationFilter 在applicationContext-shiro.xml中配置url所对应的权限. 测试流程: 1.在applicationCont ...
- ROS学习笔记(一) # ROS参数服务器
参考 roscpp/Overview/Parameter Server 0. 概述 ROS参数服务器能够保存 string, int, float, double, bool, list, dicti ...
- SpringBoot使用其他的Servlet容器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- jquery插件模式开发和react组件开发之间的异同
jquery插件模式开发和react组件开发之间的异同
- Libevent源码分析—event_add()
接下来就是将已经初始化的event注册到libevent的事件链表上,通过event_add()来实现,源码位于event.c中. event_add() 这个函数主要完成了下面几件事: 1.将eve ...