【POJ3254】Corn Fields Java实现代码

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 14923 Accepted: 7816

Description

Farmer John has purchased a lush new rectangular pasCorn Fieldsture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't be planted. Canny FJ knows that the cows dislike eating close to each other, so when choosing which squares to plant, he avoids choosing squares that are adjacent; no two chosen squares share an edge. He has not yet made the final choice as to which squares to plant.

Being a very open-minded man, Farmer John wants to consider all possible options for how to choose the squares for planting. He is so open-minded that he considers choosing no squares as a valid option! Please help Farmer John determine the number of ways he can choose the squares to plant.

Input

Line 1: Two space-separated integers:  M and  N 
Lines 2.. M+1: Line  i+1 describes row  i of the pasture with  N space-separated integers indicating whether a square is fertile (1 for fertile, 0 for infertile)

Output

Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.

Sample Input

2 3
1 1 1
0 1 0

Sample Output

9

题目大概意思:

农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的(用1标记),农夫可以在这些格子里放牛,其他格子则不能放牛(用0标记),并且要求不可以使相邻格子都有牛。现在输入数据给出这块地的大小及可否放牧的情况,求该农夫有多少种放牧方案可以选择(注意:任何格子都不放也是一种选择,不要忘记考虑!)

解题思路:将每行格子的原始状态和放入牛之后的状态都用二进制编码表示,1代表可以放牧,0代表不可以放牧,下面是Java代码实现。

public int validNums(int[][] filed){

       int row = filed.length;

       int col = filed[0].length;

       int[] filed10 = To10(filed);

       //dp[i][j]表示第i行为j状态时的种类;

       int[][] dp = new int[row + 1][1<<col];

       dp[0][0] = 1;

       for(int i = 1; i <= row;i++){//枚举所有行

//枚举这一行所有可能存在的状态

           for(int j = 0; j < (1 << col);j++){                                        if(!isValid(filed10ij))

                  continue;

              for(int k = 0; k < (1 << col);k++){

                  if((j & k) != 0)//jk不能有上下相邻的部分

                     continue;

                  dp[i][j] = dp[i][j] + dp[i - 1][k];

                  dp[i][j] %= mod;}}}

       int output = 0;

       for(int i = 0; i < (1 << col);i++){

           output += dp[row][i];

           output %= mod;

       }

       returnoutput;

    }

    //将输入的格子每行转换为10进制数

    public int[] To10(int[][] filed){

       int[] res = new int[filed.length + 1];

       for(int i = 1; i < res.lengthi++){

           res[i] = 0;

           for(int j = 0; j < filed[0].length;j++){

              res[i] = (res[i] << 1) + filed[i - 1][j];

           }

       }

       return res;

    }

//判断状态是否可行,第一需要判断和土地的状态,第二需要判断自身的状态合不合法

    public boolean isValid(int[] arrint filedint y){

       if((arr[filed] & y) != y)

           return false;

       if((y & (y << 1)) > 0)

           return false;

       return true;}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值