废了半天劲,终于ac了.
总结了一点uva上的注意事项
1.不能有package ;
2.类 : public class Xiangqi 要改成public class Main
3.import javax.swing.*; // 請勿使用
4.对于输入内容,没有明确以什么作为输入结束时,一定要用.hasNexXXX()
下面传下ac的代码,最下面没ac的也不删了,程序没问题,就是结束输入那,跟uva的形式不一样
说下这题的思路:暴力算,创建2个矩阵,矩阵h,矩阵v,矩阵h表示水平的线有没有,矩阵v表示垂直的线有没有.n表示正方形面板有n行n列,行从上到下编号为1~n, 列从左到右编号为1~n.面板上最小的正方形边长为1,最大的边长为n-1.先写一个方法getNum,计算面板上边长为i的正方形(下面叫i正)有多少个i>=1且i<=n-1.不同的i正其左上角的顶点一定不同,所以遍历所有i正的左上角顶点,再利用矩阵h和矩阵v判断每一个i正的4条边是否都存在,这样就能计算出面板里i正的个数...然后main函数里让边长i从1遍历到n-1,调用getNum就能得到各种大小的正方形个数.此题有个坑,输入数据v是反的,真不知道为啥
import java.util.ArrayList;
import java.util.Scanner;
/*
* 习题4-2 正方形( Squares, ACM/ICPCWorld Finals 1990, UVa201)
有n行n列(2≤n≤9) 的小黑点, 还有m条线段连接其中的一些黑点。 统计这些线段连成了多少
个正方形(每种边长分别统计) 。
行从上到下编号为1~n, 列从左到右编号为
1~n。 边用H i j和V i j表示, 分别代表边(i,j)-(i,j+1)和(i,j)-(i+1,j)。 如图4-5所示最左边的线段
用V 1 1表示。 图中包含两个边长为1的正方形和一个边长为2的正方形。
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<int[]> resultList = new ArrayList<>();
while(sc.hasNextInt()){
//获取n,
int n = sc.nextInt();
sc.nextLine();
// 创建矩阵,h,v表示水平和垂直,00索引不用,h往右延伸,v往下延伸
boolean[][] h = new boolean[n+1][n+1];
boolean[][] v = new boolean[n+1][n+1];
// m,表示多少个数据
int m = Integer.parseInt(sc.nextLine());
for (int i = 0; i < m; i++) {
String[] strArr = sc.nextLine().split(" ");
if(strArr[0].equals("H")){
h[Integer.parseInt(strArr[1])][Integer.parseInt(strArr[2])] = true;
}else if (strArr[0].equals("V")) {
v[Integer.parseInt(strArr[2])][Integer.parseInt(strArr[1])] = true;
}
}
int[] numSqu = new int[n];//索引i表示正方形边长,值表示此长度正方形个数
for (int i = 1; i <= n-1; i++) {
numSqu[i] = getNum(h,v,i);
}
resultList.add(numSqu);
}
//输出
for (int i = 0; i <resultList.size(); i++) {
if(i!=0) System.out.println();
System.out.println("Problem #"+(i+1));
System.out.println();
boolean flag = true;
int[] arr2 = resultList.get(i);
for (int j = 1; j < arr2.length; j++) {
if(arr2[j]!=0){
System.out.println(arr2[j]+" square (s) of size "+j);
flag = false;
}
}
if(flag) System.out.println("No completed squares can be found.");
if(i!=resultList.size()-1){
System.out.println();
System.out.println("**********************************");
}
}
}
//边长为i的正方形有多少个
public static int getNum(boolean[][] h,boolean[][] v,int i){
int count = 0;
int n1 = h.length-1;
int n2 = n1-i;//边长为i 的正方形,左上角的点从[1][1]-[n2][n2]
for (int j = 1; j <= n2; j++) {
w2:for (int j2 = 1; j2 <=n2; j2++) {
boolean flag=true;
for (int k = 0; k < i; k++) {
flag = h[j][j2+k] && h[j+i][j2+k] && v[j+k][j2] && v[j+k][j2+i];
if(!flag) continue w2;
}
if(flag) count++;
}
}
return count;
}
}
下面的测试都能通过,提交到uva,就报Runtime error...无奈了,有大神能帮忙看看是哪里报的错吗?
package suanfajingsai;
import java.util.ArrayList;
import java.util.Scanner;
/*
* 习题4-2 正方形( Squares, ACM/ICPCWorld Finals 1990, UVa201)
有n行n列(2≤n≤9) 的小黑点, 还有m条线段连接其中的一些黑点。 统计这些线段连成了多少
个正方形(每种边长分别统计) 。
行从上到下编号为1~n, 列从左到右编号为
1~n。 边用H i j和V i j表示, 分别代表边(i,j)-(i,j+1)和(i,j)-(i+1,j)。 如图4-5所示最左边的线段
用V 1 1表示。 图中包含两个边长为1的正方形和一个边长为2的正方形。
*/
public class Squares {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<int[]> resultList = new ArrayList<>();
while(true){
//获取n,
String str = sc.nextLine();
if(str.equals("")) break;
int n = Integer.parseInt(str);
// 创建矩阵,h,v表示水平和垂直,00索引不用,h往右延伸,v往下延伸
boolean[][] h = new boolean[n+1][n+1];
boolean[][] v = new boolean[n+1][n+1];
// m,表示多少个数据
int m = Integer.parseInt(sc.nextLine());
for (int i = 0; i < m; i++) {
String[] strArr = sc.nextLine().split(" ");
if(strArr[0].equals("H")){
h[Integer.parseInt(strArr[1])][Integer.parseInt(strArr[2])] = true;
}else if (strArr[0].equals("V")) {
v[Integer.parseInt(strArr[2])][Integer.parseInt(strArr[1])] = true;
}
}
int[] numSqu = new int[n];//索引i表示正方形边长,值表示此长度正方形个数
for (int i = 1; i <= n-1; i++) {
numSqu[i] = getNum(h,v,i);
}
resultList.add(numSqu);
}
//输出
for (int i = 0; i <resultList.size(); i++) {
if(i!=0) System.out.println();
System.out.println("Problem #"+(i+1));
System.out.println();
boolean flag = true;
int[] arr2 = resultList.get(i);
for (int j = 1; j < arr2.length; j++) {
if(arr2[j]!=0){
System.out.println(arr2[j]+" square (s) of size "+j);
flag = false;
}
}
if(flag) System.out.println("No completed squares can be found.");
if(i!=resultList.size()-1){
System.out.println();
System.out.println("**********************************");
}
}
}
//边长为i的正方形有多少个
public static int getNum(boolean[][] h,boolean[][] v,int i){
int count = 0;
int n1 = h.length-1;
int n2 = n1-i;//边长为i 的正方形,左上角的点从[1][1]-[n2][n2]
for (int j = 1; j <= n2; j++) {
w2:for (int j2 = 1; j2 <=n2; j2++) {
boolean flag=true;
for (int k = 0; k < i; k++) {
flag = h[j][j2+k] && h[j+i][j2+k] && v[j+k][j2] && v[j+k][j2+i];
if(!flag) continue w2;
}
if(flag) count++;
}
}
return count;
}
}
/*
4
16
H 1 1
H 1 3
H 2 1
H 2 2
H 2 3
H 3 2
H 4 2
H 4 3
V 1 1
V 2 1
V 2 2
V 2 3
V 3 2
V 4 1
V 4 2
V 4 3
2
3
H 1 1
H 2 1
V 2 1
Sample Output
Problem #1
2 square (s) of size 1
1 square (s) of size 2
**********************************
Problem #2
No completed squares can be found.
*/
4
24
H 1 1
H 1 2
H 1 3
H 2 1
H 2 2
H 2 3
H 3 1
H 3 2
H 3 3
H 4 1
H 4 2
H 4 3
V 1 1
V 1 2
V 1 3
V 2 1
V 2 2
V 2 3
V 3 1
V 3 2
V 3 3
V 4 1
V 4 2
V 4 3
4
23
H 1 2
H 1 3
H 2 1
H 2 2
H 2 3
H 3 1
H 3 2
H 3 3
H 4 1
H 4 2
H 4 3
V 1 1
V 1 2
V 1 3
V 2 1
V 2 2
V 2 3
V 3 1
V 3 2
V 3 3
V 4 1
V 4 2
V 4 3
4
23
H 1 1
H 1 3
H 2 1
H 2 2
H 2 3
H 3 1
H 3 2
H 3 3
H 4 1
H 4 2
H 4 3
V 1 1
V 1 2
V 1 3
V 2 1
V 2 2
V 2 3
V 3 1
V 3 2
V 3 3
V 4 1
V 4 2
V 4 3
4
23
H 1 1
H 1 2
H 2 1
H 2 2
H 2 3
H 3 1
H 3 2
H 3 3
H 4 1
H 4 2
H 4 3
V 1 1
V 1 2
V 1 3
V 2 1
V 2 2
V 2 3
V 3 1
V 3 2
V 3 3
V 4 1
V 4 2
V 4 3
4
23
H 1 1
H 1 2
H 1 3
H 2 2
H 2 3
H 3 1
H 3 2
H 3 3
H 4 1
H 4 2
H 4 3
V 1 1
V 1 2
V 1 3
V 2 1
V 2 2
V 2 3
V 3 1
V 3 2
V 3 3
V 4 1
V 4 2
V 4 3
4
23
H 1 1
H 1 2
H 1 3
H 2 1
H 2 3
H 3 1
H 3 2
H 3 3
H 4 1
H 4 2
H 4 3
V 1 1
V 1 2
V 1 3
V 2 1
V 2 2
V 2 3
V 3 1
V 3 2
V 3 3
V 4 1
V 4 2
V 4 3
4
23
H 1 1
H 1 2
H 1 3
H 2 1
H 2 2
H 3 1
H 3 2
H 3 3
H 4 1
H 4 2
H 4 3
V 1 1
V 1 2
V 1 3
V 2 1
V 2 2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
V 2 2
Problem #1
9 square (s) of size 1
4 square (s) of size 2
1 square (s) of size 3
**********************************
Problem #2
8 square (s) of size 1
3 square (s) of size 2
**********************************
Problem #3
8 square (s) of size 1
2 square (s) of size 2
**********************************
Problem #4
8 square (s) of size 1
3 square (s) of size 2
**********************************
Problem #5
7 square (s) of size 1
3 square (s) of size 2
1 square (s) of size 3
**********************************
Problem #6
7 square (s) of size 1
2 square (s) of size 2
1 square (s) of size 3
**********************************
Problem #7
7 square (s) of size 1
3 square (s) of size 2
1 square (s) of size 3
**********************************
Problem #8
7 square (s) of size 1
3 square (s) of size 2
1 square (s) of size 3
**********************************
Problem #9
7 square (s) of size 1
2 square (s) of size 2
1 square (s) of size 3
**********************************
Problem #10
7 square (s) of size 1
3 square (s) of size 2
1 square (s) of size 3
**********************************
Problem #11
8 square (s) of size 1
3 square (s) of size 2
**********************************
Problem #12
8 square (s) of size 1
2 square (s) of size 2
**********************************
Problem #13
8 square (s) of size 1
3 square (s) of size 2
**********************************
Problem #14
8 square (s) of size 1
3 square (s) of size 2
**********************************
Problem #15
8 square (s) of size 1
2 square (s) of size 2
**********************************
Problem #16
8 square (s) of size 1
3 square (s) of size 2
**********************************
Problem #17
7 square (s) of size 1
3 square (s) of size 2
1 square (s) of size 3
**********************************
Problem #18
7 square (s) of size 1
2 square (s) of size 2
1 square (s) of size 3
**********************************
Problem #19
7 square (s) of size 1
3 square (s) of size 2
1 square (s) of size 3
**********************************
Problem #20
7 square (s) of size 1
3 square (s) of size 2
1 square (s) of size 3
**********************************
Problem #21
7 square (s) of size 1
2 square (s) of size 2
1 square (s) of size 3
**********************************
Problem #22
7 square (s) of size 1
3 square (s) of size 2
1 square (s) of size 3
**********************************
Problem #23
8 square (s) of size 1
3 square (s) of size 2
**********************************
Problem #24
8 square (s) of size 1
2 square (s) of size 2
**********************************
Problem #25
8 square (s) of size 1
3 square (s) of size 2
**********************************
Problem #26
5 square (s) of size 1
2 square (s) of size 2
1 square (s) of size 3
**********************************
Problem #27
6 square (s) of size 1
2 square (s) of size 2
**********************************
Problem #28
6 square (s) of size 1
2 square (s) of size 2
1 square (s) of size 3
**********************************
Problem #29
No completed squares can be found.
**********************************
Problem #30
1 square (s) of size 1
**********************************
Problem #31
1 square (s) of size 1