JOJ 2039: Fire Net III

2039: Fire Net III

Status In/Out TIME Limit MEMORY Limit Submit Times Solved Users JUDGE TYPE
stdin/stdout 1s 10240K 229 63 Standard

Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.

A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.

Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.

All the maps are of the same design and only differ in size. Squares on a line from the upper-left corner to the lower-right corner and one square above each square on the line forms the street where blockhouse can be placed.

 

In this problem, we will calculate the number of different configurations of the blockhouse for a certain number of blockhouses. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map . In this problem we will consider small square cities (at most 30x30) described above.

Input

The input contains many tests.

Each test cases contains two numbers T (3 <= T <= 30)and C. T is the size of the map and C is the number of blockhouses described above.

Output

For each test cases, output a line containing the number of possible configurations.

Sample Input

3 2
3 1

Sample Output

6
5

 

Problem Source: evilll

 

/分析如下:

设函数f(n,k)中n指的是地图大小,k是指blockhouse的数量,f(n,k)指的是方法数

  JOJ 2039: Fire Net III_第1张图片

将一个5X5的地图抽象成这样的一个矩阵,可放置的位置用1表示,其他用0表示,这样一个抽象模型就出来 了。从左上角开始放起BLOCKHOUSE,无外就两种可能性。

1.将BLOCK放在左上角,那么就是f剩下(n-1,k-1)

2.第二个不把BLOCKHOUSE放在左上角,那么

所以的话f(n,k)=f(n-1,k-1)+g(n,k)

前者指的就是第一种情况,后者就是第二种情况

g(n,k)也无非两种情况,则g(n,k)=g(n-1,k-1)+f(n-1,k)

所以可以出现递推的情况,可以看出这个是DP了

两个FOR循环,I是从1开始到N,J是从1到K开始,先算G,再算F

for(i=1;i<n;i++)

  for(j=1;j<=k;j++)

  {
        g(n,k)=g(n-1,k-1)+f(n-1,k);

         f(n,k)=f(n-1,k-1)+g(n,k);

   }

现在主要是边界情况的讨论了

主要是i=1,j=1的时候

g(1,1)=g(0,0)+f(0,1);

f(1,1)=f(0,0)+g(1,1)=f(0,0)+f(0,1)+g(0,0);

g(n,k)=g(n-1,k-1)+f(n-1,k)=g(n-1,k-1)+f(n-2,k-1)+g(n-1,k);

 f(n,k)=f(n-1,k-1)+g(n,k)=f(n-1,k-1)+g(n-1,k-1)+f(n-1,k);;

对于边界情况,有:

 for(i=1;i<=30;i++)
 {
   f[i][0]=1;
   g[i][0]=1;
 }

 

 

 

#include<iostream>
int main()
{
 freopen("in.txt","r",stdin);
 freopen("out.txt","w",stdout);
 int i,j,t,c;
 long long  f[50][50],g[50][50];
 memset(f,0,sizeof(f));
 memset(g,0,sizeof(g));
 for(i=1;i<=30;i++)
 {
   f[i][0]=1;
   g[i][0]=1;
 }
 for(i=1;i<=30;i++)
 for(j=1;j<=30;j++)
 {
   if(i==j)
   {
    f[i][j]=1;
    g[i][j]=0;
   }
   else
   if(i<j)
   {
    f[i][j]=0;
    g[i][j]=0;
   }
   else
   {
     g[i][j]=g[i-1][j-1]+f[i-1][j];
     f[i][j]=f[i-1][j-1]+g[i][j];
   }
  }
  while(scanf("%d%d",&t,&c)!=EOF)
  {
     printf("%lld/n",f[t][c]);
  }
 return 0;
}

 

 

 

你可能感兴趣的:(input,each,output,distance,Numbers,Forms)