Cod sursa(job #2650047)

Utilizator George732George George732 Data 17 septembrie 2020 11:41:35
Problema Jocul Flip Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.4 kb
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

//#include "A:\Programming_languages\C++\BasicUtils\BasicUtils\Utils.h"
//using namespace utils;
using namespace std;

vector<int> Read(string path)
{
	std::ifstream file;
	file.open(path, std::ios::in);
	vector<int> read;
	int ti;
	while (file >> ti)
		read.push_back(ti);
	file.close();
	return read;
}
vector<vector<int>> ReadTable(string path)
{
	vector<vector<int>> table;
	vector<int> line;
	int rows, cols;
	ifstream file;
	file.open(path, std::ios::in);
	
	file >> rows >> cols;
	int ti;

	for (int i = 0; i < rows; i++)
	{
		line = vector<int>();
		for (int j = 0; j < cols; j++)
		{
			file >> ti;
			line.push_back(ti);
		}
		table.push_back(line);
	}
	file.close();
	return table;
}
void Write(string path, vector<int> data)
{
	std::ofstream file;
	file.open(path);
	for (int i = 0; i < data.size(); i++)
	{
		file << data[i] << " ";
	}
	file.close();
}
void Write(string path, int data)
{
	std::ofstream file;
	file.open(path);
	file << data;
	file.close();
}

void Swap(int* a, int* b)
{
	int ti = *a;
	*a = *b;
	*b = ti;
}

int GCD(int a, int b)
{
	while (b != 0)
	{
		int t = b;
		b = a % b;
		a = t;
	}
	return a;
}

int Sum(vector<vector<int>>& table)
{
	int sum = 0;
	for (int i = 0; i < table.size(); i++)
		for (int j = 0; j < table[i].size(); j++)
			sum += table[i][j];
	return sum;
}
int SumLine(vector<vector<int>>& table, int line)
{
	int sum = 0;
	for (int i = 0; i < table[line].size(); i++)
		sum += table[line][i];
	return sum;
}
int SumCol(vector<vector<int>>& table, int col)
{
	int sum = 0;
	for (int i = 0; i < table.size(); i++)
		sum += table[i][col];
	return sum;
}
void JoculFlip()
{

}
int main()
{
	vector<vector<int>> table = ReadTable("flip.in");

	int sum = 0;
	//lines
	for (int line = 0; line < table.size(); line++)
	{
		sum = 0;
		for (int col = 0; col < table[line].size(); col++)
			sum += table[line][col];
		if (sum < 0)
			for (int col = 0; col < table[line].size(); col++)
				table[line][col] *= -1;
			
	}

	//cols
	for (int col = 0; col < table[0].size(); col++)
	{
		sum = 0;
		for (int line = 0; line < table.size(); line++)
			sum += table[line][col];
		if (sum < 0)
			for (int line = 0; line < table.size(); line++)
				table[line][col] *= -1;
	}
	
	Write("flip.out", Sum(table));
}