【问题描述】
一个城市的道路成了像棋盘那样的网状,南北向的路有n条,并由西向东从1标记到n,东西向的路有m条,并从南向北从1标记到m,每一个交叉点代表一个路口,有的路口有正在等车的乘客。一辆公共汽车将从(1,1)点驶到(n,m)点,车只能向东或者向北开.
写一个程序,告诉司机怎么走能接到最多的乘客。
【数据范围限制】
100%的数据:1 <= n <= 103, 1 <= m <= 103, 1 <= k <= 103;
每个路口的乘客数量不超过1000000。
代码:
#region 公共汽车接最多人
public void GetBusMax()
{
int[,] road = null;
int[,] MAX = null;
int rows = 0;
int conlums = 0;
StreamReader sr = File.OpenText("bus.in");
StreamWriter sw = File.CreateText("bus.out");
string[] firstLine = sr.ReadLine().Split(" ".ToCharArray());
rows = int.Parse(firstLine[0]);
conlums = int.Parse(firstLine[1]);
int k = int.Parse(firstLine[2]);
road = new int[rows + 1, conlums + 1];
MAX = new int[rows + 1, conlums + 1];
string strLine = null;
int n = 0;
int m = 0;
while ((strLine = sr.ReadLine()) != null)
{
string[] str = strLine.Split(" ".ToCharArray());
n = int.Parse(str[0]);
m = int.Parse(str[1]);
k = int.Parse(str[2]);
road[n, m] = k;
}
MAX[1, 1] = road[1, 1];
for (int i = 1; i < rows + 1; i++)
{
for (int j = 1; j < conlums + 1; j++)
{
if (i > 1 && j > 1)
{
if (MAX[i - 1, j] > MAX[i, j - 1])
{
MAX[i, j] = MAX[i - 1, j] + road[i, j];
}
else
{
MAX[i, j] = MAX[i, j - 1] + road[i, j];
}
}
else if (i > 1)
{
MAX[i, j] = MAX[i - 1, j] + road[i, j];
}
else if (j > 1)
{
MAX[i, j] = MAX[i, j - 1] + road[i, j];
}
else
{
MAX[1, 1] = road[1, 1];
}
}
}
sw.WriteLine(MAX[rows, conlums]);
sr.Close();
sw.Close();
}
#endregion
这篇博客探讨了一个城市网格状道路中,公交车如何从(1,1)出发到达(n,m)能接最多乘客的问题。题目描述了数据范围限制,并提出了编程挑战,要求写出能确定最优行驶路线的程序。每个路口的乘客数量被限制在1000000以内。"
104861134,7925799,修复Chromebook HP C732-CWU连接MT7628 Wi-Fi错误,"['无线网卡', 'Wi-Fi连接', '驱动修复', '嵌入式系统']
3032

被折叠的 条评论
为什么被折叠?



