Cod sursa(job #944308)

Utilizator luchieanLuchiean Iosif luchiean Data 28 aprilie 2013 03:23:32
Problema Jocul Flip Scor 20
Compilator c Status done
Runda Arhiva de probleme Marime 1.37 kb
#include<stdio.h>
int main(){
	FILE *in,*out;
	int n,m,matrice[100][16],i,j,lines[100],columns[16],sum,positive,negative;
	
	in = fopen("flip.in","r");

	if(in == NULL){
		printf("Eroare!Nu s-a deschis fisierul de intrare!\n");
		return 0;
	}

	fscanf(in,"%d",&n);

	fscanf(in,"%d",&m);

	for(i = 0;i < n;i++){
		for(j = 0;j < m;j++){
			fscanf(in,"%d",&matrice[i][j]);
		}
	}

	fclose(in);

	for(i = 0;i < n;i++){
		positive = 0;
		negative = 0;
		for(j = 0;j < m;j++){
			if(matrice[i][j] >= 0){
				positive += matrice[i][j];
			}else{
				negative += matrice[i][j];
			}
		}

		negative *=-1;

		if(negative > positive){
			lines[i] = 1;
		}else{
			lines[i] = 0;
		}
	}

	for(i = 0;i < m;i++){
		positive = 0;
		negative = 0;
		for(j = 0;j < n;j++){
			if(matrice[j][i] >= 0){
				positive += matrice[j][i];
			}else{
				negative += matrice[j][i];
			}
		}

		negative *=-1;

		if(negative > positive){
			columns[i] = 1;
		}else{
			columns[i] = 0;
		}
	}

	sum=0;
	for(i = 0;i < n;i++){
		for(j = 0;j < m;j++){
			if(lines[i] == 1 || columns[j] == 1){
				sum += matrice[i][j]*(-1);
			}else{
				sum += matrice[i][j];
			}
		}
	}
	
	out = fopen("flip.out","w");

	if(out == NULL){
		printf("Eroare!Nu s-a deschis fisierul de iesire!\n");
		return 0;
	}

	fprintf(out,"%d",sum);

	fclose(out);
	return 0;
}