答案應該是對的 只是用的時間過長
問有沒有大佬知道怎麼 簡化
我的CODE:
import java.util.Scanner;
public class a746 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
while (input.hasNextInt()){
int n = input.nextInt();
int m = input.nextInt();
String[][] answer = new String[n][n];
int x1=-1; int y1=-1;
for (int i =0;i<m;i++){
int x2=input.nextInt();
int y2=input.nextInt(); //要往前的點
if ((x1<x2||y1<y2) && (x1!=-1&&y1!=-1)) { //如果要去位置比自己大的點
for(int e=y1-1;e<y2;e++){ //則由小變大
for (int q=x1-1;q<x2;q++){
answer[e][q] = "*";
}
}
}
if ((x1>x2 || y1>y2)&& (x1!=-1&&y1!=-1)){
for(int e=y1-1;e>=y2-1;e--){ //則由大到小
for (int q=x1-1;q>=x2-1;q--){
answer[e][q] = "*";
}
}
}
x1=x2;//把終點變為起始點
y1=y2;
}
for (int i=0;i<n+2;i++){
System.out.print("-");
}
System.out.println("");
for (int q=0;q<answer[0].length;q++){
System.out.print("|");
for (int e=0;e<answer.length;e++){
if (answer[e][q]==null) {
System.out.print(" ");
} else{
System.out.print(answer[e][q]);};
}
System.out.print("|\n");
}
for (int i=0;i<n+2;i++) {
System.out.print("-");
}
System.out.println("");
}
}
}