我的任务是找到阵列1的的最长的路线。 Hoizontal和垂直。阵列仅由0和1的,看起来如这样的:
4 40 1 1 10 1 0 10 1 1 01 0 1 0输出应打印[i] [j]的首发1,和[I] [J]。收官1,所以水平应该是[1] [0] [3] [0]。
我用我的GETCOLOR()函数来得到[I] [J]现货价值。
我想这个对于WAAAAY长,差不多花了整整一个星期的this.I有一些想法,但没有奏效。也许是因为我是新来的C和完全新的数组。
我知道我应该去通过阵列,每1发现它应该保存坐标开始,然后转到下一个,每节约1发现结束。 0被发现后,比较lenght,如果lenght是国内规模最大,覆盖lenght。但我没写code正确。有人能帮助我的广告撰写code?非常感谢你。
编辑:这是我的,但我在一开始我只是,它不工作已
如果(ARR!= NULL){ 而(J< arr-> COLS){ 而(I< = arr->行){ 如果(GETCOLOR(ARR,I,J)== 1){ startI = I; startJ = j的; 打破; } 其他 我++; } I = 0; 而(I< = arr->行){ 如果(GETCOLOR(ARR,I,J)== 1){ 恩迪= I; endJ = j的; } 我++; } I = 0; 的printf(开始%D \\ NEND%D \\ n第%d个\\ n \\ n,startI,startJ,恩迪,endJ,线); J ++; }}解决方案
对不起,悔之晚矣。我没有写你到底怎么想的那样,但是这会真的帮助你。只要阅读它,你会得到的想法。
链接到code pastebin/vLATASab
或者查看这里:
的#include<&stdio.h中GT;主要(){ INT ARR [4] [4]; 改编[0] [0] = 0;常用3 [0] [1] = 1;常用3 [0] [2] = 1;常用3 [0] [3] = 1; 改编[1] [0] = 0;常用3 [1] [1] = 1;常用3 [1] [2] = 0;常用3 [1] [3] = 1; ARR [2] [0] = 0;常用3 [2] [1] = 1;常用3 [2] [2] = 1;常用3 [2] [3] = 0; 改编[3] [0] = 1;常用3 [3] [1] = 0;常用3 [3] [2] = 1;常用3 [3] [3] = 1; INT I,J,K; INT线,LINE_START,LINE_END,LINE_MAX = 0; INT关口,col_start,col_end,col_max = 0; // Horizently 为(K = 0; K&4;; k ++) { 对于(I = 0; I&下; 4;我+ +) { 如果(ARR [k]的[I]!)继续; 为(J =; J&下; 4; J ++) { 如果突破(ARR [K] [J]!); } j--; 如果(J-I + 1> LINE_MAX) { 行= K; LINE_START = I; LINE_END = j的; LINE_MAX = LINE_END-LINE_START + 1; } } } 的printf(horizontaly \\ n); 的printf(启动:[%D]。[%D]。\\ n,行,LINE_START); 的printf(结束:[%D]。[%D]。\\ n,行,LINE_END); // Verticaly 为(K = 0; K&4;; k ++) { 对于(I = 0; I&下; 4;我+ +) { 如果(ARR [I] [K]!)继续; 为(J =; J&下; 4; J ++) { 如果突破(ARR [J] [K]!); } j--; 如果(J-I + 1> col_max) { COL = K; col_start = I; col_end = j的; col_max = col_end-col_start + 1; } } } 的printf(\\ nverticaly \\ n); 的printf(启动:[%D]。[%D]。\\ n,col_start,列); 的printf(结束:[%D]。[%D]。\\ n,col_end,列);}My task is to find "longest line" of 1's in array. Hoizontal and Vertical. Array is made only of 0's and 1's, and looks for example like this:
4 4 0 1 1 1 0 1 0 1 0 1 1 0 1 0 1 0Output should print [ i ][ j ] of "starting" 1, and [ i ][ j ] of "ending" 1., So horizontal should be [1][0] [3][0].
I am using my getcolor() function to get value on [ i ][ j ] spot.
I am thinking about this for WAAAAY to long, spent almost whole week on this.I had some ideas, but none worked. Maybe because I am new to C and completely new to arrays.
I know that I should go through the array, on every 1 found it should save the coordinates to "start", and go to next, saving every 1 found to "end". After 0 is found, compare the lenght, and if the lenght is largest, overwrite the lenght. But I didn't manage to write the code right. Can someone help me ad write the code? Thank you very much.
Edit: This is what I have, but I am just at the start and it doesn't work already:
if(arr != NULL) { while (j < arr->cols) { while (i <=arr->rows) { if (getcolor(arr, i, j) == 1) { startI = i; startJ = j; break; } else i++; } i=0; while (i <=arr->rows) { if (getcolor(arr, i, j) == 1) { endI = i; endJ = j; } i++; } i=0; printf("start %d %d\nend %d %d\nline %d\n\n", startI, startJ, endI, endJ, line); j++; } }解决方案
Sorry for being too late. I didn't write it exactly how you want it, but this will realy help you. Just read it and you will get the idea.
Link to code pastebin/vLATASab
Or view it here:
#include <stdio.h> main() { int arr[4][4]; arr[0][0] = 0;arr[0][1] = 1;arr[0][2] = 1;arr[0][3] = 1; arr[1][0] = 0;arr[1][1] = 1;arr[1][2] = 0;arr[1][3] = 1; arr[2][0] = 0;arr[2][1] = 1;arr[2][2] = 1;arr[2][3] = 0; arr[3][0] = 1;arr[3][1] = 0;arr[3][2] = 1;arr[3][3] = 1; int i, j, k; int line, line_start, line_end, line_max = 0; int col, col_start, col_end, col_max = 0; // Horizently for (k=0; k<4; k++) { for (i=0; i<4; i++) { if (!arr[k][i]) continue; for (j=i; j<4; j++) { if (!arr[k][j]) break; } j--; if (j-i+1>line_max) { line = k; line_start = i; line_end = j; line_max = line_end-line_start+1; } } } printf("horizontaly\n"); printf("start: [%d][%d]\n", line, line_start); printf("end: [%d][%d]\n", line, line_end); // Verticaly for (k=0; k<4; k++) { for (i=0; i<4; i++) { if (!arr[i][k]) continue; for (j=i; j<4; j++) { if (!arr[j][k]) break; } j--; if (j-i+1>col_max) { col = k; col_start = i; col_end = j; col_max = col_end-col_start+1; } } } printf("\nverticaly\n"); printf("start: [%d][%d]\n", col_start, col); printf("end: [%d][%d]\n", col_end, col); }
更多推荐
如何获得在阵列/矩阵的1的线路最长
发布评论