[ create a new paste ] login | about

Link: http://codepad.org/bBPLa2ks    [ raw code | output | fork ]

C++, pasted on Apr 25:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
	int A[5][5] = { { 3, 3, 2, 1, 7 },
					{ 5, 1, 1, 1, 1 },
					{ 1, 1, 7, 8, 8 },
					{ 8, 9, 1, 1, 1 },
					{ 1, 5, 6, 6, 3 } };

	for (int n1 = 0; n1 < 5; n1++)
	{
		for (int n2 = 0; n2 < 5; n2++)
			printf("%d ",A[n1][n2]);

		printf("\n");
	}

	printf("\n");

	int max_len = 0, len = 0; bool merge = false;
	int x1 = 0, y1 = 0, x1_max = 0, y1_max = 0; 
	int x2 = 0, y2 = 0, x2_max = 0, y2_max = 0; 

	for (int i = 0; i < 5; i++)
		for (int k = 0; k < 5; k++)
		{
			if (merge == false) { len = 0; x1 = i, y1 = k; }
			while ((A[i][k] == A[i][k+1]) && (k < 5)) { k++; len++; }

			merge = ((A[i][k] == A[i+1][0]) && (k >= 5)) ? 1 : 0;
			if ((merge == false) && (len > 0)) 
			{ 
				x2 = i; y2 = k; 
				if (len > max_len) 
				{ 
					max_len = len; 
					x1_max = x1; y1_max = y1; 
					x2_max = x2; y2_max = y2; 
				}

				for (int t = y1; t <= y2 && !abs(x1-x2); t++)
					printf("%d ",A[x1][t]);
				
				for (int t1 = y1; t1 < 5 && abs(x1-x2); t1++)
					printf("%d ",A[x1][t1]);
				for (int t2 = 0; t2 <= y2 && abs(x1-x2); t2++)
					printf("%d ",A[x2][t2]);

				printf("len = %d\n",len+1);
			}
		}

	printf("\n============================================================================\n");

	printf("\nMax sequence:\n");

	for (int r = y1_max; r <= y2_max && !abs(x1_max-x2_max); r++)
		printf("%d ",A[x1_max][r]);
				
	for (int r1 = y1_max; r1 < 5 && abs(x1_max-x2_max); r1++)
		printf("%d ",A[x1_max][r1]);
	for (int r2 = 0; r2 <= y2_max && abs(x1_max-x2_max); r2++)
		printf("%d ",A[x2_max][r2]);

	printf("max_len = %d\n",max_len+1);

	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
3 3 2 1 7 
5 1 1 1 1 
1 1 7 8 8 
8 9 1 1 1 
1 5 6 6 3 

3 3 len = 2
1 1 1 1 1 1 len = 6
8 8 8 len = 3
1 1 1 1 len = 4
6 6 len = 2

============================================================================

Max sequence:
1 1 1 1 1 1 max_len = 6


Create a new paste based on this one


Comments: