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

18 篇文章 0 订阅
12 篇文章 0 订阅


剪邮票


如【图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;  //44
	return 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 = 792

struct 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;  //答案  115
	cout << cc << endl;   //组合情况总数 792
	return 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;
}

//能够被纳入连通域返回true
bool 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; //116
	cout << cc << endl;  //792
	return 0;
}


  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DESCRIPTION: 1.Analyze Problem A : sorted stamps array A={ai} ai: one stamp element in array n: array size, that is number of elements in array r: desired value of stamps F(n,r):expected result, minimum number of stamps for a given value r from n size array. S: selected stamps array S={si} 2.Choose Algorithm a.Greedy algorithm seems to be a good choice, try to solve it in O(n), i try divide array into subarry B={bi}, r should larger than every elemnt in B that is r>bi and suppose bk is the smallest element in B, so that r= bk%r, f(i,r)=(bk/r), F(n,r)=∑f(i,r). The main idea is to choose the last element who larger than desired value each time. However,it can not give us optimal solution in some condition, like A={8,5,4,1}, if r=10, this algoritm will give a solution F(n,r)=3, S={8,1,1},but the optimal solution should be F(n,r)=2, S={5,5}. b.Full search so the straight forwards algorithm is to search for every solution in A for desired value directly.However, it will always take O(n!) to go through every combination. c.Dynamic programming, at last, I decide to choose dynamic programming. analyze optimal structure, suppose in A={ai}, for a specific stamp ak,there will be two cases it is choosen so that f(i,r)=1+f(i,r-ak) , 1<=i<=k, r>=ak it is not valid so that f(i,r)=f(i-1,r) 3.Design Dynamic programming optimal structure: Compute-opt(r)= 1 + Compute-opt(r-ai) value: Compute-opt(r) = ∞ (r < 0) Compute-opt(r) = 0 (r = 0) Compute-opt(r) = 1+{Compute-opt(r-ai)} ( 1=<i<=n, r>ai>0 ) Complexity :O(nr) Memory cost:O(n+r) Compute in a bottom-up style to recursive every desired value and array. store value of Compute-opt in memory for future use, so that we can easily get value from those memory in future recursive call, and avoid compute again, that is if the array is not change, you can easily fetch result any desired value j (j < r, r is the value using for compute ). 2.For User totally, I design a small command line for this machine list below 1.Manual Operation 2.Self Auto Testing 3.Check Results q.Quit Manual Operation: when select this machine will turn to be manual mode, ask person to input stamps and desired value; Self Auto Testing:when select this machine will turn to be auto mode, show the test case already design in code after that machine will quit automatically. Check Results: only be visiable in Manual Operation, people can check desired value for the array input before, the desired value should be no more than first time input. Quit, clean all the memory and quit system.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值