Skip to content

Instantly share code, notes, and snippets.

@rogerioagjr
Last active February 5, 2016 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerioagjr/85c8635c11fa24fbbac3 to your computer and use it in GitHub Desktop.
Save rogerioagjr/85c8635c11fa24fbbac3 to your computer and use it in GitHub Desktop.
Torre
// Torre - F2PJ/F2P1 - OBI 2015
// Rogério Júnior
// Complexidade: O(n²)
#include <cstdio> // scanf e printf
#include <algorithm> // função max
using namespace std; // algorithm
#define MAXN 1010 // defino o valor de MAXM como 1010
#define INF 0x3f3f3f3f // defino o valor de INF
// declaro as variáveis que vou usar
int n, resp=-INF, tab[MAXN][MAXN], linha[MAXN], coluna[MAXN];
int main(){
// leio o valor de n
scanf("%d", &n);
// leio os valores das casas do tabuleiro
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
scanf("%d", &tab[i][j]);
// calculo a soma de cada linha
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
linha[i]+=tab[i][j];
// calculo a soma de cada coluna
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
coluna[i]+=tab[j][i];
// percorro todo o tabuleiro, calculando o peso de cada casa
// e guardando o maior valor encontrado na variável resp
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
resp=max(resp, linha[i]+coluna[j]-2*tab[i][j]);
// por fim, imprimo o valor salvo em resp
printf("%d\n", resp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment