Metro(Ural_1119)
来源:互联网 发布:大华130万网络摄像机 编辑:程序博客网 时间:2024/05/23 19:16
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.
Input
There are two integers in the first line: N and M (0 < N,M ≤ 1000) — west-east and south-north sizes of the grid. Nikifor starts his way from a crossroad which is situated south-west of the quarter with coordinates (1, 1). A Metro station is situated north-east of the quarter with coordinates (N, M). The second input line contains a number K (0 ≤ K ≤ 100) which is a number of quarters that can be crossed diagonally. Then K lines with pairs of numbers separated with a space follow — these are the coordinates of those quarters.
Output
Your program is to output a length of the shortest route from Nikifor’s home to the Metro station in meters, rounded to the integer amount of meters.
Sample input output
3 2
3
1 1
3 2
1 2
383
Problem Author: Leonid Volkov
Problem Source: USU Open Collegiate Programming Contest October’2001 Junior Session
这题就是一道简单的动规,设dp[i][j]为到达坐标(i,j)时的最短路,那么到达该坐标的方法一共只有三种,从西边来,从南边来和从西南边来,从 到达这三种状态的最短路径长度加上从这三种状态再到(i,j)的路径长度 中找出最小的那个,就是到达(i,j)的最短路。(囧rz
代码
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int dia[1010][1010];double dp[1010][1010];int main(){ int n, m; int a, b, c; memset(dia, 0, sizeof(dia)); memset(dp, 0, sizeof(dp)); scanf("%d%d", &n, &m); scanf("%d", &a); for(int i = 0; i < a; i++) { scanf("%d%d", &b, &c); dia[b][c] = 1; } for(int i = 1; i <= n; i++) dp[i][0] = dp[i-1][0] + 100; for(int i = 1; i <= m; i++) dp[0][i] = dp[0][i-1] + 100; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { dp[i][j] = min(dp[i][j-1], dp[i-1][j]) + 100; if(dia[i][j]) { dp[i][j] = min(dp[i][j], dp[i-1][j-1] + 141.421356); //注意精度,这里一开始写了141.4,WA了 } } } printf("%.0f\n",dp[n][m]); //要求四舍五入,千万别强制转换成int,会WA return 0;}
- Metro(Ural_1119)
- Metro
- Metro
- Metro
- URAL1119 Metro
- win8 metro
- metro client
- metro 尝新
- Metro UI
- metro serialize
- win8 metro
- 使用Metro Studio制作Metro样式图标
- Windows8 Metro开发 (01) : Metro应用程序生命周期
- Sun Metro分析
- Geoserver发布metro服务
- Metro of SH
- ural 1119. Metro
- Metro-用户手册-目录
- IOS开发for循环创建button
- ExpandableListView的用法
- NOIp 2009 多项式输出
- Python碎片小知识点总结
- 201603252026
- Metro(Ural_1119)
- 如何使用BMap.Point传递变量、存储数据?
- 各类缩写总结
- Python环境的安装
- POJ 3468 A Simple Problem with Integers (线段树区间更新模板)
- 网易3.22号笔试(android)
- 20160325 Codevs 2924 挑战数独,2806 红与黑,4163 hzwer与逆序对
- Qt OpenGL:学习现代3D图形编程之二,玩转色彩
- android Scroll学习