zoukankan      html  css  js  c++  java
  • Square Filling

    题目链接:http://codeforces.com/contest/1207/problem/B

                                                                                  Square Filling

    You are given two matrices ? and ?. Each matrix contains exactly ? rows and ? columns. Each element of ? is either 0 or 1; each element of ? is initially 0.

    You may perform some operations with matrix ?. During each operation, you choose any submatrix of ? having size 2×2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers ? and ? such that 1≤?<? and 1≤?<?, and then set ??,?, ??,?+1, ??+1,? and ??+1,?+1 to 1.

    Your goal is to make matrix ? equal to matrix ?. Two matrices ? and ? are equal if and only if every element of matrix ? is equal to the corresponding element of matrix ?.

    Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes ? equal to ?. Note that you don’t have to minimize the number of operations.

    Input
    The first line contains two integers ? and ? (2≤?,?≤50).

    Then ? lines follow, each containing ? integers. The ?-th integer in the ?-th line is ??,?. Each integer is either 0 or 1.

    Output
    If it is impossible to make ? equal to ?, print one integer −1.

    Otherwise, print any sequence of operations that transforms ? into ? in the following format: the first line should contain one integer ? — the number of operations, and then ? lines should follow, each line containing two integers ? and ? for the corresponding operation (set ??,?, ??,?+1, ??+1,? and ??+1,?+1 to 1). The condition 0≤?≤2500 should hold.

    Examples
    input
    3 3
    1 1 1
    1 1 1
    0 1 1
    output
    3
    1 1
    1 2
    2 2
    input
    3 3
    1 0 1
    1 0 1
    0 0 0
    output
    -1
    input
    3 2
    0 0
    0 0
    0 0
    output
    0

    题意: 二维矩阵a,b。b矩阵全是0,你每次可以在b里面指定一个2x2的矩阵全部赋为1。问能否使得a矩阵等于b矩阵

    思路:只需要贪心的选择b里面的2x2矩阵,记录下位置,在b里面相同位置变为1。最后比对一下a和b矩阵即可。

    #include<bits/stdc++.h>
    using namespace std;
    int a[55][55], b[55][55];
    vector<pair<int, int>> ans;
    int main()
    {
    	ios::sync_with_stdio(0);
    	int n, m;
    	cin >> n >> m;
    	for (int i = 1; i <= n; i++)
    		for (int j = 1; j <= m; j++)
    			cin >> a[i][j];
    	for (int i = 1; i <= n; i++) {
    		for (int j = 1; j <= m; j++) {
    			if (a[i][j] && a[i + 1][j] && a[i][j + 1] && a[i + 1][j + 1]) {
    				ans.push_back(make_pair(i, j));
    				b[i][j] = 1, b[i + 1][j] = 1, b[i][j + 1] = 1, b[i + 1][j + 1] = 1;
    			}
    		}
    	}
    	for (int i = 1; i <= n; i++) {
    		for (int j = 1; j <= m; j++) {
    			if (a[i][j] != b[i][j]) {
    				cout << -1 << endl;
    				return 0;
    			}
    		}
    	}
    	cout << ans.size() << endl;
    	for (int i = 0; i < ans.size(); i++)
    		cout << ans[i].first << ' ' << ans[i].second << endl;
    	return 0;
    }
  • 相关阅读:
    FFT学习笔记
    FWT(Fast Walsh Transformation)快速沃尔什变换学习笔记
    GMS2游戏开发学习历程
    [BZOJ3238][AHOI2013]差异 [后缀数组+单调栈]
    Trie树简单讲解
    自己的题
    小技巧
    编程注意事项
    构造方法
    递归
  • 原文地址:https://www.cnblogs.com/shmilky/p/14089031.html
Copyright © 2011-2022 走看看