Code Submission Evaluation System Login

CSES - HIIT Open 2018

HIIT Open 2018

Contest start:2018-05-26 11:00:00
Contest end:2018-05-26 16:00:00

Task list | Submit code | Submissions | Messages | Scoreboard | Statistics


History
2018-05-26 13:10:34
Task:Grid
Sender:DualCore
Submission time:2018-05-26 13:10:34
Status:READY
Result:ACCEPTED

Show test data

Code

#include <iostream>
#include <vector>
#include <string>

int find_row(const std::vector<int>& vec, int n)
{
  for (unsigned int i = 0; i < vec.size(); ++i)
    if (vec[i] == 0)
      return i / n;
  return -1;
}

int find_zero(const std::vector<int>& vec)
{
  for (unsigned int i = 0; i < vec.size(); ++i)
    if (vec[i] == 0)
      return i;
  return -1;
}

int main()
{
  std::string input;
  std::getline(std::cin, input);
  int n = std::stoi(input);
  std::vector<int> vec;
  vec.reserve(n * n);
  for (int i = 0; i < n; ++i)
    {
      std::string line;
      std::getline(std::cin, line);
      for (int j = 0; j < n; ++j)
	vec.push_back(line[j] - '0');
    }

  int row = find_row(vec, n);
  
  if (row == -1)
    {
      std::cout << n;
      return 0;
    }

  std::vector<int> rows(n);
  for (int i = 0; i < row; ++i)
    rows[i] = 1;


  std::vector<int> cols(n);


  for (int i = row * n, j = 0; i < row * n + n; ++i, ++j)
    cols[j] = vec[i];

  int j = find_zero(cols);
  for (unsigned int i = row * n + j; i < vec.size(); i += n)
      rows[i / n] = vec[i];

  for (unsigned int i = 0; i < vec.size(); ++i)
    {
      int c = cols[i % n];
      int r = rows[i / n];
      if (vec[i] == 1)
	{
	  if (c == 0 && r == 0)
	    {
	      std::cout << -1;
	      return 0;
	    }
	}
      else
	{
	  if (c == 1 || r == 1)
	    {
	      std::cout << -1;
	      return 0;
	    }
	}
    }
  int sum = 0;
  for (unsigned int i = 0U; i < rows.size(); ++i)
    sum += rows[i] + cols[i];
  std::cout << sum;
}