zoukankan      html  css  js  c++  java
  • Ural 1119 Metro

    Problem Description
    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 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
    inputoutput
    3 2
    3
    1 1
    3 2
    1 2
    
    383
    

    如果对角线没有穿越:

    dp[i][j]=min(dp[i-1][j] ,dp[i][j-1])+100

    如果对角线有穿越:

    dp[i][j]=min(dp[i][j] ,dp[i-1][j-1]+sqrt(2.0)*100)

    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #define MAXN 1005
    
    int n,m;
    bool g[MAXN][MAXN];
    double dp[MAXN][MAXN];
    
    double min(double a, double b){
    	if(a<b)return a;
    	else return b;
    }
    
    int main(){	
    	int t,x,y;
    	while( scanf("%d %d",&n ,&m)!=EOF ){
    		scanf("%d",&t);
    		memset(g ,0 ,sizeof(g));
    		int i,j;
    		for(i=0; i<t; i++){
    			scanf("%d %d" ,&x ,&y);
    			g[y][x]=1;	
    		}
    		for(i=1; i<=n; i++){
    			dp[0][i]=dp[0][i-1]+100;
    		}
    		for(i=1; i<=m; i++){
    			dp[i][0]=dp[i-1][0]+100;
    			for(j=1; j<=n; j++){
    				dp[i][j]=min(dp[i-1][j] ,dp[i][j-1])+100;
    				if(g[i][j]){
    					dp[i][j]=dp[i-1][j-1]+sqrt(2.0)*100;
    				}
    			}
    		}
    		printf("%.0lf
    ",dp[m][n]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    Jaxb2 实现JavaBean与xml互转
    标准输入与标准输出
    linux 一行一行的读取文件
    linux $* 和$@ if [ ](字符串比较)
    scala 学习(三)——Array和ArrayBuffer
    Shell编程(六)awk工具
    Shell编程(五)脚本语法
    Shell编程(四)Shell变量
    Shell编程(三)Shell特性
    Shell编程(一)概览
  • 原文地址:https://www.cnblogs.com/chenjianxiang/p/3636865.html
Copyright © 2011-2022 走看看