题目描述
如下图, 有12张连在一起的12生肖的邮票。现在你要从中剪下5张来,要求必须是连着的。(仅仅连接一个角不算相连)

比如,下面两张图中,粉红色所示部分就是合格的剪取。

请你计算,一共有多少种不同的剪取方法。
输出
请填写表示方案数目的整数。

解题思路:
这题不能用dfs,原因是dfs无法处理下面这种情况:

dfs一路走到底,所以它没办法回头,所以这题不能用dfs,那么我们可以用next_permutation函数枚举所选的5个点,然后用dfs判断是否连通。

代码如下:

#include <iostream>
#include <algorithm>
using namespace std;
const int N = 5;
bool mp[N][N];
int cnt;
int ans;int a[] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1};int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};void dfs(int x, int y) {if (x < 0 || x > 2 || y < 0 || y > 3)return;if (mp[x][y] != 1)return ;cnt++;if (cnt == 5) {ans++;return ;}mp[x][y] = 0;for (int i = 0; i < 4; i++) {int xx = x + dx[i], yy = y + dy[i];dfs(xx, yy);}
}int main() {do {int px, py;for (int i = 0; i <= 2; i++)for (int j = 0; j <= 3; j++) {if (a[i * 4 + j] == 1) {//将一维数组映射到二维数组px = i;py = j;mp[i][j] = 1;} elsemp[i][j] = 0;}dfs(px, py);cnt = 0;} while (next_permutation(a, a + 12));cout << ans << endl;return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 5;int a[] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1};int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int cnt ;
int px, py;
int mp[N][N];
int ans;void dfs(int x, int y) {for (int i  = 0; i < 4; i++) {int xx = x + dx[i];int yy = y + dy[i];if (xx < 0 || xx >= 3 || yy < 0 || yy >= 4 || !mp[xx][yy])continue;mp[xx][yy] = 0;cnt++;if (cnt == 5)ans++;dfs(xx, yy);}
}int main() {do {for (int i = 0; i < 3; i++)for (int j = 0; j < 4; j++) {if (a[i * 4 + j] == 1) {mp[i][j] = 1;px =  i;py = j;} elsemp[i][j] = 0;}cnt = 1;mp[px][py] = 0;dfs(px, py);} while (next_permutation(a, a + 12));cout << ans << endl;return 0;
}

[蓝桥杯2016初赛]剪邮票-dfs+next_permutation(好题)相关推荐

  1. [蓝桥杯2016初赛]方格填数-next_permutation

    代码如下: #include <iostream> #include <algorithm> using namespace std;int main() {int a[10] ...

  2. [蓝桥杯2016初赛]凑算式-dfs,next_permutation

    代码如下: #include <iostream> using namespace std; const int N = 15; bool st[N]; double a[N];int c ...

  3. 蓝桥杯2016初赛python题解

    前言:除特殊说明外题解均可AC 蓝桥杯2016初赛 [蓝桥杯2016初赛]网友年龄 [蓝桥杯2016初赛]生日蜡烛 [蓝桥杯2016初赛]方格填数 [蓝桥杯2016初赛]寒假作业 [蓝桥杯2016初赛 ...

  4. 蓝桥杯取球博弈c语言算法,1298: [蓝桥杯2016初赛]取球博弈 (博弈)

    1298: [蓝桥杯2016初赛]取球博弈 (博弈) 1298: [蓝桥杯2016初赛]取球博弈 (博弈) #include #include #include #include #include # ...

  5. 蓝桥杯——2016第七届C/C++真题[省赛][B组]

    目录 1. 煤球数目(结果填空) 2. 生日蜡烛(结果填空) 3. 凑算式(结果填空) 4. 快速排序(代码填空) 5. 抽签(代码填空) 6. 方格填数(结果填空) 7. 剪邮票(结果填空) 8. ...

  6. [蓝桥杯2017初赛]算式900+dfs,next_permutation

    法一: 代码如下: #include <iostream> using namespace std; const int N = 15; bool st[N]; int a[N];void ...

  7. [蓝桥杯2016初赛]寒假作业-next_permutation枚举

    暴搜代码(耗时较长,要90s左右)如下: #include <iostream> using namespace std; const int N = 15; int a[N]; bool ...

  8. [蓝桥杯2016初赛]搭积木-枚举,next_permutation

    代码如下: #include <iostream> #include <algorithm> using namespace std;int a[] = {0, 1, 2, 3 ...

  9. [蓝桥杯2016决赛]七星填数-next_permutation枚举

    题目描述 如下图所示: 在七角星的14个节点上填入1~14 的数字,不重复,不遗漏.要求每条直线上的四个数字之和必须相等. 图中已经给出了3个数字.请计算其它位置要填充的数字,答案唯一. 填好后,请提 ...

最新文章

  1. LeetCode(9.回文数)JAVA
  2. 【iOS数据持久化】Plist使用
  3. 计算机教室内网连接不了,校园网登陆不了内网怎么办?校园网登陆不了内网的解决方法...
  4. Pixhawk原生固件PX4之串口添加读取传感器实现
  5. DDR3和eMMC区别
  6. [SpringBoot2]HelloWorld
  7. adb push ,adb pull和adb install的区别
  8. 中间件:ElasticSearch组件RestHighLevelClient用法详解
  9. openEuler Summit 带你解锁开源与操作系统的不解之缘
  10. Intel Sandy Bridge/Ivy Bridge架构/微架构/流水线 (14) - 读存操作写存操作
  11. python调用math函数_python3中调用C语言的函数
  12. 1-5Badboy添加检查点和参数化
  13. educoder软件工程导论结构化分析方法
  14. 中国象棋AI在线弈游戏源码
  15. Java输入1~12之间的整数,显示该月份的英语单词及这个月属第几季度。
  16. 【昊鼎王五】pip安装pymssql模块时报错“PEP 517”怎么解决?
  17. Mac 调节鼠标移速
  18. 五、Hive数据仓库(完整版)
  19. linux freemind字体,解决freemind在Ubuntu中输入中文问题
  20. 乐视网暂停上市成定局:去年净资产为负30亿 债务难解

热门文章

  1. Flutter之Decoration
  2. Android之如何解决adb server is out of date,killing...ADB server didn't ACK
  3. Android之Bitmap学习总结
  4. 《零基础看得懂的C语言入门教程 》——(十三)socket服务端编写
  5. 【C语言简单说】六:取模运算符以及变量的扩展
  6. windows下配置mysql主从复制_Windows下MySQL主从复制的配置方法
  7. 如果从椭圆的一个焦点发出光线,再经过椭圆的反射,会发生什么?
  8. 你最擅长哪种数学思维?
  9. 大牛逝世 = 新人上位 = 科学进步?新研究表明确实如此
  10. 数据时代,信息的无处遁形