HDU 2614 Beat 【DFS】

题目

Zty is a man that always full of enthusiasm. He wants to solve every kind of difficulty ACM problem in the world. And he has a habit that he does not like to solve
a problem that is easy than problem he had solved. Now yifenfei give him n difficulty problems, and tell him their relative time to solve it after solving the other one.
You should help zty to find a order of solving problems to solve more difficulty problem.
You may sure zty first solve the problem 0 by costing 0 minute. Zty always choose cost more or equal time’s problem to solve.

输入

The input contains multiple test cases.
Each test case include, first one integer n ( 2 < n < 15 ) n ( 2< n < 15) n(2<n<15). express the number of problem.
Than n n n lines, each line include n n n integer T i j ( 0 < = T i j < 10 ) T_{ij} ( 0<=T_{ij}<10) Tij(0<=Tij<10), the i i i’s row and j j j’s col integer T i j T_{ij} Tij express after solving the problem i i i, will cost T i j T_{ij} Tij minute to solve the problem j j j.

输出

For each test case output the maximum number of problem zty can solved.

样例输入

3
0 0 0
1 0 1
1 0 0
3
0 2 2
1 0 1
1 1 0
5
0 1 2 3 1
0 0 2 3 1
0 0 0 3 1
0 0 0 0 2
0 0 0 0 0

样例输出

3
2
4
提示

sample one, as we know zty always solve problem 0 0 0 by costing 0 0 0 minute.
So after solving problem 0 0 0, he can choose problem 1 1 1 and problem 2 2 2, because T 01 ≥ 0 T_{01} \ge 0 T010 and T 02 ≥ 0 T_{02} \ge 0 T020.
But if zty chooses to solve problem 1 1 1, he can not solve problem 2 2 2, because T 12 < T 01 T_{12} < T_{01} T12<T01.
So zty can choose solve the problem 2 2 2 second, than solve the problem 1 1 1.

题目分析

得到的二维数组中 T i j T_{ij} Tij表示解决问题 i i i后解决问题 j j j的耗时。因为题目耗时要不小于上一题,得到解题顺序 i , j , k i,j,k i,j,k的关系式 T i j ≤ T j k T_{ij} \le T_{jk} TijTjk。以此比较,即采用深度优先搜索策略。

代码

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

int n,a[21][21],b[16],max_n,cnt;

int MAX(int x,int y)
{
	return x>y?x:y;
}

void MaxPro(int x,int y)\\dfs
{
	max_n=MAX(max_n,cnt);
	if(max_n == n)
		return ;
	for(int i = 1;i<n;++i)
	{
		if(b[i] != 1)
			if(a[y][i]>=a[x][y])\\时间关系式比较
			{
				b[i]=1;
				++cnt;
				MaxPro(y,i);\\解决当前问题i
				--cnt;
				b[i]=0;
			}
	}
}

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		memset(b,0,sizeof(b));\\刷新
		for(int i = 0;i<n;++i)
			for(int j = 0;j<n;++j)
				scanf("%d",&a[i][j]);
		cnt=2;
		max_n=0;
		b[0]=1;
		for(int i = 1;i < n;++i)\\依次设置开始题目
		{
			b[i]= 1;
			MaxPro(0,i);
			b[i]= 0;
		}
		cout<<max_n<<endl;
	}
	return 0;
}

运行结果

在这里插入图片描述

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=2614

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

registor11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值