SDAU训练日志第21篇----------DFS和BFS(3)(2018年3月4日)

前几天生病了,没写日志,今天好点了,另外以后不熬夜了,太伤身体了。

放上来今天A的题,细节以后再补充

洛谷P1219八皇后问题

题目描述

检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行、每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子。

上面的布局可以用序列2 4 6 1 3 5来描述,第i个数字表示在第i行的相应位置有一个棋子,如下:

行号 1 2 3 4 5 6

列号 2 4 6 1 3 5

这只是跳棋放置的一个解。请编一个程序找出所有跳棋放置的解。并把它们以上面的序列方法输出。解按字典顺序排列。请输出前3个解。最后一行是解的总个数。

输入格式:
一个数字N (6 <= N <= 13) 表示棋盘是N x N大小的。
输出格式:
前三行为前三个解,每个解的两个数字之间用一个空格隔开。第四行只有一个数字,表示解的总数。

P1219 八皇后
#include<iostream>
using namespace std;
int row[100],arrange[100],zheng_catercorner[100],fan_catercorner[100];
int n,solves;
//行、列、正对角线、反对角线
void out()
{
	if(solves<=2)
	{
		for(int z=1;z<=n;z++)
			cout<<row[z]<<' ';
	    cout<<endl;
	}
	solves++;
}
void dfs(int now_nums)//dfs过程
{
	if(now_nums>n)
	{
		out(); 
		return;
	}
	else 
	{
		for(int j=1;j<=n;j++)
			if((arrange[j]==0)&&(zheng_catercorner[now_nums+j]==0)&&(fan_catercorner[now_nums-j+n]==0))
			{
				row[now_nums]=j,arrange[j]=1,zheng_catercorner[now_nums+j]=1,fan_catercorner[now_nums-j+n]=1;
				dfs(now_nums+1);//深度优先搜索
				arrange[j]=0,zheng_catercorner[now_nums+j]=0,fan_catercorner[now_nums-j+n]=0;
			}
	}
}
int main()
{
	cin>>n;
	dfs(1);
	cout<<solves<<endl;
	return 0;
}

DFS的经典写法,和全排列的题差不多

题目2:搜索与查找二分hdu习题1002

Now, here is a fuction:<br>&nbsp;&nbsp;F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 &lt;= x &lt;=100)<br>Can you find the minimum value when x is between 0 and 100.
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)
Output
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.

思路:

因为是求原函数最小值,考虑对该函数求导由极小值点找最小值点,因为导函数

y=42*x^6+48*x^5+21*x^2+10*x单调递增,所以导函数的零点也是极小值点就是最小值点。对导函数用二分找零点,代入原函数即可出答案。

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
double yuanhanshu(double x,double y)
{
	return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x;
}
double daoshu(double x)
{
	return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x;
}
int main()
{
	int t;
	double y,L,R,mid;
	cin>>t;
	while(t--)
	{
		cin>>y;
		L=0.000;R=100.000;
		while(R-L>1e-7)
		{
			mid=(L+R)/2;
			if(daoshu(mid)>y) R=mid;
			else L=mid;
		}
		printf("%.4lf\n",yuanhanshu(mid,y));
	}
	return 0;
}

睡觉~,新学期加油!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值