SHARE
    TWEET
    aqibm

    Untitled

    Apr 6th, 2025
    47
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    text 2.53 KB | None | 0 0
    1. #include <iostream>
    2. #include <vector>
    3. #include <algorithm>
    4. #include <climits>
    5. using namespace std;
    6. pair<int, int> findLargestSquareWithKFlips(const vector<vector<int>>& matrix, int k) {
    7. if(matrix.empty() || matrix[0].empty())
    8. return {-1, -1};
    9. int n = matrix.size(), m = matrix[0].size();
    10. // Build a prefix sum matrix for counting zeros.
    11. // prefix[i+1][j+1] = number of zeros in submatrix from (0,0) to (i,j)
    12. vector<vector<int>> prefix(n + 1, vector<int>(m + 1, 0));
    13. for (int i = 0; i < n; i++){
    14. for (int j = 0; j < m; j++){
    15. int isZero = (matrix[i][j] == 0) ? 1 : 0;
    16. prefix[i+1][j+1] = isZero + prefix[i][j+1] + prefix[i+1][j] - prefix[i][j];
    17. }
    18. }
    19. // Function to count zeros in a submatrix with top-left (i,j) and side length L.
    20. auto countZeros = [&](int i, int j, int L) -> int {
    21. return prefix[i + L][j + L] - prefix[i][j + L] - prefix[i + L][j] + prefix[i][j];
    22. };
    23. // Binary search for the maximum square side length that can be achieved.
    24. int low = 1, high = min(n, m), best = 0;
    25. pair<int, int> bestPos = {-1, -1};
    26. while(low <= high) {
    27. int mid = (low + high) / 2;
    28. bool found = false;
    29. pair<int, int> curPos = {-1, -1};
    30. for (int i = 0; i <= n - mid; i++){
    31. for (int j = 0; j <= m - mid; j++){
    32. int zeros = countZeros(i, j, mid);
    33. if(zeros <= k) {
    34. found = true;
    35. curPos = {i, j};
    36. break;
    37. }
    38. }
    39. if(found) break;
    40. }
    41. if(found) {
    42. best = mid;
    43. bestPos = curPos;
    44. low = mid + 1; // try to find a larger square
    45. } else {
    46. high = mid - 1;
    47. }
    48. }
    49. return bestPos;
    50. }
    51. int main() {
    52. // Example input:
    53. // First line: n m k
    54. // Next n lines: each containing m binary digits (0 or 1)
    55. // For instance:
    56. // 5 5 2
    57. // 1 1 1 1 1
    58. // 1 0 0 1 1
    59. // 1 1 1 1 1
    60. // 1 1 0 1 1
    61. // 1 1 1 1 1
    62. int n, m, k;
    63. cin >> n >> m >> k;
    64. vector<vector<int>> matrix(n, vector<int>(m));
    65. for (int i = 0; i < n; i++){
    66. for (int j = 0; j < m; j++){
    67. cin >> matrix[i][j];
    68. }
    69. }
    70. pair<int, int> ans = findLargestSquareWithKFlips(matrix, k);
    71. cout << "Upper-left corner of largest square: " << ans.first << " " << ans.second << endl;
    72. return 0;
    73. }
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /