题目描述
Cainiao is a university student who loves ACM contest very much. It is a festival for him once when he attends ACM Asia Regional Contest because he always can find some famous ACMers there.
Cainiao attended Asia Regional Contest Fuzhou Site on November 20, 2011. After he got seat map, he wanted to know which seat is the best one.
Cainiao have joined so many QQ Group about ACM/ICPC that he is almost familiar with the strength of each team. In his mind, the value of a seat is defined as following:
1. Strength of each team can be expressed as a positive integer.
2. The value of a seat is related to the adjacent seat (up/down/left/right, only four directions being considering).
3. For an adjacent seat, if the strength of this team is stronger than yours, the absolute value of difference of two teams should be added to your seat, otherwise, the absolute value of difference should be subtracted from your seat.
4. If the adjacent seat is empty (which means you are located at the most left/right/up/down), the value of your seat should be subtracted 1.
5. The best one in a contest is the seat that has the highest value.
6. The initial value of the seat is ZERO.
输入
Input contain a positive integer T( T <=50 ) in the first line, which means T cases.
The first line of each case contains two positive integers N and M (3 <= N, M <= 20) which means the row and column number of the teams, then N rows following, each line contains M positive integers that represent the strengths of the teams.
输出
For each case, first output the case number, and then output the value and row number and column number of the best seat in one line for each case.
If there are multiple solutions for one case, you should output the seat whose row number is largest and only output the seat whose column number is largest if still overlapping.
示例输入
13 41 5 3 46 3 3 44 3 2 1
示例输出
Case 1: 7 1 1
解题思路:
使用两数组a,b,a用来保存原数据,b用来存放相应位置每个原数据计算后的数据,即保存每个原数据的状态变量。对数据进行分情况。1,四个角的数据,需要进行2次运算。2,最外面四条边上,去掉角剩下的中间数据,需要进行三次运算。3,去掉外面的一圈数据剩下的原数据,需要进行四次运算,上下左右。一开始在算法上出了错误,不是不对,而是特别麻烦,折腾的头晕眼花,每次计算都用if 怎么样,然后else 怎么样,后来慢慢发现,其实不用判断所要进行计算的原数据的相邻数据是否比它大还是小,直接用相邻数据减去原数据即可,大大减少了代码量,而且效率高。
代码:
#include <iostream> using namespace std; int a[21][21];//用来放原数据 int b[21][21];//用来放计算后的数据 int main() { int T,t; cin>>T; for(t=1;t<=T;t++) { int i,j,i1,j1;//数组i行,j列 cin>>i>>j; for(i1=0;i1<i;i1++) for(j1=0;j1<j;j1++) { cin>>a[i1][j1]; b[i1][j1]=0;//数组b初值全部为0 } b[0][0]+=-2;//考虑数组的左上角数据,因为有两侧没有相邻数据,所以减去2分 b[0][j-1]+=-2;//右上角 b[i-1][0]+=-2;//左下角 b[i-1][j-1]+=-2;//右下角 b[0][0]+=a[0][1]-a[0][0];//下面分别对四个角的数据计算,每个数据计算两次 b[0][0]+=a[1][0]-a[0][0];//计算的时候不需比较相邻数据,直接用相邻数据减去本数据即可满足题目要求,b数组保留每次计算的结果 b[i-1][0]+=a[i-2][0]-a[i-1][0]; b[i-1][0]+=a[i-1][1]-a[i-1][0]; b[0][j-1]+=a[0][j-2]-a[0][j-1]; b[0][j-1]+=a[1][j-1]-a[0][j-1]; b[i-1][j-1]+=a[i-2][j-1]-a[i-1][j-1]; b[i-1][j-1]+=a[i-1][j-2]-a[i-1][j-1]; for(i1=1;i1<=i-2;i1++)//考虑数组最左边中间的数据,每个数据需要计算三次 { b[i1][0]+=-1;//一侧没有相邻数据,减去1分 b[i1][0]+=a[i1][1]-a[i1][0];//右减左 b[i1][0]+=a[i1-1][0]-a[i1][0];//上减下 b[i1][0]+=a[i1+1][0]-a[i1][0];//下减上 } for(j1=1;j1<=j-2;j1++)//最上边中间的数据 { b[0][j1]+=-1; b[0][j1]+=(a[1][j1]-a[0][j1]); b[0][j1]+=(a[0][j1-1]-a[0][j1]); b[0][j1]+=(a[0][j1+1]-a[0][j1]); } for(j1=1;j1<=j-2;j1++)//最下边中间的数据 { b[i-1][j1]+=-1; b[i-1][j1]+=a[i-2][j1]-a[i-1][j1]; b[i-1][j1]+=a[i-1][j1-1]-a[i-1][j1]; b[i-1][j1]+=a[i-1][j1+1]-a[i-1][j1]; } for(i1=1;i1<=i-2;i1++)//最右边中间的数据 { b[i1][j-1]+=-1; b[i1][j-1]+=a[i1][j-2]-a[i1][j-1]; b[i1][j-1]+=a[i1-1][j-1]-a[i1][j-1]; b[i1][j-1]+=a[i1+1][j-1]-a[i1][j-1]; }//到这里已经把a数组里的最外面一圈的数据处理完毕,把处理结果都放到相应位置的b数组中了 for(i1=1;i1<=i-2;i1++)//处理内部数据 for(j1=1;j1<=j-2;j1++) { b[i1][j1]+=a[i1-1][j1]-a[i1][j1];//每个数组都需要计算四次 b[i1][j1]+=a[i1+1][j1]-a[i1][j1]; b[i1][j1]+=a[i1][j1-1]-a[i1][j1]; b[i1][j1]+=a[i1][j1+1]-a[i1][j1]; }//到这里a数组里的全部数据计算完毕 int m,n,max,x,y;//x,y用来标记b数组中最大数的 行数 和 列数,max用来求b数组中的最大值 max=b[0][0]; for(m=0;m<=i-1;m++) for(n=0;n<=j-1;n++)//遍历b数组 { if(max<=b[m][n]) { max=b[m][n]; x=m; y=n; } } cout<<"Case "<<t<<": "<<max<<" "<<x+1<<" "<<y+1<<endl; } return 0; }
运行截图: