提交时间:2025-03-14 15:43:04

运行 ID: 7162

#include <iostream> #include <cstdio> #include <cstring> using namespace std; int n,p; int b[8]; int a[8][2]; int x, y; int ans[40],q;//多组解的情况 bool check(); void dfs(int temp,int t) { if (t == 0) { if (check() == 1) { p = 1; for (int i = 0; i < n; i++) { if (b[i] == 1) { ans[q]=ans[q]*10+(i + 1);//因为这里是从0开始的,所以还要加一 } } q++; } return; } for (int i = temp; i >=0; i--) { b[i] = 1; dfs(i - 1, t - 1); b[i] = 0; } return; } bool check() { bool s = 0; int x0,y0; for (int i = 0; i < n; i++) { x0 = x; y0 = y; if (b[i] == 1) { x0 -= 1; } else { y0 -= 1; } if (b[i]==1 && (a[i][0] != x0 || a[i][1] != y0))//说真话的人说假话 { s = 1; break; } if(b[i]==0 && (a[i][0] == x0 && a[i][1] == y0))//说假话的人说真话 { s = 1; break; } } if (s == 0) { return true; } else { return false; } } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i][0] >> a[i][1]; } for (int i = 0; i <=n; i++) { x = i;//白色人数 y = n - i; memset(b, 0, 8 * 4); dfs(n-1,i); } if (p == 1) { int mi=100000000; for (int i = 0; i < q; i++) { if (ans[i] < mi) { mi = ans[i]; } } cout << mi; } if (p == 0)cout << "NoSolution."; return 0; }