Pagini recente » Cod sursa (job #2094477) | Cod sursa (job #462631) | Cod sursa (job #2705320) | Cod sursa (job #1219043) | Cod sursa (job #2303802)
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define INPUT_FILE "flip.in"
#define OUTPUT_FILE "flip.out"
#define READ_MODE "r"
#define WRITE_MODE "w"
#define MAX_ROWS 16
#define MAX_COLS 16
typedef struct{
short rows;
short cols;
int **tab;//void destroy_game(GameTable*);
}GameTable;
typedef struct{
short dim;
short *switches;
}SwitchTable;
typedef struct{
int i;
int j;
int val;
}maximum;
GameTable* create_game(void);
void destroy_game(GameTable*);
SwitchTable* create_switches(short);
void destroy_switches(SwitchTable*);
short read_file(FILE*, short*, short*);
int** create_table(FILE*, int, int);
void Switch(SwitchTable*);
int main()
{
GameTable *game = create_game();
if(game){
SwitchTable *switchRows = create_switches(game->rows);
int maxim = (int)0;
while(switchRows->switches[0] != 2){
SwitchTable *switchCols = create_switches(game->cols);
while(switchCols->switches[0] != 2){
int sum = (int)0;
for(short i=0; i<game->rows; i++){
for(short j=0; j<game->cols; j++){
if(switchRows->switches[i] || switchCols->switches[j]){
sum += game->tab[i][j] * -1;
}
else{
sum += game->tab[i][j];
}
}
}
if(sum > maxim){
maxim = sum;
}
Switch(switchCols);
}
destroy_switches(switchCols);
Switch(switchRows);
}
FILE *output = fopen(OUTPUT_FILE, WRITE_MODE);
fprintf(output, "%d", maxim);
fclose(output);
}
return 0;
}
int** create_table(FILE *f, int rows, int cols){
int **tab = NULL;
tab = (int**)malloc(rows*sizeof(int*));
for(int i=0; i<rows; i++){
tab[i] = (int*)malloc(cols*sizeof(int));
}
if(tab){
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
int num = (int)0;
fscanf(f, "%d", &num);
if(num < INT_MAX){
tab[i][j] = (int)num;
}
}
}
}
return (int**)tab;
}
short read_file(FILE* f, short* rows, short* cols){
short er = (short)0;
short r = (short)0;
short c = (short)0;
fscanf(f, "%hi", &r);
fscanf(f, "%hi", &c);
if(r<=MAX_ROWS && c<=MAX_COLS){
er = (short)1;
*rows = r;
*cols = c;
}
return (short)er;
}//void destroy_game(GameTable*);
GameTable* create_game(void){
GameTable *newGame = NULL;
short n = (short)0;
short m = (short)0;
FILE *f = NULL;
f = fopen(INPUT_FILE, READ_MODE);
if(f){
if(read_file(f, &n, &m)){
if(n && m){
newGame = (GameTable*)malloc(sizeof(GameTable));
if(newGame){
newGame->rows = (short)n;
newGame->cols = (short)m;
int** tableGame = (int**)create_table(f,n,m);
if(tableGame){
newGame->tab = tableGame;
return (GameTable*)newGame;
}
else{
printf("Not enough space for a new game\n");
}
}
}
}
else{
printf("Wrong input values!\n");
}
}
else{
printf("File not found!\n");void Switch(SwitchTable*);
}
fclose(f);
return (GameTable*)newGame;
}
SwitchTable* create_switches(short n){//void destroy_game(GameTable*);
SwitchTable *newSwitch = NULL;
newSwitch = (SwitchTable*)malloc(sizeof(SwitchTable));
if(newSwitch){
newSwitch->switches = (short*)calloc(n, sizeof(short));
if(newSwitch->switches){
newSwitch->dim = (short)n;
}
}
return (SwitchTable*) newSwitch;
}
void Switch(SwitchTable *s){
s->switches[(s->dim)-1]++;
for(int i=(s->dim)-1; i>=1; i--){
if(s->switches[i]==2){
s->switches[i]=0;
s->switches[i-1]++;
}
}
}
void destroy_switches(SwitchTable *s){
free(s->switches);
free(s);
}
//void destroy_game(GameTable*);