Postal Vans
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
/*
ID: 15114582
PROG: vans
LANG: C++
*/
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
struct BigNum
{
int s[111],w;
BigNum()
{
w=1;
memset(s,0,sizeof(s));
}
void add(BigNum a)
{
if(a.w>w)w=a.w;
for(int i=0;i<w;++i)
{
s[i]+=a.s[i];
s[i+1]+=s[i]/100000;
s[i]%=100000;
}
while(s[w])++w;
}
void out()
{
printf("%d",s[w-1]);
for(int i=w-2;i>=0;--i)
printf("%05d",s[i]);
puts("");
}
}f[1111][6],ans;
int i,j,n;
int main()
{
freopen("vans.in","r",stdin);
freopen("vans.out","w",stdout);
f[1][0].s[0]=f[1][1].s[0]=1;
for(i=2;i<1001;++i)
{
f[i][0].add(f[i-1][0]);
f[i][0].add(f[i-1][2]);
f[i][0].add(f[i-1][4]);
f[i][1]=f[i][0];
f[i][1].add(f[i-1][3]);
f[i][2].add(f[i-1][1]);
f[i][2].add(f[i-1][5]);
f[i][3]=f[i-1][1];
f[i][4]=f[i][2];
f[i][5]=f[i][2];
}
while(~scanf("%d",&n))
{
ans=f[n-1][1];
ans.add(f[n-1][5]);
ans.add(ans);
ans.out();
}
return 0;
}
本文提供了一种求解邮递员路径问题的算法,该问题涉及在一个网格状郊区中,邮递员如何从邮局出发,经过每一个交叉口恰好一次,并返回邮局。文中通过详细解释算法步骤和关键数学原理,展示了如何用大数运算来解决此类问题。通过实例演示和代码实现,读者可以理解并应用到类似路径规划问题中。
504

被折叠的 条评论
为什么被折叠?



