7-56 to find saddle points (20 points) (maximum meaning)

A "saddle point" refers to the maximum matrix element value of the element at that position, the minimum on the column on the line.

This problem requires programming, seeking a given n-order square matrix saddle point.

Input formats:

The first input line is given a positive integer n (1≤n≤6). Then n lines of n integers are given, separated by a space therebetween.

Output formats:

Output in "lower line marked column index" (index starting from 0) of the output format saddle point position in a row. If the saddle point does not exist, the output "NONE". Title ensure a given matrix at most one saddle point.

Input Sample 1:
. 4
1. 7. 4 1
. 4. 3. 8. 6
1 1 2. 6
0. 7. 8. 9
Output Sample 1:
21
Input Sample 2:
2
1. 7
. 4 1
Output Sample 2:
NONE

Code

#include<stdio.h>

int main(){
	int n;
	scanf("%d",&n);
	int a[n][n];
	int i,j,k;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			scanf("%d",&a[i][j]);
		}
	}
	int t,tb;
	for(i=0;i<n;i++){
		tb=0;
		t=a[i][0];
		for(j=1;j<n;j++){
			if(a[i][j]>t) {
				t=a[i][j];
			}  
		}
		
		for(j=0;j<n;j++){
			if(a[i][j]==t) {
				tb=j;
				for(k=1;k<n;k++){
					if(a[(i+k)%n][tb]<t) break;
				}
				if(k==n){
					printf("%d %d",i,tb);
					return 0;
				}
				
			}
			
		}
		
	}

	printf("NONE");
	return 0;
}

Guess you like

Origin blog.csdn.net/asdf8759/article/details/91413534