Problem 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
题目大意:
一个大佬做题喜欢按难度递增来做,做完一道题后,下一道题所花费的时间必须不少于前一道题所花费的时间,给定一个二维数组,第i行第j列 cost[i][j] 表示做完第i题之后做第j题要花费的时间为cost[i][j],题目的编号从0开始,并且第0题花费的时间为0,求这位大佬最多能做几道题。(比较简单的DFS题)
思路:
做完一道题后要在第i行搜索花费时间不少于第i题且未做过的题目,不断重复这个过程,找出最佳结果,那么采用回溯法+DFS即可
AC代码:
#include <bits/stdc++.h>
#define mem(a,n) memset(a,n,sizeof(a))
#define maxn 15
using namespace std;
int cost[maxn][maxn];//记录花费
int vis[maxn];//记录问题是否解决
int ans,n;
//x为当前要解决的问题,c为解决问题x花费的时间,cnt为当前所解决的问题数量
void dfs(int x,int c,int cnt)
{
//ans保存最大的题数,一旦出现更好的结果就更新
if(cnt>ans) ans=cnt;
//当所有的题目都解决完了或是没有符合条件的题目时就会结束递归
for(int i=1;i<n;i++)
{
//下一题必须是未解决的且花费时间>=c
if(!vis[i]&&cost[x][i]>=c)
{
vis[i]=1;
dfs(i,cost[x][i],cnt+1);
vis[i]=0;
}
}
}
int main()
{ios::sync_with_stdio(false);cin.tie(0);
while(cin>>n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>cost[i][j];
mem(vis,0);
ans=0;
dfs(0,0,1);
cout<<ans<<endl;
}
return 0;
}