uva524(dfs水题)

题意:

把编号为1到n的珠子,串成手环,要求任意两个相邻的珠子和都为质数;

思路:

数据量只有16,打个质数表,直接dfs搜就行了;

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int vis[32];
int viss[20];
int res[20],n;
void init() {
	vis[1] = 1;
	for(int i = 2; i <= 31;i++) {
		if(!vis[i]) {
			for(int j = i + i; j <= 31; j += i)
				vis[j] = 1;
		}
	}
	return;
}

void dfs(int pre,int sum) {
	if(sum == n) {
		if(!vis[pre + 1]) {
			printf("%d",res[1]);
			for(int i = 2; i <= n;i++) {
				printf(" %d",res[i]);
			}
			printf("\n");
		}
		return;
	}
	for(int i = 1; i <= n; i++) {
		if(!viss[i] && !vis[i + pre]) {
			viss[i] = 1;
			res[sum + 1] = i;
			dfs(i,sum + 1);
			viss[i] = 0;
		}
	}
	return;
}
int main() {
	int cas = 1;
	init();
	bool f = 0;
	while(scanf("%d",&n) == 1) {
		if(f)
			printf("\n");
		else
			f = 1;
		memset(viss, 0,sizeof(viss));
		printf("Case %d:\n",cas++);
		viss[1] = 1;
		res[1] = 1;
		dfs(1,1);
	}
}
时间: 2024-11-08 23:48:06

uva524(dfs水题)的相关文章

poj 1979 dfs水题

// 练练水题,夯实基础吧 #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib

poj 1979 Red and Black(dfs水题)

Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only

CodeForces 510B DFS水题

题目大意:在图中找到一个字符可以围成一个环(至少有环四个相同元素) 题目思路:对当前点进行搜索,如果发现可以达到某个已经被查找过的点,且当前点不是由这个点而来,则查找成功. #include<cstdio> #include<stdio.h> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #include<cstring>

华为题 搜索水题 DFS

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <string> 6 #include <iterator> 7 #include <algorithm> 8 #include <cstdlib> 9 #include <deque> 10 #include &l

hdu 2212 DFS(水题)

DFS Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4923    Accepted Submission(s): 3029 Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every digit

4.7-4.9补题+水题+高维前缀和

题目链接:51nod 1718 Cos的多项式  [数学] 题解: 2cosx=2cosx 2cos2x=(2cosx)^2-2 2cos3x=(2cosx)^3-3*(2cosx) 数归证明2cos(nx)能表示成关于2cosx的多项式,设为f(n) f(1)=x,f(2)=x^2-2(其中的x就是2cosx) 假设n=1~k时均成立(k>=3) 当n=k+1时 由cos((k+1)x)=cos(kx)cos(x)-sin(kx)sin(x) cos((k-1)x)=cos(kx)cos(x)

历年NOIP水题泛做

快noip了就乱做一下历年的noip题目咯.. noip2014 飞扬的小鸟 其实这道题并不是很难,但是就有点难搞 听说男神错了一个小时.. 就是$f_{i,j}$表示在第$i$个位置高度为$j$的时候最小点击次数 递推的话对于上升的情况只做一次,后面几次在后面再做.. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace st

POJ 1274--The Perfect Stall【二分图 &amp;&amp; 最大匹配数 &amp;&amp; 水题】

The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20795   Accepted: 9386 Description Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering pr

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive