習題4-2 正方形( Squares, ACM/ICPC World Finals 1990, UVa201)
廢了半天勁,終於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