ural 1119 Metro dp水
来源:互联网 发布:淘宝达人申请链接 编辑:程序博客网 时间:2024/05/20 16:33
点击打开链接
1119. Metro
Time limit: 0.5 second
Memory limit: 64 MB
Memory limit: 64 MB
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
3 231 13 21 2
383
Problem Author: Leonid Volkov
Problem Source: USU Open Collegiate Programming Contest October'2001 Junior Session
Problem Source: USU Open Collegiate Programming Contest October'2001 Junior Session
从左下角的点走到右上角的点最少可以走多少步,其中有的点可以走对角线。
第一行和第一列的点肯定无法从对角线得来,所以可以初始化。对于其余的点,有的可以从其左面或者下面得来,有的还可以从对角线得来,只要求最小即可。
状态转移方程:
如果可以从左面或者下面得来的点:dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1]+1);
如果可以从左面或者下面或者对角线得来的点:dp[i][j]=min(dp[i-1][j]+1,min(dp[i][j-1]+1,dp[i-1][j-1]+P));
开了3层for循环,原本以为会超时,1000*1000*100。结果跑了0.1568s,也是醉了。
//0.1568 110 KB#include<stdio.h>#include<algorithm>#include<math.h>#define P sqrt(2)using namespace std;double dp[1007][1007];struct N{ int x,y;}p[107];int cmp(N a,N b){ if(a.x==b.x)return a.y<b.y; return a.x<b.x;}int main(){int n,m;while(scanf("%d%d",&n,&m)!=EOF){ n++;m++; int k; scanf("%d",&k); for(int i=0;i<k;i++) scanf("%d%d",&p[i].x,&p[i].y); sort(p,p+k,cmp); dp[1][1]=0; for(int i=2;i<=n;i++)//初始化第一行 dp[i][1]=dp[i-1][1]+1; for(int i=2;i<=m;i++)//初始化第一列 dp[1][i]=dp[1][i-1]+1; for(int i=2;i<=n;i++) for(int j=2;j<=m;j++) { int flag=0; for(int a=0;a<k;a++) if(p[a].x==(i-1)&&p[a].y==(j-1))//从三个方向得来 {dp[i][j]=min(dp[i-1][j]+1,min(dp[i][j-1]+1,dp[i-1][j-1]+P));flag=1;break;} else if(p[a].x>(i-1))break; if(!flag)dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1]+1);//从两个方向得来 } printf("%.lf\n",dp[n][m]*100);}return 0;}
0 0
- ural 1119 Metro dp水
- Ural 1119 Metro(DP)
- Ural 1119 Metro(DP) .
- URAL 1119Metro dp练习
- URAL 1119 Metro (DP动态规划)
- ural 1119. Metro dp
- Ural 1119. Metro DP+精度
- ural 1119. Metro(dp)
- URAL 1119 Metro
- ural 1119 Metro
- URAL 1119Metro
- Ural 1119 Metro
- URAL 1119. Metro(简单dp)
- URAL 1119 Metro (动态规划)
- ural 1119. Metro
- ural 1119. Metro DFS
- ural 1119【dp+精度】
- URAL 2067 水dp
- VS2012中VC的可再发行组件包的位置
- 高仿微信下拉出现小眼睛动画
- Preference的组织结构
- windows上配置安装laravel
- 终有一天,你一定会很棒
- ural 1119 Metro dp水
- Java 排序算法
- 将数字转换成对应的汉语拼音
- 程序员:伤不起的三十岁
- Android MediaPlayer使用方法简单介绍
- 最快速的Android开发环境搭建ADT-Bundle及Hello World
- 在人生方向感没那么强的时候,怎么更好地生活下去
- rails无法使用页面缓存的解决办法
- Python的传值和传址与copy和deepcopy