JOJ 2039: Fire Net III

2039: Fire Net III


StatusIn/OutTIME LimitMEMORY LimitSubmit TimesSolved UsersJUDGE TYPE
stdin/stdout1s10240K22963Standard

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)指的是方法数

 

将一个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;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值