zoukankan      html  css  js  c++  java
  • hdu 2125 Local area network 简单dp之pair初体验

    A local area network (LAN) supplies networking capability to a group of computers in close proximity to each other such as in an office building, a school, or a home. A LAN is useful for sharing resources like files, printers, games or other applications. A LAN in turn often connects to other LANs, and to the Internet or other WAN.
    In this contest, everybody is connecting with each others like the following figure.

    The contest’s network was built as N rows and M columns, your computer locate at (0, 0) and the judger’s computer locate at (m-1, n-1) The code you submit would only transfer to up or right smoothly. It doesn’t matter if some accidents happened. Could you tell me how many ways from your computer to judger when a data wire was broken?

    Input
    There are multiple cases. Every case contains two integers in the first line, N, M (3<=N+M<=40). Second line contains four integers X1, Y1, X2, Y2 (0<=X1, X2<M, 0<=Y1, Y2<N, |X1+Y1-X2-Y2|=1), meaning the broken wire position.
    Output
    Print the answer in one line.
    Sample Input
    3 3
    0 0 1 0
    Sample Output
    3

    并不是什么难题,看一眼就知道的简单dp思想
    想找个机会使用一下pair
    然而这道其实没有必要使用
    记一下pair语法
    声明和赋值
    pair<int ,int >p (5,6);
    pair<int ,int > p1= make_pair(5,6);
    pair<string,double> p2 (“aa”,5.0);
    引用时.second
    值得一提的是 pair 比较规则为
    先比较first 再比较second
    上代码

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<iostream>
    using namespace std;
    long long dp[45][45];
    void print(int n,int m)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
                printf("%lld ",dp[i][j]);
            cout<<endl;
        }
        cout<<endl;
        return ;
    }
    int main()
    {
    
        int n,m;
        while(~scanf("%d%d",&n,&m))
        {
            int flag=0;//1 heng 2 shu
            pair<int,int>coor1,coor2,coor3;
            int tmp1,tmp2,tmp3,tmp4;
            scanf("%d%d%d%d",&tmp1,&tmp2,&tmp3,&tmp4);
            if(tmp1==tmp3) flag=1;
            else flag=2;
            coor1=make_pair(tmp1,tmp2);
            coor2=make_pair(tmp3,tmp4);
            coor3=max(coor1,coor2);
            //cout<<coor3.first<<endl;
            //cout<<coor3.second<<endl;
            dp[0][0]=1;
            for(int i=1; i<n; i++)
            {
                if(flag==1&&coor3.second==i&&coor3.first==0)
                    dp[0][i]=0;
                else dp[0][i]=dp[0][i-1];
            }
            for(int i=1; i<m; i++)
            {
                if(flag==2&&coor3.first==i&&coor3.second==0)
                    dp[i][0]=0;
                else dp[i][0]=dp[i-1][0];
            }
            if(m==1||n==1)
            {
                printf("%lld
    ",dp[n-1][m-1]);
                continue;
            }
            for(int i=1; i<m; i++)
            {
                for(int j=1; j<n; j++)
                {
                    if(coor3.first==i&&coor3.second==j)
                    {
                        if(flag==1) dp[i][j]=dp[i-1][j];
                        else dp[i][j]=dp[i][j-1];
                    }
                    else dp[i][j]=dp[i-1][j]+dp[i][j-1];
                }
            }
            //print(n,m);
            printf("%lld
    ",dp[m-1][n-1]);
        }
        return 0;
    }
    //caowenbo
    
  • 相关阅读:
    C#static
    Sql中CHARINDEX用法
    分分钟用上C#中的委托和事件
    为什么使用抽象类?有什么好处?
    【NOIP】普及组2009 细胞分裂
    【VIJOS】P1512 SuperBrother打鼹鼠
    【NOIP】提高组2014
    @NOIP2018
    @NOIP2018
    @NOIP2018
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852339.html
Copyright © 2011-2022 走看看