Cod sursa(job #542760)

Utilizator DevilShadowJunc Raul Cosmin DevilShadow Data 26 februarie 2011 22:23:34
Problema Jocul Flip Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <iostream>
#include <fstream.h>

using namespace std;

struct point { int x; int y; int le; int co; };
point p[2][32000];
int a[1030][1030];
int dx[] = {  0,  0,  1, -1 };
int dy[] = {  1, -1,  0,  0 };

void schimba()
{
	int a = p[0][0].co;
	int c = p[0][a].le;
	int d = p[1][0].le;
	if(c > d)
	{
		int i;
		for(i = 0; i <= p[0][0].co; i ++)
		{
			p[1][i].x = p[0][i].x;
			p[1][i].y = p[0][i].y;
		}
		p[1][0].co = p[0][0].co;
		p[1][0].le = c;
	}
}

void recursivitate(int x1, int y1, int x2, int y2, int j)
{
	x1 += x2;
	y1 += y2;
	p[0][j].x = x1;
	p[0][j].y = y1;
	p[0][j].le = p[0][j - 1].le + a[x1][y1] - a[x1 - x2][y1 - y2];
	j ++;
	for(int i = 0; i < 4; i ++)
	{
		if(a[x1][y1] < a[x1 + dx[i]][y1 + dy[i]])
			recursivitate(x1, y1, dx[i], dy[i], j);
	}
	p[0][0].co = j - 1;
	schimba();
	j --;
}

int main ()
{
	ifstream f ("alpin.in");
	ofstream g ("alpin.out");
	
	int n, i, j, x, y;
	
	f >> n;
	for(i = 0; i <= n; i ++)
		a[0][i] = a[i][0] = a[n + 1][i] = a[i][n + 1] = 0;
	for(i = 1; i <= n; i ++)
		for(j = 1; j <= n; j ++)
			f >> a[i][j];
	
	for(x = 1; x <= n; x ++)
		for(y = 1; y <= n; y ++)
		{
			recursivitate(x, y, 0, 0, 0);
		}
	g << p[1][0].le << endl;
	for(i = 0; i <= p[1][0].co; i ++)
		g << p[1][i].x << " " << p[1][i].y << endl;
}