Beat

                                                                                                Beat

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.

题目意思:
一个孩子要做N个题目,每个题目难度不同,但这个孩子很傲娇,他不做比之前做过的题更简单的题,即他做的第二题用的时间不能比第一题少;题目中的Tij代表做完i题之后做J题所需的时间。求这个孩子最多能做多少道题。

这个问题比较坑,主要就在于题目意思的不好理解,给的范例也不典型。。。其总体思想就是深搜,如果用向量打表的话,广搜也可以尝试一下,
坑的地方在哪儿呢?就是题目中所给的第零道题的时间是零,而这个零和所给范例的第一行第一列的零正好重合,,,实际上这个数比不代表第零个题,,,

代码如下:
//感觉深搜可以
#include<iostream>
#include<cstring>
using namespace std;

int mark[20][20] = {0};
int num = 1;
int countt = 1;
int maxx = 0;
void dfs(int deep,int col) {
 int p = 0;
 for (int i = 0; i < num; i++)
 {
  int fu;
  if (deep == 0 && col == 0) fu = 0;
  else fu = mark[col][deep];
  if (fu<= mark[deep][i]&&deep!=i)
  {
   p = 1;
   for (int k = 0; k < num; k++)
   {
    mark[k][deep] -= 100;
   }
   countt++;
   dfs(i, deep);
   for (int k = 0; k < num; k++)
   {
    mark[k][deep] += 100;
   }
   countt--;
  }
 }
 if (0 == p) {
  if (countt > maxx)
   maxx = countt;
  return;
 }
}


int main() {
 while (cin>>num)
 {
  countt = 1;
   maxx = 0;
  memset(mark, 0, sizeof(mark));
  for (int i = 0; i < num; i++)
  {
   for (int j = 0; j < num; j++)
   {
    cin >> mark[i][j];
   }
  }
  dfs(0, 0);
  cout << maxx << endl;
 }
 return 0;
}

注意事项:再深搜的时候做标记,可以用一个数组来实现,并不一定要在原数组上操作,,,

优化代码:
#include <iostream>
#include <string.h>
using namespace std;
int problem[16][16], vis[16];
int n, maxx;

void dfs(int line, int pre, int sl)
{
 int i, flag = 0;
 for (i = 0; i<n; ++i)
 {
  // 当前i是否访问过,或者i是否就是当前站点
  if (i == line || vis[i] == 1)    continue;

  if (problem[line][i] >= pre)
  {
   vis[i] = 1;
   dfs(i, problem[line][i], sl + 1);
   vis[i] = 0;
   flag = 1;
  }
 }
 if (!flag)  maxx = sl>maxx ? sl : maxx;
}

int main()
{
 int i, j;
 while (cin >> n)
 {
  memset(vis, 0, sizeof(vis));
  for (i = 0; i<n; ++i)
   for (j = 0; j<n; ++j)
    cin >> problem[i][j];

  // 初始化maxx,开始dfs
  vis[0] = 1;
  maxx = -1;
  dfs(0, 0, 1);
  cout << maxx << endl;
 }
 return 0;
}

精简了许多,,



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值