HDU(杭州电子科技大学) 2614 Beat (BFS写法)

Beat

Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

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. 
         
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;

int map[16][16],ans,n;//画地图 
bool vis[16];//标记是否来过 

struct AC
{
	int pos,maxT,count;
	bool vis[16];
}node,temp;

void bfs ()
{
	queue<AC>q;
	node.count = 1;//计算解决了问题的个数,至少解决一个 
	node.pos = 1;//坐标的横坐标为变化值 
	node.maxT = 0;//记录最大Tij 
	node.vis[1]=true;//表示来访过 
	
	q.push(node);
	while (!q.empty())
	{
		AC top = q.front();
		q.pop();
		ans = max(ans,top.count);
		int p = top.pos,maxT = top.maxT;
		for (int i=1;i<=n;i++)
		{
			if (i!=p && top.vis[i]==false && map[p][i]>=maxT)//首先Tij中i和j不能相等,我这里的top.pos就是横坐标
			//其次要记录来访过,最后要标记Tij的最大值 
			{
				temp=top;//这点很重要,为什么要先把top赋值给temp,因为要加上上一个节点的count和vis[i] 
				temp.pos = i;
				temp.vis[i] = true;
				temp.count++;
				temp.maxT = map[p][i];
				
				q.push(temp);
			}
		}
	}
}

int main()
{
	
	while (cin>>n)
	{
		
		for (int i=1;i<=n;i++) //我的图的下标是从1开始的 
		{
			for (int j=1;j<=n;j++)
			{
				cin>>map[i][j];
			}
		}
		ans = -1;
		bfs();
		cout<<ans<<endl;
	}//看见好像没有这题的bfs解法,我写了下 
}

猜你喜欢

转载自blog.csdn.net/qq_40763929/article/details/81748998