HDU2614 Beat题解【DFS】

一、原文:

Description
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.

Input
The input contains multiple test cases.
Each test case include, first one integer n ( 2< n < 15).express the number of problem.
Than n lines, each line include n integer Tij ( 0<=Tij<10), the i’s row and j’s col integer Tij express after solving the problem i, will cost Tij minute to solve the problem j.
Output
For each test case output the maximum number of problem zty can solved.

Sample Input
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
Sample Output
3
2
4
Hint

Hint: sample one, as we know zty always solve problem 0 by costing 0 minute.
So after solving problem 0, he can choose problem 1 and problem 2, because T01 >=0 and T02>=0.
But if zty chooses to solve problem 1, he can not solve problem 2, because T12 < T01.
So zty can choose solve the problem 2 second, than solve the problem 1.

二、思路:

根据题目要求,我们必须先解决第0道题目,第0道题目花费时间为0,加下来我们继续找“ 第1道 ~ 第n-1道 “题目中没有被解决过得且花费时间必须大于上一次的花费的问题
因此,深搜的每一步的思路很明确,即找到上面黑体字的要求。题目中没有被解决过得且花费时间必须大于上一次的花费的问题。在搜索的过程中如果当前做出来的题目数量大于目前计算出来的最大数量我们就更新最大数量值。当把所有可能的情况都搜索一遍时,就得出了最大值。

三、如何解决关键问题。

由上面的思路可以得出。关键问题是解决"题目中没有被解决过得且花费时间必须大于上一次的花费的问题"。
代码如下:

/* 如果j被访问过,并且G[i][j] >= 当前的困难值,就访问第j个问题*/
for (int j = 0; j < n; j++) {
		if (!vis[j] && G[i][j] >= diff) {
			dfs(cnt + 1, j, G[i][j]);
		}
	}

四、代码加解释:

  1. 首先我们新建变量n表示问题个数,ans表示最多可解决的问题个数。G[20][20]表示问题难度的相对关系。vis[20]标志每个问题是否被访问。
int n, ans;
int G[20]20];
bool vis[20];
  1. 其次,我们在主函数进行输入:
while (cin >> n) {
   	for (int i = 0; i < n; i++) {
   		for (int j = 0; j < n; j++) {
   			cin >> G[i][j];
   		}
   	}
   /* 在这里调用DFS函数并输出结果 */
   }
  1. DFS函数
/*
   cnt:	表示当前已经解决的问题个数。
   i:	表示当前正在解决第i个问题。
   diff:	表示当前解决问题的困难值。(difficulty)
*/
void dfs(int cnt, int i, int diff) {
   /*更新最大值*/ 
   if (cnt > ans) {
   	ans = cnt;
   }
   
   /*访问下一个元素,记得要标记vis数组*/
   vis[i] = true;
   for (int j = 0; j < n; j++) {
   	if (!vis[j] && G[i][j] >= diff) {
   		dfs(cnt + 1, j, G[i][j]);
   	}
   }
   vis[i] = false;
} 
  1. 输出结果。
  2. 全部代码如下:
#include <iostream>
using namespace std;

int n, ans;
int G[20][20];
bool vis[20];

void dfs(int cnt, int i, int diff) {
	if (cnt > ans) {
		ans = cnt;
	}
	vis[i] = true;
	for (int j = 0; j < n; j++) {
		if (!vis[j] && G[i][j] >= diff) {
			dfs(cnt + 1, j, G[i][j]);
		}
	}
	vis[i] = false;
} 

int main() {
	while (cin >> n) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> G[i][j];
			}
		}
		ans = 0;
		dfs(1, 0, 0);
		cout << ans << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值