2016蓝桥杯省赛C/C++B组7题剪邮票 DFS枚举组合情况BFS判联通

来源:互联网 发布:乌云漏洞数据库 编辑:程序博客网 时间:2024/06/06 05:21


剪邮票


如【图1.jpg】, 有12张连在一起的12生肖的邮票。
现在你要从中剪下5张来,要求必须是连着的。
(仅仅连接一个角不算相连)
比如,【图2.jpg】,【图3.jpg】中,粉红色所示部分就是合格的剪取。


请你计算,一共有多少种不同的剪取方法。


请填写表示方案数目的整数。

注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。


  










刚开始以为直接每个格子DFS五个,然后最后总数除以5就行了,结果44,后来发现有一种特殊情况DFS是搜不到的




比如这种,紫色区域DFS不管怎么搜都不会搜到5个联通


错误代码:


#include <cstdio>#include <queue>#include <map>#include <cmath>#include <vector>#include <cstring>#include <iostream>#include <stack>#include <algorithm>using namespace std;const int r = 3, c = 4;bool vis[10][10];int cou;int dir[4][2] = { -1, 0, 0, 1, 1, 0, 0, -1 };bool check(int x, int y) {if (x < 0 || x >= r || y < 0 || y >= c || vis[x][y]) {return false;}return true;}void dfs(int x, int y, int n) {if (n == 5) {cou++;return ;}for (int i = 0; i < 4; i++) {int nx = x + dir[i][0];int ny = y + dir[i][1];if (check(nx, ny)) {vis[nx][ny] = true;dfs(nx, ny, n + 1);vis[nx][ny] = false;}}}int main(){memset(vis, false, sizeof(vis));cou = 0;for (int i = 0; i < r; i++) {for (int j = 0; j < c; j++) {vis[i][j] = true;dfs(i, j, 1);vis[i][j] = false;}}cout << cou / 5 << endl;  //44return 0;}


后来又想用DFS+并查集,但是在二维平面上用并查集,四个方向的祖先孩子关系标来标去标乱了,应该是我处理的问题


比如




相邻的两个点既可能是并查集中的祖先也可能是孩子,而我在标记的顺序上并没有什么限定,觉得这里出错的可能性很大


错误代码:


#include <cstdio>#include <vector>#include <cstring>#include <iostream>#include <set>using namespace std;const int r = 3, c = 4;int cc;  //用来记录组合的总数,12取5 = 792struct node {//x,y为当前点的横纵坐标,fa为所属集合标记int x, y, fa;node() {}node(int x, int y, int fa) : x(x), y(y), fa(fa) {}};node a[10];int len;bool mapp[10][10];int cou;int dir[4][2] = { 0, 1, 1, 0, -1, 0, 0, -1 }; //右,下,上,左int getf(int x) {return a[x].fa == x ? x : getf(a[x].fa);}void merge(int x, int y) {int t1 = getf(x);int t2 = getf(y);if (t1 != t2) {a[t2].fa = t1;  //写成a[t1].fa = t2最后结果为零...}}bool Isout(int x, int y) {if (x < 0 || x >= r || y < 0 || y >= c) {return true;}return false;}bool Ismarked(int nx, int ny) {return mapp[nx][ny];}void dfs(int x, int y, int dep) {if (dep == 5) {//遍历四个方向for (int i = 0; i < len; i++) {for (int j = 0; j < 4; j++) {int nx = a[i].x + dir[j][0];int ny = a[i].y + dir[j][1];//没出界并且是某次组合选择的5个之一if (!Isout(nx, ny) && Ismarked(nx, ny)) {int k;//找到这个点在a中的下标for (k = 0; k < len; k++) {if (a[k].x == nx && a[k].y == ny) {break;}}//集合并入merge(i, k);}}}set<int> s;for (int i = 0; i < len; i++) {s.insert(a[i].fa);}//如果只有一个集合,说明联通if (s.size() == 1) cou++;cc++;return;}if (x == r) return;for (int i = y; i <= c; i++) {if (i < c) {mapp[x][i] = true;//将收集的五个点放到a中便于并查集运算a[len] = node(x, i, len); //a[x].fa = x;len++;dfs(x, i + 1, dep + 1);len--;mapp[x][i] = false;}else {dfs(x + 1, 0, dep);}}}int main(){memset(mapp, false, sizeof(mapp));cou = 0;cc = 0;len = 0;dfs(0, 0, 0);cout << cou << endl;  //答案  115cout << cc << endl;   //组合情况总数 792return 0;}


最后才想到用BFS,BFS在判断联通的的时候不会出现像DFS那种一条线走到底不会拐弯的情况,能够把整个面都覆盖上





#include <cstdio>#include <vector>#include <cstring>#include <iostream>#include <queue>#include <set>using namespace std;const int r = 3, c = 4;int cc;struct node {int x, y;node() {}node(int x, int y) : x(x), y(y) {}};node a[10];int len;bool mapp[10][10];bool bm[10][10];int cou;int dir[4][2] = { 0, 1, 1, 0, -1, 0, 0, -1 }; //右,下,上,左bool Isout(int x, int y) {if (x < 0 || x >= r || y < 0 || y >= c) {return true;}return false;}//能够被纳入连通域返回truebool CanCollect(int nx, int ny) {return bm[nx][ny];}int bfs() {int cn;  //连通域中块的个数memcpy(bm, mapp, sizeof(bm));queue<node> Q;Q.push(a[0]);bm[a[0].x][a[0].y] = false;cn = 1;while (!Q.empty()) {node tx = Q.front(); Q.pop();for (int i = 0; i < 4; i++) {int nx = tx.x + dir[i][0];int ny = tx.y + dir[i][1];if (!Isout(nx, ny) && CanCollect(nx, ny)) {bm[nx][ny] = false;cn++;Q.push(node(nx, ny));}}}return cn;}void dfs(int x, int y, int dep) {if (dep == 5) {if (bfs() == 5) {cou++;}cc++;return;}if (x == r) return;for (int i = y; i <= c; i++) {if (i < c) {mapp[x][i] = true;a[len] = node(x, i);len++;dfs(x, i + 1, dep + 1);len--;mapp[x][i] = false;}else {dfs(x + 1, 0, dep);}}}int main(){memset(mapp, false, sizeof(mapp));cou = 0;cc = 0;len = 0;dfs(0, 0, 0);cout << cou << endl; //116cout << cc << endl;  //792return 0;}


0 0
原创粉丝点击