diff options
author | Jack Sangdahl <[email protected]> | 2025-05-17 23:15:29 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-05-17 23:15:29 -0600 |
commit | 87badbaba07e4cd13c2281e15c96ccf2dd8b3106 (patch) | |
tree | ceb97f38b7a69211175f4bb25c4b707c29f4bd5d | |
parent | 2304d32820d403740108de84125059c623686fc9 (diff) | |
download | leetcode-87badbaba07e4cd13c2281e15c96ccf2dd8b3106.tar.gz leetcode-87badbaba07e4cd13c2281e15c96ccf2dd8b3106.zip |
1931. painting a grid with three different colours
-rw-r--r-- | painting_grid_three_colours.c | 78 | ||||
-rw-r--r-- | painting_grid_three_colours.py | 42 |
2 files changed, 120 insertions, 0 deletions
diff --git a/painting_grid_three_colours.c b/painting_grid_three_colours.c new file mode 100644 index 0000000..82c9d53 --- /dev/null +++ b/painting_grid_three_colours.c @@ -0,0 +1,78 @@ +// 1931. Painting a Grid With Three Different Colors +#include "leetcode.h" + +/* + * you are given two integers 'm' and 'n'. consider an 'm x n' grid where each + * cell is initially white. you can paint each cell red, green, or blue. all + * cells must be painted. return the number of ways to colour the grid with no + * two adjacent cells having the same colour. since the answer can be very + * large, return it modulo 10^9+7. + */ + +int colorTheGrid(int m, int n) { + const int mod = 1e9 + 7; + int max_mask = 1; + for (int i = 0; i < m; ++i) + max_mask *= 3; + int states[48], s = 0; + for (int i = 0; i < max_mask; ++i) { + int curr = i, prev = -1; + bool valid = true; + for (int j = 0; j < m; ++j) { + int dir = curr % 3; + if (dir == prev) { + valid = false; + break; + } + prev = dir; + curr /= 3; + } + if (valid) + states[s++] = i; + } + int compat[48][48], compat_size[48]; + for (int i = 0; i < s; ++i) + compat_size[i] = 0; + for (int i = 0; i < s; ++i) + for (int j = 0; j < s; ++j) { + int x = states[i], y = states[j]; + bool valid = true; + for (int k = 0; k < m; ++k) { + if (x % 3 == y % 3) { + valid = false; + break; + } + x /= 3; + y /= 3; + } + if (valid) + compat[i][compat_size[i]++] = j; + } + int dp[48], copy[48]; + for (int i = 0; i < s; ++i) + dp[i] = 1; + for (int i = 0; i < n - 1; ++i) { + for (int j = 0; j < s; ++j) + copy[j] = 0; + for (int j = 0; j < s; ++j) { + int dir = dp[j]; + if (dir) + for (int k = 0; k < compat_size[j]; ++k) { + int new = compat[j][k]; + copy[new] = (copy[new] + dir) % mod; + } + } + for (int j = 0; j < s; ++j) + dp[j] = copy[j]; + } + long long ans = 0; + for (int i = 0; i < s; ++i) + ans = (ans + dp[i]) % mod; + return (int)ans; +} + +int main() { + printf("%d\n", colorTheGrid(1, 1)); // expect: 3 + printf("%d\n", colorTheGrid(1, 2)); // expect: 6 + printf("%d\n", colorTheGrid(5, 5)); // expect: 580986 +} diff --git a/painting_grid_three_colours.py b/painting_grid_three_colours.py new file mode 100644 index 0000000..d33b1d8 --- /dev/null +++ b/painting_grid_three_colours.py @@ -0,0 +1,42 @@ +# 1931. Painting a Grid With Three Different Colors +from collections import Counter + +""" +you are given two integers 'm' and 'n'. consider an 'm x n' grid where each +cell is initially white. you can paint each cell red, green, or blue. all +cells must be painted. return the number of ways to colour the grid with no +two adjacent cells having the same colour. since the answer can be very +large, return it modulo 10^9+7. +""" + + +class Solution(object): + def colorTheGrid(self, m, n): + """ + :type m: int + :type n: int + :rtype: int + """ + dp = {(1 << (2 * m)) - 1: 1} + mod = 10**9 + 7 + for i in range(m * n - 1, -1, -1): + copy = Counter() + x, y = divmod(i, m) + for j in dp: + valid = {0, 1, 2} + if y < m - 1: + valid.discard(j >> (2 * m - 2)) + if x < n - 1: + valid.discard(j & 3) + for k in valid: + new = (j >> 2) + (k << (2 * m - 2)) + copy[new] = (copy[new] + dp[j]) % mod + dp = copy + return sum(dp.values()) % mod + + +if __name__ == "__main__": + obj = Solution() + print(obj.colorTheGrid(m=1, n=1)) + print(obj.colorTheGrid(m=1, n=2)) + print(obj.colorTheGrid(m=5, n=5)) |