/* * c.cpp * * Created on: 2013-10-7 * Author: wangzhu */ #include<cstdio> #include<iostream> using namespace std; #define NMAX 1000010 int arr[NMAX]; /** * 只需要從左下角或右上角開始查詢,即可。 * 當是從右上角開始查詢時, * 左上角(row = 0,column = columns - 1), * 1)若查詢的值key大於當前的值arr[i],則說明key在此行以下,故有row++; * 2)若查詢的值key小於當前的值arr[i],則說明key在此列的左邊,故有column--; * 3)若相等,則返回true * 4)判斷條件:row<rows && column >=0,若滿足,繼續1),若不滿足,則直接返回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; } /** * 當是從左下角開始查詢時, * 左上角(row = rows - 1,column = 0), * 1)若查詢的值key小於當前的值arr[i],則說明key在此行以下,故有row--; * 2)若查詢的值key大於當前的值arr[i],則說明key在此列的左邊,故有column++; * 3)若相等,則返回true * 4)判斷條件:row >= 0 && column < columns,若滿足,繼續1),若不滿足,則直接返回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() { freopen("data.in", "r", stdin); int n, m, t, len; bool flag; while (~scanf("%d%d%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; }