aboutsummaryrefslogtreecommitdiff
path: root/tests/sysy_lvX/081_n_queens.sy
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.001@gmail.com>2026-09-15 08:16:43 +0800
committerRichard Qin <richard.qin.001@gmail.com>2026-09-15 08:16:43 +0800
commit4022e5c95210631e4aceb08eaf9d9071aa5aefc4 (patch)
treea5922559836e45fb3459a3d070367912e1035b1a /tests/sysy_lvX/081_n_queens.sy
downloadrcc-master.tar.gz
rcc-master.tar.xz
Set up a git repoHEADmaster
Diffstat (limited to 'tests/sysy_lvX/081_n_queens.sy')
-rw-r--r--tests/sysy_lvX/081_n_queens.sy49
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/sysy_lvX/081_n_queens.sy b/tests/sysy_lvX/081_n_queens.sy
new file mode 100644
index 0000000..5a5c086
--- /dev/null
+++ b/tests/sysy_lvX/081_n_queens.sy
@@ -0,0 +1,49 @@
+int ans[50], sum = 0, n;
+
+int row[50], line1[50], line2[100];
+
+void printans()
+{
+ sum = sum + 1;
+ int i = 1;
+ while (i <= n) {
+ putint(ans[i]);
+ if (i == n) {
+ putch(10);
+ return;
+ } else
+ putch(32);
+ i = i + 1;
+ }
+}
+
+void f(int step)
+{
+ int i = 1;
+ while (i <= n) {
+ if (row[i] != 1 && line1[step + i] == 0 && !line2[n + step - i]) {
+ ans[step] = i;
+ if (step == n)
+ printans();
+ row[i] = 1;
+ line1[step + i] = 1;
+ line2[n + step - i] = 1;
+ f(step + 1);
+ row[i] = 0;
+ line1[step + i] = 0;
+ line2[n + step - i] = 0;
+ }
+ i = i + 1;
+ }
+}
+
+int main()
+{
+ int N = getint();
+ while (N > 0) {
+ n = getint();
+ f(1);
+ N = N - 1;
+ }
+ return sum;
+}