"The point of the sword" two-dimensional array lookup

Source: Internet
Author: User

Reprint Please specify the Source: http://blog.csdn.net/ns_code/article/details/24977113

The sword refers to the third problem on offer. Tested on the nine-degree OJ.

Topic Description Narration:

In a two-dimensional array, each row is ordered in ascending order from left to right. Each column is sorted in ascending order from top to bottom. Please complete a function, enter a two-dimensional array and an integer, and infer if the array contains the integer.

Input:

The input may include multiple test examples, for each test case,

The first behavior of the input two integers m and n (1<=m,n<=1000): Represents the number of rows and columns of the matrix that will be entered.

The second line of input includes an integer t (1<=t<=1000000): Represents the number to find.

The next M-line, each row has n number, represents the title of the M-row N-column matrix (the matrix as seen in the title description, each row is ordered from left to right increments, each column according to the order from top to bottom increment.

Output:

Each test case,

The output "Yes" indicates that a number T is found in a two-dimensional array.

The output "No" indicates that no number T is found in the two-dimensional array.

Example input:
3 3
5
1 2 3
4 5 6
7 8 9
3 3
1
2 3 4
5 6 7
8 9 10
3 3
12
2 3 4
5 6 7
8 9 10
Example output:
Yes
No
No

time limit:1 seconds

Memory limit:32 MB



Two-point search method is used. The time complexity is O (max (m,n)).

The given value key is compared to the element in the upper-right corner of the two-dimensional array, and returns true if it is equal. If key is less than it. Then the last column of the element must be greater than the key, at this time can delete the last column, and if the key is greater than it, then the first row of elements must be less than key, at this time can delete the first row, in turn, lower than the lower left corner, assuming that the element is not equal to the key. The Fasle is returned.

Implementation code such as the following:

 #include < Stdio.h> #include <stdbool.h>/* to find out if there are key*/bool find (int *matrix,int m,int n,int key) in the ascending two-dimensional array matrix of m*n, {if ( Matrix==null | | m<1 | | n<1) return false;int row = 0;int col = n-1;while (row<=m-1 && col>=0) {if (matrix[row*n+col] = = key) return T Rue;else if (Matrix[row*n+col] > key) col--;elserow++;} return false;} int main () {int m,n;while (scanf ("%d%d", &m,&n)! = EOF) {int key,i;//without static generates a stack memory overflow,//assuming no static. The memory of the matrix array is allocated within the stack. Let's say add static. The memory allocation of the matrix array is in the global initialized zone. 

//additional. The maximum range of arrays is used here. Will increase the memory used,//But assuming that it is dynamically allocated with malloc, the execution time will be added.

Static int matrix[1000*1000] = {0};scanf ("%d", &key), for (i=0;i<m*n;i++) scanf ("%d", matrix+i); bool result = Find (Matrix,m,n,key), if (result) printf ("yes\n"); elseprintf ("no\n");} return 0;}

/************************************************************** &NBSP;&NBSP;&NBSP;&NBSP; problem:1384 &NBSP;&NBSP;&NBSP;&NBSP; user:mmc_maodun &NBSP;&NBSP;&NBSP;&NBSP; language:c &NBSP;&NBSP;&NBSP;&NBSP; result:accepted &NBSP;&NBSP;&NBSP;&NBSP; time:680 Ms &NBSP;&NBSP;&NBSP;&NBSP; memory:4820 KB ****************************************************************/


"The point of the sword" two-dimensional array lookup

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.