Topic 1384: Finding in a two-dimensional array

Source: Internet
Author: User

http://ac.jobdu.com/problem.php?pid=1384

Topic Description:

In a two-dimensional array, each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Please complete a function, enter such a two-dimensional array and an integer to determine whether the array contains the integer. Input:

The input may contain multiple test samples, for each test case,

The first behavior entered is 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 you enter includes an integer t (1<=t<=1000000) that represents the number you want to find.

The next M row, each row has n number, represents the matrix of m rows n columns given by the topic (as shown in the topic description, each row is sorted in ascending order from left to right, each column sorted in ascending order from top to bottom.) Output:

corresponding to each test case,

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

The output "No" means that no number T is found in the two-dimensional array. Sample input:

3 3
5 1 2 3 4 5 6 7 8 9 3 3 1 2 3 4 5 6 7 8 9
ten
3 3 2 3 4 5 6 7
8 9 10
Sample output:
Yes
No
No
The solution of the tricks: the input of the array at the same time to judge
#include <cstdio>
int a[1000][1000];

int main () {
	int m, n, T;
	while (scanf ("%d%d", &m, &n)!= EOF) {
		scanf ("%d", &t);
		int flag = FALSE;
		for (int i = 0; i < m. ++i) {
			for (int j = 0; J < N; ++j) {
				scanf ("%d", &a[i][j]);
				if (a[i][j] = = t)
					flag = True
			;
		}
		if (flag)
			printf ("yes\n");
		else
			printf ("no\n");
	}
	return 0;
}

Do a binary lookup for each row, remove multiple columns to the right according to the location of the lookup, and continue to remove the next line based on the lookup on the previous line:

#include <cstdio>
int a[1000][1000];

int main () {
	int m, n, T;
	while (scanf ("%d%d", &m, &n)!= EOF) {
		scanf ("%d", &t);
		int flag = FALSE;
		for (int i = 0; i < m. ++i) {
			for (int j = 0; J < N; ++j) {
				scanf ("%d", &a[i][j]);
			}
		int end = n-1;
		for (int i = 0; i < m &&!flag; ++i) {
			int begin = 0;
			if (T < a[i][0]) break;
			else if (T > A[i][end]) continue;
			else{while
				(begin <= End &&!flag) {
					int mid = (begin + End)/2;
					if (t = = A[i][mid]) 
						flag = true;
					else if (T < A[i][mid]) End
						= mid-1;
					else
						begin = mid + 1;
				}
		}} if (flag)
			printf ("yes\n");
		else
			printf ("no\n");
	}
	return 0;
}

Continued optimization, using the properties of the young matrix, can be achieved by O (m+n) Complexity:

#include <cstdio>
int a[1000][1000];

int main () {
	int m, n, T;
	while (scanf ("%d%d", &m, &n)!= EOF) {
		scanf ("%d", &t);
		for (int i = 0; i < m. ++i) {
			for (int j = 0; J < N; ++j) {
				scanf ("%d", &a[i][j]);
			}
		int row = 0, col = n-1;
		BOOL flag = FALSE;
		while (Row < m && Col >= 0) {
			if (a[row][col] = = t) {
				flag = true;
				break;
			else if (A[row][col] > t)
				--col;
			else
				++row;
		}
		if (flag)
			printf ("yes\n");
		else
			printf ("no\n");
	}
	return 0;
}


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.