POJ no.2411

Source: Internet
Author: User

Test instructions: The 1*2 of the size of the brick laying n*m ground, to find the total number of laying solutions.

Type: Tiling problem & Status DP

Analysis: About the tiling problem, AI here provides two kinds of DP methods.

The first type: when laying with 1*2 bricks, the bricks can be placed horizontally and vertically in two ways. For the current position (I, j), if horizontal, the (I, J) and (I, j+1) are set to 1, if vertical, then (i, J) is 0, (I, j+1) is 1. Then for the nth row there must be (n, j) 1 and 1<=j <= m. Based on the above analysis, we can define a two-dimensional DP array, d[i][j]: = The total number of laying schemes when the state of line I is J, the initialization of the DP array simply enumerates the state of the first line as Dp[1][j], if the check state J can exist then dp[1][j] = 1, the thinking of state transfer equation is to check whether the state J of line I can transfer to the state K of Line I+1, which is called compatibility, and if they are compatible, there is dp[i+1][k] + = Dp[i][j]. The time complexity of this method is O (nm2^ (2m)).

The second type: we check and make bricks from top to bottom, from left to right in the order of selection. For the position that has been laid to true, the position is not laid to be recorded as false. Then for the currently checked position (I, j), the position before (I, j) must be true. Since the laying of the bricks can be chosen to horizontal and vertical two, only (I, j) to (I, M), (i+1, 0) to (i+1, j-1) The state is uncertain, that is (I+1, j) and subsequent positions will not have the bricks placed, it is false. To sum up, we simply enumerate the state of the topmost position that each joins has not yet queried, with a total of M. Similarly, define a three-dimensional DP array, dp[i][j][k]: = The total number of laying schemes when the state is k when laying position (I, J). Let's deduce the state transition equation for this DP. For the current position (I, J), set the state of the enumeration to used, check that the position (I, j) is required to place the bricks simply by looking at used >> J & 1 is 1, if 1 is no longer required and must have (I+1, j) for 0,dp[i][j][used] = dp[ I][j+1][used & ~ (1 << j)], otherwise you can select horizontal and vertical on the position (I, J). The conditions can be used>> (j+1) &1 = = 0 and J <= M-1, at this time the number of paving schemes dp[i][j+1][used | (1 << (j+1))] The condition of vertical discharge is i+1 <= N, at this time the number of laying scheme is dp[i][j+1][used | (1 << J)]. If both horizontal and vertical are available, there is dp[i][j][used]=  dp[i][j+1][used | (1 << (j+1))] + dp[i][j+1][used | (1 << J)]. So what about the initialization of the DP array? Dp[n][m][0] = 1: Easy to understand when laying to the last position we may wish to add a column, but the state of the column must be false, so the first m-1 position of column n+1 and the last position of nth column can only be used=0 and only one of them can be selected in the horizontal and vertical. Finally only the answer output DP[1][1][0]: When laying the first position (1, 1) the rest of the position is false,used can only be 0. The time complexity of this method O (nm2^m).

Conclusion: The two methods can be used to optimize the spatial complexity of the two one-dimensional arrays, and the second method is more efficient than the first in the time complexity. The following reference code is about the second method, the first method you can try to write.

Implementation code

#include <iostream>
#include <algorithm>
using namespace Std;

Long Long dp[2][1 << (];//1 <= n,m <= 15)

void Solve (int n, int m)
{
int *cur = Dp[0], *NXT = dp[1];//The utilization of the scrolling array, Dp[s] The number of scenarios when the table state is S
int I, j, K, s = 1 << m;
Cur[0] = 1;
for (i = n-1; I >= 0; i.)
{
for (j = m-1; J >= 0;--j)
{
for (k = 0; k < s; ++k)
{
if (k >> J & 1)
Nxt[k] = cur[k & ~ (1 << j)];
Else
{
int ans = 0;
if (j + 1 < m &&! K >> (j + 1) & 1))
Ans + = Cur[k | (1 << (j + 1))];
if (i + 1 < n)
Ans + = Cur[k | (1 << J)];
Nxt[k] = ans;
}
}
Swap (cur, NXT);
}
}
cout << cur[0] << Endl;
}

int main ()
{
int n, m;
CIN >> n >> m;
Solve (n, m);
return 0;
}

POJ no.2411

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.