Challenge: Find The Operations

Subdomeniu: Number Theory (number-theory)

Scor cont: 80.0 / 80

Submission status: Accepted

Submission score: 1.0

Submission ID: 464737475

Limbaj: cpp14

Link challenge: https://www.hackerrank.com/challenges/flip/problem

Cerinta

You are given a square grid of size N, with rows numbered from 0 to N - 1 starting from the top and columns numbered from 0 to N - 1 starting from the left.   

A cell (u, v) refers to the cell that is on the u<sup>th</sup> row and the v<sup>th</sup> column. Each cell contains an integer - 0 or 1. You can pick any cell and flip the number in all the cells (including the picked cell) within the Manhattan distance D from the picked cell. A flip here means changing the number from 0 to 1 and vice-versa. The manhattan distance from the cell (u, v) to the cell (x, y) is equal to $|u - x| + |v - y|$ where $|i|$ is the absolute value of i.   

Your mission is to change all values in the grid to zero without using more than N×N flips.

**Input Format**  
The first line of the input contains two integers N and D  separated by a single space.  
Each line in the next N lines contains N integers separated by a single space which are either 0 or 1. the i<sub>th</sub> number on the j<sub>th</sub> line is the number on the cell (i - 1, j - 1) of the grid.

**Constraints**  
1 ≤  N ≤  20  
 0 ≤ D ≤ 40

**Output Format**  
If there is no solution, your output should contain exactly a single string "Impossible" (without quotes). If a solution exists, print out the string "Possible" (without quotes) in the first line of your output. In the second line, print out an integer M which represent the number of operations that you need. Each line in the next M lines should contain a pair of integers separated by a single space representing the cell that you picked for the corresponding operation. Note that if there is more than one solution you can pick any one of them.

**Sample Input:#00**

    3 1
    0 1 0
    1 1 1
    0 1 0

**Sample Output:#00**

    Possible
    1
    1 1

**Sample Input:#01**

    3 2
    1 0 1 
    1 1 0 
    0 0 0 

**Sample Output:#01**

    Impossible

**Explanation**

In the first testcase, we can perform the first operation in the center cell, this will flip all the elements to 0 within 1 manhattan distance.  
In the second testcase, we cannot make it an all 0 matrix under 9 moves. Hence, Impossible.

Cod sursa

// https://www.hackerrank.com/challenges/FLIP

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN = 25;
int N, D;
int B[MAXN][MAXN], E[MAXN*MAXN][MAXN*MAXN], S[MAXN*MAXN];

bool is_possible(int i,int j) {
    return i >= 0 && i < N && j >= 0 && j < N;
}

int main () {
    int blah = scanf("%d%d", &N, &D);

    for (int i = 0;i < N; ++i)
        for(int j = 0;j < N; ++j) {
          blah = scanf("%d", &B[i][j]);
          S[i * N + j] = B[i][j];
        }

    for (int i = 0; i < N; ++i)
        for (int j = 0;j < N; ++j) {
          for (int a = -D; a <= D; ++a)
            for (int b = abs(a) - D; b <= D - abs(a); ++b)
              if (is_possible(i + a, j + b))
                E[i * N + j][(i + a) * N + (j + b)] = 1;
        }

    int L = N*N, cur = 0;
    bool possible = 1;

    for(int j = 0; j < L; ++j) {
        for (int i = cur; i < L; ++i)
          if (E[i][j] == 1) {
            if (i != cur) {
              for (int k=0;k<L;++k) swap(E[cur][k], E[i][k]);
              swap(S[cur], S[i]);
            }

            break;
          }

        if(E[cur][j] == 0) continue;

        for (int i = 0; i < L; ++i)
          if (i != cur && E[i][j] == 1) {
            for (int k = 0; k < L; ++k) E[i][k] ^= E[cur][k];
            S[i] ^= S[cur];
          }

        ++cur;
    }

    for (int i = L-1; i >= 0; --i) {
        int ret = 0, it = -1;

        for (int j=L-1;j>=0;--j) {
          ret += E[i][j];
          if (E[i][j]) it = j;
        }

        if(ret == 0 && S[i] == 1) {
          possible = 0;
          break;
        }

        for (int j = L-1; j >= 0 && ret > 1; --j) {
          if (E[i][j] == 1) {
            E[i][j] = 0;
            --ret;
          }
        }

        if (ret == 1) {
          for (int k=0;k<L;++k)
            if (k != i && E[k][it] == 1) {
              E[k][it] = 0;
              S[k] ^= S[i];
            }
        }
    }

    if (!possible)
        printf("Impossible\n");
    else {
        printf("Possible\n");
        int ret = 0;

        for(int i = 0; i < L; ++i) ret += S[i];
        printf("%d\n",ret);

        for(int i = 0; i < L; ++i)
          if(S[i] == 1) {
            for (int j = 0; j < L; ++j)
              if(E[i][j] == 1) {
                printf("%d %d\n", j/N, j%N);
                break;
              }
          }
    }
}
HackerRank Number Theory – Find The Operations