C-language parameter passing two-dimensional arrays-keeping the city skyline

Source: Internet
Author: User

Today did a leetcode on a topic, the principle is simple, very easy to get along with the solution, but in the process of writing code to pass a two-dimensional array is always an error, so summed up how to pass:

Reference Blog http://www.cnblogs.com/yangxi/archive/2012/03/22/2411452.html

Title: In a two-dimensional array grid , grid[i][j] represents the height of a building located somewhere. We are allowed to increase the height of any building in any quantity (the number of different buildings may be different). Height 0 is also considered a building.

Finally, the "Skyline" viewed in all four directions of the new array (that is, top, bottom, left, and right) must be the same as the skyline of the original array. The city skyline is the outer contour of the rectangle formed by all buildings when viewed from a distance. Take a look at the example below.

What is the maximum sum of the building height that can be increased?

Declaring functions such as: int maxincreasekeepingskyline (int** grid) {}

Intended to pass a two-dimensional array grid[4][4], if called maxincreasekeepingskyline (grid or * grid[4] or grid[] &grdi[0]); And so the format is wrong, depending on the different compiler settings, may appear warning or error,

A forced conversion (int**) grid is required; That's OK.

Maxincreasekeepingskyline ((int**) grid);

Then in the Maxincreasekeepingskyline method to access Grid[m][n] can not be accessed by subscript direct access, need to be accessed through the compiler and grid+n*i+j; Access in this case is * ((int*) grid +n*i+j))

Source code: Local debugging pass, upload test case error, do not know why

#include <stdio.h>
Returns the minimum number in a two-digit number
int maxnum (int a, int b) {
if (a >= b)
return b;
Else
return A;

}
Find out the maximum value of each row per column in a two-dimensional array and store it back in two arrays row and Col
void Searchmaxhigher (int **grid, int row[], int col[], int gridrowsize, int *gridcolsizes) {
int I, J;
Calculates the maximum value of each row of a two-dimensional array stored in row[]
for (i = 0; i<gridrowsize; i++) {
Row[i] = 0;
for (j = 0; j< (*gridcolsizes); j + +)
{
if (row[i]< (* (int *) grid+ (*gridcolsizes) *i + j)))
Row[i] = * ((int *) grid + (*gridcolsizes) *i + j);

}
}
Calculates the maximum value of each row of a two-dimensional array stored in col[]
for (i = 0; i< (*gridcolsizes); i++) {
Col[i] = 0;
for (j = 0; j< gridrowsize; j + +)
{
if (col[i]< (* (int *) grid + (*gridcolsizes) *j + i))
Col[i] = (* (int *) grid + (*gridcolsizes) *j + i));

}
}
}
Find the increment of each element and accumulate
int Maxincreasekeepingskyline (int** grid, int gridrowsize, int *gridcolsizes) {
int rowmax[50], colmax[50];
int I, J;
int sum = 0;
Searchmaxhigher (grid, Rowmax, Colmax, Gridrowsize, gridcolsizes);
for (i = 0; i<gridrowsize; i++)
for (j = 0; j< (*gridcolsizes); j + +) {
if ((* (* (int *) grid + (*gridcolsizes) *i + j)) <= Rowmax[i] && (* ((int*) grid + (*gridcolsizes) *i + j)) <= ColM Ax[j]) {
sum = sum + (Maxnum (Rowmax[i], colmax[j])-(* ((int *) grid + (*gridcolsizes) *i + j)));
}


}
return sum;
}

void Main () {
int A[4][4] = {{3,0,8,4},{2,4,5,7},{9,2,6,3},{0,3,1,0}};
int * * GRID;
int row = 4, col = 4;
int sum = 0;

Sum=maxincreasekeepingskyline (int * *) A, row, &col);

printf ("%d\n", sum);

}

C-language parameter passing two-dimensional arrays-keeping the city skyline

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.