Untitled
unknown
plain_text
5 months ago
1.5 kB
6
Indexable
#include <bits/stdc++.h> #define ll long long #define INF 1e18 using namespace std; //const int N = 1e7 + 5; //ll A[N]; int col[9], ans[9], mn; //bool isPrime(int n) //{ // if (n <= 1) // return false; // // for (int i = 2; i <= n / 2; i++) // if (n % i == 0) // return false; // // return true; //} //ll get_sum(ll l, ll r){ // return (r - l + 1) * (l + r) / 2; //} bool isValid(int row, int column){ for (int i = 0; i < column; ++i) { if(row == ans[i] || abs(row - ans[i]) == abs(column - i)) return false; } return true; } void rec(int column){ if (column == 8){ int newMin = 0; for (int j = 0; j < 8; ++j) { newMin += (col[j] != ans[j]); } mn = min(mn, newMin); return; } for (int r = 1; r <= 8; ++r) { if (isValid(r, column)){ ans[column] = r; rec(column + 1); } } } void solve() { int cs = 1; while (cin >> col[0]){ for (int i = 1; i < 8; ++i) { cin >> col[i]; } mn = INT_MAX; rec(0); cout << "Case " << cs++ << ": "<< mn << "\n"; } } int main() { //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
Editor is loading...
Leave a Comment