简体   繁体   中英

Copy char matrix (char**) without library functions

I need to make a function as homework which given a char matrix composed of T and A returns the number of islands there are on said matrix, being T the land and A the water in Spanish. All this I could do yet there is one more problem, I can modify the matrix I was given as parameter. I thought of copying the matrix before calling my function and sending the new one but I could not do this. As the title says I need to to this with absolutely no library functions of any kind (eg strcopy). I'll pass my code just in case:

int islands(char** map, int col, int row){
char** aux = map;//I don't think this line does anything
int cont = 0;
for (int i = 0; i < col; i++) {
    for (int j = 0; j < row; j++) {
        char c = aux[i][j];
        if (aux[i][j] =='T') {
            cont++;
            deleteLand(aux, i,j, col, row);
        }
    }
}
int a = cont;
return cont;
}


void deleteLand(char** map, int t1,int t2, int col, int row) {
map[t1][t2] = 'A';
for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
        if (!(i==0 && j ==0)&&(t1 + i >= 0 && t1 + i < col) && (t2 + j >= 0 && t2 + j < row)) {
            if (map[t1 + i][t2 + j] == 'T') {
                int tc = t1+i;
                int tr = t2 + j;
                deleteLand(map, tc,tr,col,row);
            }
        }
    }
}
}

And an example of what it returns: Entry: (['T','T','A' 'A','A','T' 'A','A','A' 'T','T','A'], 3, 4) Returns: 2

Hope it is clear enough. Also, while writing this I realized I could save the value of the matrix when entering and copy it again on exit but I think it would be practically the same. Thanks a lot for any help provided

To copy an array (data from an array), you'd need to allocate space for it. After using the copy you should free the memory.


I suppose the only reason to copy the data in this case is to restore the original data in map . If so, much better solution would be to use another letter, say 'B', to mask visited islands in deleteLand . Then after you are done, you can quickly restore the original map changing all B's to T's. It avoids all the problems with allocating and deallocating memory.

EDIT : I had to remove the actual code, which was in C++, since the tag C++ was removed in the meantime.

As I posted in a comment I could fix the problem with the help of PaulMcKenie, I'll post the code just in case it's useful for anyone.

char** copyMatrix(char ** mat, int cols) {
char** newMat = new char*[cols];
for (int i = 0; i < cols; i++) {
    newMat[i] = copyStr(mat[i]);
}
return newMat;
}

And, as I mentioned before I can't use library functions so I'll also post copyStr:

char* copyStr(char* string) {
char* copia = new char[strLen(string)];
int i = 0;
while (*string) {
    copia[i] = *string;
    *string++;
    i++;
}
copia[i] = *string;
return copia;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM