# Metro

44人阅读 评论(0)

http://acm.timus.ru/problem.aspx?space=1&num=1119

Many of SKB Kontur programmers like to get to work by Metro because the main office is situated quite close the station Uralmash. So, since a sedentary life requires active exercises off-duty, many of the staff — Nikifor among them — walk from their homes to Metro stations on foot.
Nikifor lives in a part of our city where streets form a grid of residential quarters. All the quarters are squares with side 100 meters. A Metro entrance is situated at one of the crossroads. Nikifor starts his way from another crossroad which is south and west of the Metro entrance. Naturally, Nikifor, starting from his home, walks along the streets leading either to the north or to the east. On his way he may cross some quarters diagonally from their south-western corners to the north-eastern ones. Thus, some of the routes are shorter than others. Nikifor wonders, how long is the shortest route.
You are to write a program that will calculate the length of the shortest route from the south-western corner of the grid to the north-eastern one.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cstring>
#include <functional>
#include <cmath>
using namespace std;

bool path[1010][1010];
double dp[1010][1010];
int main()
{
//freopen("in.txt", "r", stdin);
int m, n;
while (~scanf("%d %d",&n,&m))
{
memset(path, 0, sizeof(path));
int k, x, y;
scanf("%d", &k);
for (int i = 0; i < k; i++)
{
scanf("%d %d", &x, &y);
path[x][y] = true;
}
dp[0][0] = 0;
for (int i = 1; i < n + 1; i++)
dp[i][0] = dp[i - 1][0] + 1;
for (int i = 1; i < m + 1; i++)
dp[0][i] = dp[0][i-1] + 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m;j++)
{
if (path[i][j]==true)
{
dp[i][j] = min(dp[i - 1][j] + 1, min(dp[i][j - 1] + 1, dp[i - 1][j - 1] + sqrt(2.0f)));
}
else
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);
}
printf("%.f", dp[n][m] * 100);

}
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：3174次
• 积分：597
• 等级：
• 排名：千里之外
• 原创：59篇
• 转载：0篇
• 译文：0篇
• 评论：0条
友情链接
评论排行