usaco training 6.1.1 Postal Vans 题解

转载请注明出处:http://blog.csdn.net/jiangshibiao/article/details/21446033

【原题】

Postal Vans

ACM South Pacific Region -- 2003

Tiring of their idyllic fields, the cows have moved to a new suburb. The suburb is a rectangular grid of streets with a post office at its Northwest corner. It has four avenues running East-West and N (1 <= N <= 1000) streets running North-South.

For example, the following diagram shows such a suburb with N=5 streets, with the avenues depicted as horizontal lines, and the post office as a dark blob at the top-left corner:

Each day the postal van leaves the post office, drives around the suburb and returns to the post office, passing exactly once through every intersection (including those on borders or corners). The executives from the post company want to know how many distinct routes can be established for the postal van (of course, the route direction is significant in this count).

For example, the following diagrams show two such routes for the above suburb:

As another example, the following diagrams show all the four possible routes for a suburb with N=3 streets:

Write a program that will determine the number of such distinct routes given the number of streets.

PROGRAM NAME: vans

INPUT FORMAT

  • Line 1: A single integer, N

SAMPLE INPUT (file vans.in)

4

OUTPUT FORMAT

  • Line 1: A single integer that tells how many possible distinct routes corresponding to the number of streets given in the input.

SAMPLE OUTPUT (file vans.out)

12


【译题】

描述

郊区呈矩形,有四条东西方向的街道和N(1<=N<=1000)条南北方向的街道。在郊区的西北角有一个邮局。

如N=5时,郊区如下图所示,圆点表示邮局,直线表示街道。

postal1.gif

每天邮政卡车从邮局出发,每个十字路口(包括边界和四角)经过且只经过一次。现在邮局希望知道邮政货车行驶的路线有几种。 例如,下面两幅图表示的是满足上图的两条路线

postal2.gif

另一个例子,下面四幅图表示了当N=3时的全部四种情况

postal3.gif

[编辑]格式

PROGRAM NAME: vans

INPUT FORMAT:

(file vans.in)

INPUT FORMAT 一行:一个数值N

OUTPUT FORMAT:

(file vans.out) 一行: 到INPUT中给出的街道的路径总数

[编辑]SAMPLE INPUT

 4

[编辑]SAMPLE OUTPUT

 12

wcada


【前言】参考了pty大神的详细推导,于是把题解原封不动地写在这里。

【题解】

----------------------------------------------原文始-----------------------------------------------------

预备知识:

哈密度路:由一个点出发到另外一个点结束,要求经过图中所有的点的一条路(不能重复经过点)。

哈密顿回路:从一个点出发再回到此点,经过图中所有点的一条路(不能重复经过点)。

 

问题显然的解法:对于一个n*4的图求哈密顿回路的个数。用陈丹琦的方法。

但是由于宽度只有4,所以有另外一种递推的方法,达到优化的目的:

设f[i]为前i列中,第i列的第一个格子到第二个格子的哈密度路的条数。(显然,f[n]就为答案)


1         2          3   。。i-1     i

设g[i]为前i列中,第i列的第一个格子到第四个格子的哈密顿路的条数。


1           2         3   。。i-1     i

很显然的:f[i]=f[i-1]+g[i-1]

证明第i列一二号格子的只能由两种方式得到:

1、 =f[i-1]  2、=g[i-1]

下面介绍g[i]的递推方法:

分四种情况进行讨论:g[i]=g1[i]+g2[i]+g3[i]+g4[i]

1、 =g1[i]=f[i-1]   2、 =g2[i]=f[i-1]

3、 =g3[i]=g[i-2]   4、 g4[i]

又分三种情况讨论

 =f[i-2]   =f[i-2]

有没有发现这种情况就是第i-1列的g4[i-1]

所以g4[i]=g4[i-1]+f[i-2]*2=g[i-1]-g1[i-1]-g2[i-1]-g3[i-1]+f[i-2]*2=g[i-1]-g[i-3]

所以g[i]=g1[i]+g2[i]+g3[i]+g4[i]=f[i-1]*2+g[i-1]+g[i-2]-g[i-3](比较优美吧。呵呵)

两式联立:f[i]=f[i-1]+g[i-1]

----------------------------------------------原文完-----------------------------------------------------

【代码1】非高精

#include<cstdio>
using namespace std;
int n,i,f[1001],g[1001];
int main()
{
  scanf("%d",&n);
  g[1]=2;g[2]=2;g[3]=8;
  f[1]=0;f[2]=2;f[3]=4;
  for (i=4;i<=n;i++)
  {
    g[i]=f[i-1]*2+g[i-1]+g[i-2]-g[i-3];
    f[i]=f[i-1]+g[i-1];
  }
  printf("%d",f[n]);
}

【代码2】高精

/*
PROG:vans
ID:juan1973
LANG:C++
*/
#include<cstdio>
using namespace std;
struct arr{int n,p[1001];}g[1001],f[1001];
int n,i;
arr chen(arr a)
{
  for (int i=1;i<=a.n;i++)
    a.p[i]*=2;
  for (int i=1;i<=a.n;i++)
    if (a.p[i]>9) {a.p[i+1]+=a.p[i]/10;a.p[i]%=10;}
  if (a.p[a.n+1]>0) a.n++;
  return a;
}
arr add(arr a,arr b)
{
  a.n=a.n>b.n?a.n:b.n;
  for (int i=1;i<=a.n;i++)
    a.p[i]+=b.p[i];
  for (int i=1;i<=a.n;i++)
    if (a.p[i]>9) {a.p[i+1]+=a.p[i]/10;a.p[i]%=10;}
  if (a.p[a.n+1]>0) a.n++;
  return a;
}
arr Minus(arr a,arr b)
{
  int i=1,j,k;  
  while (i<=b.n)  
  {  
    if (a.p[i]>=b.p[i])a.p[i]=a.p[i]-b.p[i];  
    else  
    {  
      j=i+1;  
      while (a.p[j]==0) j++;  
      a.p[j]--;  
      for (k=i+1;k<j;k++) a.p[k]=9;  
      a.p[i]=a.p[i]+10-b.p[i];  
    }  
    i++;  
  }  
  while (a.p[a.n]==0&&a.n>1)a.n--;  
  return a;  
}  
int main()
{
  freopen("vans.in","r",stdin);
  freopen("vans.out","w",stdout);
  scanf("%d",&n);
  g[1].p[1]=2;g[2].p[1]=2;g[3].p[1]=8;
  g[1].n=g[2].n=g[3].n=1;
  f[1].p[1]=0;f[2].p[1]=2;f[3].p[1]=4;
  f[1].n=f[2].n=f[3].n=1;
  for (i=4;i<=n;i++)
  {
    g[i]=Minus(add(chen(f[i-1]),add(g[i-1],g[i-2])),g[i-3]);
    f[i]=add(f[i-1],g[i-1]);
  }
  for (i=f[n].n;i>0;i--)
    printf("%d",f[n].p[i]);
  printf("\n");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值