package dfs;
import java.util.Scanner;
public class 靶子射箭 {
static int n,count=1;
static int[] ans;
static boolean[][] flag;
static int[] lie;
static int[] lie2;
static int[] hang;
static int[] hang2;
static int[] X = new int[]{-1,1,0,0};
static int[] Y = new int[]{0,0,1,-1};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
flag = new boolean[n+1][n+1];
lie = new int[n+2];
lie2 = new int[n+2];
hang = new int[n+2];
hang2 = new int[n+2];
ans = new int[n*n];
for (int i = 0; i < n; i++) {
lie[i] = scanner.nextInt();
}
for (int i = 0; i < n; i++) {
hang[i] = scanner.nextInt();
}
scanner.close();
lie2[0] = 1;
hang2[0] = 1;
flag[0][0] = true;
dfs(0,0);
}
static void dfs(int x,int y){
if (x == n - 1 && y == n - 1) {
int i = 0;
while (i<n&&lie[i] == lie2[i] && hang2[i]==hang[i]){
i++;
}
if (i == n) {
for (int j = 0; j < n * n; j++) {
if (ans[j] != 0 || j == 0) {
System.out.print(ans[j] +" ");
}
}
}
return;
}
for (int i = 0; i < 4; i++) {
int a = x + X[i];
int b = y + Y[i];
if (a < 0 || a >= n || b < 0 || b >= n || flag[a][b]) {
continue;
}
if(lie2[b] >= lie[b] || hang2[a] >= hang[a]) {
continue;
}
flag[a][b] = true;
ans[count] = (a)*n+b;
count++;
lie2[b]++;
hang2[a]++;
dfs(a,b);
flag[a][b] = false;
count--;
ans[count] = 0;
lie2[b]--;
hang2[a]--;
}
}
}
如果出现段错误,看看是不是程序添加了一句System.exit(1);
加了上面那句就会报段错误,System.exit(1)代表非正常退出
如果加System.exit(0)就可以了