# Include <cstdio> # include <iostream> using namespace STD; # define Nmax limit 10int arr [Nmax];/*** you only need to search from the lower left or upper right corner. * When searching starts from the upper right corner, * in the upper left corner (ROW = 0, column = columns-1), * 1) if the queried value Key is greater than the current value arr [I], it indicates that the key is below this row, so Row ++; * 2) if the queried value Key is less than the current value arr [I], it indicates the key on the left of this column, therefore, if column --; * 3) is equal, true * is returned. 4) Judgment condition: Row <rows & Column> = 0. If yes, continue 1 ), if not, false */bool query (INT rows, int columns, int key) {bool flag = false; int column = columns-1; int ROW = 0; while (row <rows & Column> = 0) {int temp = arr [row * columns + column]; If (temp = Key) {flag = true; return flag;} else if (temp> key) {column --;} else {row ++ ;}} return flag;}/*** when searching from the lower left corner, * top left corner (ROW = rows-1, column = 0), * 1) if the queried value Key is smaller than the current value arr [I], it indicates that the key is below this row, so row --; * 2) if the queried value Key is greater than the current value arr [I], it indicates that the key is on the left of this column, so column ++; * 3) If it is equal, true * 4 is returned) condition: Row> = 0 & Column <columns. If yes, continue 1). If not, false */bool query1 (INT rows, int columns, int key) {bool flag = False; int column = 0; int ROW = rows-1; int temp =-1; while (row> = 0 & Column <columns) {temp = arr [row * columns + column]; If (temp = Key) {flag = true; return flag;} else if (temp> key) {row --;} else {Column ++;} return false;} int main () {int n, m, T, Len; bool flag; while (~ Scanf ("% d", & N, & M, & T) {Len = N * m; For (INT I = 0; I <Len; I ++) {scanf ("% d", arr + I);} flag = query1 (n, m, T); If (FLAG) {printf ("Yes \ n") ;}else {printf ("NO \ n") ;}} return 0 ;}
9-degree 1384 2D array search