暴力搜索 寻找鞍点
来源:互联网 发布:淘宝客服外包多少钱 编辑:程序博客网 时间:2024/06/01 13:03
暴力搜索 寻找鞍点
前一段时间参加了个小笔试,最后有一道编程题挺好玩儿,在此记录一下。如果还有什么更好的方法,希望各位看官不吝赐教。
题目:
找出一个二维数组中的鞍点,即该位置上的元素在该行上最大、在该列上最小。也可能没有鞍点。
输入描述:
输入有多组数据。每组数据的第一行包含两个整数,m 和 n。紧接着是 mxn(1≤m, n≤50)的矩阵。
输出描述:
对应每组数据,按照从上到下、从左到右的顺序依次输出所有鞍点的坐标(从1开始)。如果没有任何鞍点,输出 “No Point”。每组数据之后输出一个空行作为间隔。
输入例子:
3 455 68 54 8252 66 43 6648 77 83 702 31 2 34 5 6
输出例子:
2 22 41 3
思路:暴力搜索。
因为每行每列都肯能有多个鞍点,例子如下:
6 6 66 6 66 6 6
代码:
#include <vector>#include <iostream>using namespace std;void FindPoint(int m, int n){ if((m == 0) && (n == 0)) { cout<<"No Point"<<endl; return; } int count = 0; int num; vector<vector<int>> nums(m, vector<int>(n, 0)); for(int i = 0; i < m; ++i) { for(int j = 0; j < n; ++j) { cin>>num; nums[i][j] = num; } } for(int i = 0; i < m; ++i) { for(int j = 0; j < n; ++j) { int k; for(k = 0; k < n; ++k) { if(nums[i][j] < nums[i][k]) { break; } } if(k != n) { continue; } int l; for(l = 0; l < m; ++l) { if(nums[i][j] > nums[l][j]) { break; } } if(l != m) { continue; } ++count; cout<< i + 1 << " " << j + 1 << endl; } } if(count == 0) { cout<<"No Point"<<endl; } cout<<endl; return;}int main(){ int m, n; while(cin>> m >> n) { FindPoint(m, n); } return 0;}
阅读全文
0 0
- 暴力搜索 寻找鞍点
- 鞍点寻找
- 六个循环暴力搜索—寻找矩形
- 寻找矩阵中的鞍点
- 寻找矩阵 鞍点
- 二维数组寻找鞍点
- 寻找矩阵鞍点
- 鞍点的寻找
- CCF NOI1047 寻找鞍点
- 寻找一个矩阵的鞍点
- 二维数组中寻找鞍点
- 寻找一个数组中的鞍点
- 寻找矩阵的鞍点问题
- 寻找矩阵中的鞍点<二维数组>
- 暴力搜索
- 暴力搜索
- 暴力搜索
- 鞍点
- MySQL 5 数据库基础语句总结
- Dubbo配置
- mysql insert锁机制
- commons-pool2对象池分配回收策略
- Struts2框架搭建
- 暴力搜索 寻找鞍点
- SQL语言学习心得
- Redis之对象
- hdu1242 Rescue(BFS +优先队列 or BFS )
- 5、linux常用不太记得住命令
- Retrofit2.0与RxJava2.0结合出现的问题解决
- 表格
- android 获取sha1
- Android studio升级3.0 carry2 出现问题Gradle sync failed: Connection timed out: connect解决办法