URAL 1119Metro dp练习

来源:互联网 发布:数据分享网站 编辑:程序博客网 时间:2024/05/29 06:55
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.
Problem illustration
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 c


题目很简单 但是要注意题目给出的是坐标 ,而坐标表示与我们平时的矩阵先行后列的习惯是相反地,故写入的时候要注意; 

并且题目给的坐标都是块的坐标不是点的坐标 不注

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;double dp[1100][1100];int map1[1100][1100];int main(){    int n,m;    while(scanf("%d%d",&n,&m)!=EOF)    {       int k;       scanf("%d",&k);       for(int i = 0 ; i <= m; i++)        for(int j = 0; j <= n; j++)         map1[i][j] = 0;       for(int i = 1; i <= k; i++)       {           int x,y;           scanf("%d%d",&x,&y);           map1[y][x] = 1;       }       dp[0][0] = 0;       for(int i = 1; i <= m; i++)        dp[i][0] = dp[i-1][0] + 100;       for(int i = 1; i <= n; i++)        dp[0][i] = dp[0][i-1] +100;       double num = sqrt(2)*100;       for(int i = 1; i <= m; i++)       {           for(int j = 1; j <= n; j++)           {               dp[i][j] = min(dp[i-1][j],dp[i][j-1])+100;               if(map1[i][j] == 1)               {                   dp[i][j] = min(dp[i][j],dp[i-1][j-1]+num);               }           }       }       printf("%.0f\n",dp[m][n]);    }    return 0;}

意也可能出错


0 0