第五届河南省程序设计大赛nyoj545Metric Matrice(java 类goto语句)
2013-11-29 14:07
519 查看
Metric Matrice
时间限制:1000 ms | 内存限制:65535 KB难度:1
描述
Given as input a square distance matrix, where a[i][j] is the distance between point i and point j, determine if the distance matrix is "a metric" or not.
A distance matrix a[i][j] is a metric if and only if
1. a[i][i] = 0
2, a[i][j]> 0 if i != j
3. a[i][j] = a[j][i]
4. a[i][j] + a[j][k] >= a[i][k] i ¹ j ¹ k
输入The first line of input gives a single integer, 1 ≤ N ≤ 5, the number of test cases. Then follow, for each test case,
* Line 1: One integer, N, the rows and number of columns, 2 <= N <= 30
* Line 2..N+1: N lines, each with N space-separated integers
(-32000 <=each integer <= 32000).输出Output for each test case , a single line with a single digit, which is the lowest digit of the possible facts on this list:
* 0: The matrix is a metric
* 1: The matrix is not a metric, it violates rule 1 above
* 2: The matrix is not a metric, it violates rule 2 above
* 3: The matrix is not a metric, it violates rule 3 above
* 4: The matrix is not a metric, it violates rule 4 above
样例输入
240 1 2 31 0 1 22 1 0 13 2 1 020 32 0
样例输出
03
来源第五届河南省程序设计大赛上传者rihkddd
import java.util.Scanner;
public class nyoj545Metric_Matrice {
public static void main(String[] args) {
int[][]a;
int n;
Scanner sc = new Scanner(System.in);
int ans=0;
loop:for(int N=sc.nextInt();N-->0;System.out.println(ans)){
n=sc.nextInt();
a=new int
;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a[i][j]=sc.nextInt();
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
if(a[i][i]!=0){
ans=1;continue loop ;
}if(i!=j&&a[i][j]<=0){
ans=2;continue loop;
}if(a[i][j]!=a[j][i]){
ans=3;continue loop;
}
}
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
for(int k=0;k<n;k++)//该条件不可嵌套在上面代码之中!!!
if((i!=j && j!=k && i!=k)&& ( a[i][j] + a[j][k] < a[i][k] )){
ans=4; continue loop;
}
ans=0;
}
}
}
return做法:
import java.util.Scanner;
public class Main {
private static int[][] array = null;
private static int cases, i, j, n;
private static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
cases = in.nextInt();
while (cases-- > 0) {
n = in.nextInt();
array = new int
;
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
array[i][j] = in.nextInt();
}
}
System.out.println(solve(array));
}
}
private static int solve(int[][] array) {
for (int i = 0; i < array.length; ++i) {
if (array[i][i] != 0) {
return 1;
}
for (int j = i + 1; j < array[i].length; ++j) {
if (array[i][j] <= 0 || array[j][i] <= 0) {
return 2;
}
if (array[i][j] != array[j][i]) {
return 3;
}
}
}
for (int i = 0; i < array.length; ++i) {
for (int j = 0; j < array[i].length; ++j) {
if (j == i) {
continue;
} else {
for (int k = 0; k < array.length; ++k) {
if (k == i || k == j) {
continue;
} else {
if (array[i][k] + array[k][j] < array[i][j]) {
return 4;
}
}
}
}
}
}
return 0;
}
}
相关文章推荐
- nyoj 543 遥控器 第五届河南省程序设计大赛
- NYOJ - 541 - 最强DE 战斗力 (第五届河南省程序设计大赛--大数!!)
- nyoj 540 第五届河南省程序设计大赛A
- nyoj 545 第五届河南省程序设计大赛F
- nyoj 541 第五届河南省程序设计大赛B
- nyoj 546 Divideing Jewels 第五届河南省程序设计大赛
- nyoj542 试 制 品 (第五届河南省程序设计大赛)
- 第八届河南省程序设计大赛-NYOJ-1241-Distribution(水题)
- 第三届河南省程序设计大赛-NYOJ-247-虚拟的城市之旅(SPFA)
- nyoj 716 River Crossing 第六届河南省程序设计大赛
- POJ 1797Heavy Transportation +第七届河南省程序设计大赛NYOJ1248 海岛争霸 (最小生成树变形/djs变形)
- NYOJ-712 探寻宝藏(第六届河南省程序设计大赛)
- NYOJ-168-房间安排(第三届河南省程序设计大赛A题)
- nyoj1253 Turing equation(第七届河南省程序设计大赛)
- nyoj 168 房间安排 第三届河南省程序设计大赛
- NYOJ 303【序号互换】第四届河南省程序设计大赛
- 第八届河南省程序设计大赛-NYOJ-1236-挑战密室
- NYOJ-303-序号互换(第四届河南省程序设计大赛A题(模拟))
- NYOJ-305-表达式求值(第四届河南省程序设计大赛C题(模拟+栈||递归)(sscanf函数的应用))
- nyoj 1239 引水工程 第八届河南省程序设计大赛