poj 3038

Flying Right

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1580   Accepted: 557

Description

Figuring that they cannot do worse than the humans have, Farmer John‘s cows have decided to start an airline. Being cows, they decide to cater to the heretofore-untapped market of cows as passengers. They plan to serve the cows who live along the western coast of Lake Michigan. Each morning, they will fly from the northern-most point of the coast southward towards Chicowgo, making many stops along the way. Each evening, they will fly back north to the northern-most point.

They need your help to decide which passengers to carry each day. Each of N (1 <= N <= 10,000) farms numbered 1..N along the coast contains an airport (Farm 1 is northern-most; farm N is southern-most). On this day, K (1 <= K <= 50,000) groups of cows wish to travel.Each group of cows wants to fly from a particular farm to another particular farm. The airline, if it wishes, is allowed to stop and pick up only part of a group. Cows that start a flight, however,must stay on the plane until they reach their destination.

Given the capacity C (1 <= C <= 100) of the airplane and the groups of cows that want to travel, determine the maximum number of cows that the airline can fly to their destination.

Input

* Line 1: Three space-separated integers: K, N, and C

* Lines 2..K+1: Each line contains three space-separated integers S, E, and M that specify a group of cows that wishes to travel. The M (1 <= M <= C) cows are currently at farm S and want to travel to farm E (S != E).

Output

* Line 1: The maximum number of cows that can be flown to their destination. This is the sum of the number of cows flown to their destination on the flight southward in the morning plus the number of cows flown to their destination on the flight northward in the evening.

Sample Input

4 8 3
1 3 2
2 8 3
4 7 1
8 3 2

Sample Output

6

Hint

INPUT DETAILS: 
Four groups of cows, eight farms, and three seats on the plane.

OUTPUT DETAILS: 
In the morning, the flight takes 2 cows from 1->3, 1 cow from 2->8,and 1 cow from 4->7. In the evening, the flight takes 2 cows from 8->3.

思想很好

**注意三个问题:
* 1.使用DFS计算左转优先和右转优先的路径,使用BFS计算最短路径
* 2.这里的DFS不需要标记,因为按照方向顺时针(或逆时针)前进时,除非无路可走才会返回,所以不会因为没有标记而出现问题,不过它的前进方向是相对,是相对当前位置进行左上右下(右前左后)探索(这个相对性非常重要),最初的方向由起点S确定,而下一步的方向则由前一步的走向决定
以顺时针,左手优先为例,即(相对于当前方向,左转90°后的方向为第一优先,依次右转90°找可能的通路)
如何左转90度?0,1,2,3代表四个方向,显示直接-1是不行的,可能变成负值了,那么可以用
(d+3)%4,就可以左转90度到优先位置,当然往右转的过程中,还是会超过0,1,2,3,到了外面,所以在运算时,使用d%4就可以了。
* 3.起点到终点顺时针的话,它的逆时针也可以使用顺时针所使用的DFS,只要把起点和终点倒过来就可以了,也就是DFS可以共用。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int Max = 50;
int dir[4][2] = {{0,-1},{-1,0},{0,1},{1,0}}; //以左上右下(顺时针)为基本方向数组
char map[Max][Max];
int vis[Max][Max];//广搜标记数组
int sx,sy,ex,ey;
int w,h,count,flag;
struct node
{
	int x,y;
	int dis;
}now,temp;
void dfs(int x, int y, int tx, int ty, int d)
{
	int nx,ny;
	if(x == tx && y == ty)
	{
		flag = 1;
		return;
	}
	d = (d+3)%4; //在上一个方向的基础上,左转90度到优先位置
	int i = d;
	for(i=d; i<d+4; i++)
	{
		nx = x + dir[i%4][0];
		ny = y + dir[i%4][1];
		if(nx >= 0 && nx < h && ny >= 0 && ny < w && map[nx][ny] != ‘#‘)
		{
			count++;
			d = i; //记录当前方向
			dfs(nx, ny, tx, ty, d);
			if(flag)
				return;
		}
	}
}
void bfs()
{
	queue<node> q;
	now.x = sx;
	now.y = sy;
	now.dis = 1;
	q.push(now);
	while(!q.empty())
	{
		now = q.front();
		q.pop();
		if(map[now.x][now.y] == ‘E‘)
		{
			cout<<now.dis<<endl;
			return;
		}
		for(int i=0; i<4; i++)
		{
			temp.x = now.x + dir[i][0];
			temp.y = now.y + dir[i][1];
			temp.dis = now.dis + 1;
			if(temp.x >=0 && temp.x <= h && temp.y >= 0 && temp.y < w && map[temp.x][temp.y] != ‘#‘ && !vis[temp.x][temp.y])
			{
				vis[temp.x][temp.y] = 1;
				q.push(temp);
			}
		}
	}
}
int main()
{
	int t;
	while(scanf("%d",&t) != EOF)
	{
		while(t--)
		{
	 		scanf("%d %d",&w,&h);
	 		for(int i=0; i<h; i++) //记住这里用h
	 		{
	 			getchar();
	 			for(int j=0; j<w; j++) //记住这里用w
	 			{
	 				scanf("%c",&map[i][j]);
					if(map[i][j] == ‘S‘)
	 				{
	 					sx = i;
	 					sy = j;
	 				}
	 				if(map[i][j] == ‘E‘)
	 				{
	 					ex = i;
	 					ey = j;
	 				}
	 			}
	 		}
	 			flag = 0;
	 			count = 1;
	 			dfs(sx, sy, ex, ey, 0); //顺时针,左手边,初始方向无所谓,因为只有1个出口,反正会调整好
 	 			cout<<count<<" ";
	 			flag = 0;
	 			count = 1;
 				dfs(ex, ey, sx, sy, 0); //逆时针(右手边)
 	 			cout<<count<<" ";
 	 			memset(vis, 0, sizeof(vis));
 	 			bfs();
		}
	 }
	 return 0;
}

  

时间: 2024-10-05 23:36:17

poj 3038的相关文章

dfs+bfs poj 3038

Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10331   Accepted: 4466 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombie

Flying Right POJ - 3038

有一条从南到北的航线,航线上有N个机场1-n从南到北分布,每天早上飞机从1飞到n,傍晚从n飞到1.有k组乘客,他们数量为M[k],从S飞到E,飞机上只有C个座位,计算每天飞机最多能拉多少乘客 贪心可以解决这个问题~(我一开始一直在想dp(lll¬ω¬)) 每个站点让所有乘客都上飞机,如果此时超载了,那么就让目的地离当前站点最远的乘客下飞机.可以用优先队列来维护. emmm这个代码来自 https://blog.csdn.net/severus_qin/article/details/189566

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

POJ——T2271 Guardian of Decency

http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   Accepted: 2463 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is

POJ——T2446 Chessboard

http://poj.org/problem?id=2446 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18560   Accepted: 5857 Description Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of c

poj 1088 滑雪 DP(dfs的记忆化搜索)

题目地址:http://poj.org/problem?id=1088 题目大意:给你一个m*n的矩阵 如果其中一个点高于另一个点 那么就可以从高点向下滑 直到没有可以下滑的时候 就得到一条下滑路径 求最大的下滑路径 分析:因为只能从高峰滑到低峰,无后效性,所以每个点都可以找到自己的最长下滑距离(只与自己高度有关).记忆每个点的最长下滑距离,当有另一个点的下滑路径遇到这个点的时候,直接加上这个点的最长下滑距离. dp递推式是,dp[x][y] = max(dp[x][y],dp[x+1][y]+

POJ 1385 计算几何 多边形重心

链接: http://poj.org/problem?id=1385 题意: 给你一个多边形,求它的重心 题解: 模板题,但是不知道为啥我的结果输出的确是-0.00 -0.00 所以我又写了个 if (ans.x == 0) ans.x = 0 感觉好傻逼 代码: 1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <queue> 5 #include <stack> 6