Cod sursa(job #1701054)

Utilizator dalavricDaniel Lavric dalavric Data 12 mai 2016 01:54:40
Problema Jocul Flip Scor 30
Compilator java Status done
Runda Arhiva de probleme Marime 5.02 kb
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
 
 
public class Main {
     
    static int[][] matrix = new int[16][16];
    public static void flipColumn(int j , int n)
    {
        for(int i = 0 ; i < n ; i++)
        {
            matrix[i][j] = 0 - matrix[i][j];
        }
    }
     
    public static void flipLine(int i,int m)
    {
        for(int j = 0 ; j < m ;j++)
        {
            matrix[i][j] = 0 - matrix[i][j];
        }
    }
    public static void main(String[] args)
    {
        try
        {
             
			BufferedReader br = new BufferedReader(new FileReader("flip.in"));
			PrintWriter pr = new PrintWriter("flip.out");
             
            String line;
            line = br.readLine();
            String[] splitedLine = line.split(" ");
            int n = Integer.parseInt(splitedLine[0]);
            int m = Integer.parseInt(splitedLine[1]);
            for(int i = 0 ; i < n ;i++)
            {
                line = br.readLine();
                splitedLine = line.split(" ");
                for(int j = 0 ; j < m ; j++)
                {
                    matrix[i][j] = Integer.parseInt(splitedLine[j]);
                 
                }
            }
             
            for(int j = 0 ; j < m ;j++)
            {
                int positiveSum = 0 ;
                int negativeSum = 0 ;
                for(int i = 0 ; i < n; i++)
                {
                    if(matrix[i][j] < 0)
                    {
                        negativeSum += matrix[i][j];
                    }
                    else
                    {
                        positiveSum += matrix[i][j];
                    }
                }
                if(Math.abs(negativeSum) > positiveSum)
                {
                    flipColumn(j,n);
                }
            }
            
            for(int i = 0 ; i < n ;i++)
            {
                int positiveSum = 0 ;
                int negativeSum = 0 ;
                for(int j = 0 ; j < m; j++)
                {
                    if(matrix[i][j] < 0)
                    {
                        negativeSum += matrix[i][j];
                    }
                    else
                    {
                        positiveSum += matrix[i][j];
                    }
                }
                if(Math.abs(negativeSum) > positiveSum)
                {
                    flipLine(i,n);
                }
            }
            int sum = 0 ;
            for(int i = 0 ; i < n ; i++)
            {
                for(int j = 0 ; j < m ; j++)
                {
                    sum += matrix[i][j];
                }
            }
             
            pr.print(sum);
            pr.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         
    }
 
}


/*import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;

public class Main {

	private static int n;
	private static int m;
	private static int maxSum = 0;
	private static ArrayList<Integer> array = new ArrayList<Integer>();
	static int[][] matrix = new int[16][16];
	static int[] flip = new int [16];
	private static int count = 0;
	static int[][] aux = new int[16][16];
	
	private static void calculateSum()
	{
		int sum = 0;
		
		
		for (int i = 0 ; i< n ;i ++)
			for(int j = 0 ; j < m ; j++)
			{
				sum += aux[i][j] * flip[i];
			}
		
		if(sum > maxSum )
		{
			maxSum = sum;
		}
	}
	
	
	private static void backTracking(int k )
	{
		if(k == n)
		{
			for(int i = 0 ; i < n ; i++)
			{
				System.out.print(flip[i] +  " ");
			}
			System.out.println();
				calculateSum();
			return;
		}
		
		for(int i = -1 ; i<2 ;i+=2)
		{
			flip[k] = i;
			backTracking(k + 1);
		}
		
	}
	
	public static void main(String[] args)
	{
		
		try {
			BufferedReader br = new BufferedReader(new FileReader("D:\\Input Files For Porgrams\\flip.in"));
			PrintWriter pr = new PrintWriter("D:\\Input Files For Porgrams\\flip.out");
			
			String line = br.readLine();
			String[] splitedLine = line.split(" ");
			
			n = Integer.parseInt(splitedLine[0]);
			m = Integer.parseInt(splitedLine[1]);
			
			
			if(n < m)
			{
				for(int i = 0 ; i < n ; i++)
				{
					line = br.readLine();
					splitedLine = line.split(" ");
					for( int j = 0 ; j < m ; j++)
					{
						aux[i][j] = Integer.parseInt(splitedLine[j]);
					}
				}
			}
			else
			{
				for(int i = 0 ; i < n ; i++)
				{
					line = br.readLine();
					splitedLine = line.split(" ");
					for( int j = 0 ; j < m ; j++)
					{
						aux[j][i] = Integer.parseInt(splitedLine[j]);
					}
				}
			}
			
			n = Math.min(n, m);
			
			backTracking(0);
			
			pr.print(maxSum);
			pr.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}*/