UVa 11195 Another n-Queen Problem

Posted 大四开始ACM

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVa 11195 Another n-Queen Problem相关的知识,希望对你有一定的参考价值。

方法:dfs 状态压缩

方法比较明显,就是一个基本的回溯问题。据说直接做会超时,然而我还是过了。。

code:

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <string>
  6 #include <vector>
  7 #include <stack>
  8 #include <bitset>
  9 #include <cstdlib>
 10 #include <cmath>
 11 #include <set>
 12 #include <list>
 13 #include <deque>
 14 #include <map>
 15 #include <queue>
 16 #include <fstream>
 17 #include <cassert>
 18 #include <unordered_map>
 19 #include <unordered_set>
 20 #include <cmath>
 21 #include <sstream>
 22 #include <time.h>
 23 #include <complex>
 24 #include <iomanip>
 25 #define Max(a,b) ((a)>(b)?(a):(b))
 26 #define Min(a,b) ((a)<(b)?(a):(b))
 27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
 28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
 29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
 30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
 31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
 32 #define FOREACH(a,b) for (auto &(a) : (b))
 33 #define rep(i,n) FOR(i,0,n)
 34 #define repn(i,n) FORN(i,1,n)
 35 #define drep(i,n) DFOR(i,n-1,0)
 36 #define drepn(i,n) DFOR(i,n,1)
 37 #define MAX(a,b) a = Max(a,b)
 38 #define MIN(a,b) a = Min(a,b)
 39 #define SQR(x) ((LL)(x) * (x))
 40 #define Reset(a,b) memset(a,b,sizeof(a))
 41 #define fi first
 42 #define se second
 43 #define mp make_pair
 44 #define pb push_back
 45 #define all(v) v.begin(),v.end()
 46 #define ALLA(arr,sz) arr,arr+sz
 47 #define SIZE(v) (int)v.size()
 48 #define SORT(v) sort(all(v))
 49 #define REVERSE(v) reverse(ALL(v))
 50 #define SORTA(arr,sz) sort(ALLA(arr,sz))
 51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
 52 #define PERMUTE next_permutation
 53 #define TC(t) while(t--)
 54 #define forever for(;;)
 55 #define PINF 1000000000000
 56 #define newline \'\\n\'
 57 
 58 #define test if(1)if(0)cerr
 59 using namespace std;
 60 using namespace std;
 61 typedef vector<int> vi;
 62 typedef vector<vi> vvi;
 63 typedef pair<int,int> ii;
 64 typedef pair<double,double> dd;
 65 typedef pair<char,char> cc;
 66 typedef vector<ii> vii;
 67 typedef long long ll;
 68 typedef unsigned long long ull;
 69 typedef pair<ll, ll> l4;
 70 const double pi = acos(-1.0);
 71 
 72 
 73 string board[15];
 74 int n;
 75 bool c[15] = {false}, d[2][31]={false};
 76 ll ans = 0;
 77 void dfs(int pos)
 78 {
 79     if (pos == n)
 80     {
 81         ++ans;
 82         return;
 83     }
 84     for (int i = 0; i < n; ++i)
 85     {
 86         if (c[i] || d[0][n-1+pos-i] || d[1][pos+i] || board[pos][i] == \'*\') continue;
 87         c[i] = d[0][n-1+pos-i] = d[1][pos+i] = true;
 88         dfs(pos+1);
 89         c[i] = d[0][n-1+pos-i] = d[1][pos+i] = false;
 90     }
 91 }
 92 int main()
 93 {
 94     ios::sync_with_stdio(false);
 95     int kase = 0;
 96     while (cin >> n && n)
 97     {
 98         rep(i, n) cin >> board[i];
 99         ans = 0;
100         dfs(0);
101         cout << "Case " << ++kase << ": ";
102         cout << ans << newline;
103     }
104 }
View Code

看到别人的题解,使用状态压缩来做的,学到了。

code:

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <string>
  6 #include <vector>
  7 #include <stack>
  8 #include <bitset>
  9 #include <cstdlib>
 10 #include <cmath>
 11 #include <set>
 12 #include <list>
 13 #include <deque>
 14 #include <map>
 15 #include <queue>
 16 #include <fstream>
 17 #include <cassert>
 18 #include <unordered_map>
 19 #include <unordered_set>
 20 #include <cmath>
 21 #include <sstream>
 22 #include <time.h>
 23 #include <complex>
 24 #include <iomanip>
 25 #define Max(a,b) ((a)>(b)?(a):(b))
 26 #define Min(a,b) ((a)<(b)?(a):(b))
 27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
 28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
 29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
 30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
 31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
 32 #define FOREACH(a,b) for (auto &(a) : (b))
 33 #define rep(i,n) FOR(i,0,n)
 34 #define repn(i,n) FORN(i,1,n)
 35 #define drep(i,n) DFOR(i,n-1,0)
 36 #define drepn(i,n) DFOR(i,n,1)
 37 #define MAX(a,b) a = Max(a,b)
 38 #define MIN(a,b) a = Min(a,b)
 39 #define SQR(x) ((LL)(x) * (x))
 40 #define Reset(a,b) memset(a,b,sizeof(a))
 41 #define fi first
 42 #define se second
 43 #define mp make_pair
 44 #define pb push_back
 45 #define all(v) v.begin(),v.end()
 46 #define ALLA(arr,sz) arr,arr+sz
 47 #define SIZE(v) (int)v.size()
 48 #define SORT(v) sort(all(v))
 49 #define REVERSE(v) reverse(ALL(v))
 50 #define SORTA(arr,sz) sort(ALLA(arr,sz))
 51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
 52 #define PERMUTE next_permutation
 53 #define TC(t) while(t--)
 54 #define forever for(;;)
 55 #define PINF 1000000000000
 56 #define newline \'\\n\'
 57 
 58 #define test if(1)if(0)cerr
 59 using namespace std;
 60 using namespace std;
 61 typedef vector<int> vi;
 62 typedef vector<vi> vvi;
 63 typedef pair<int,int> ii;
 64 typedef pair<double,double> dd;
 65 typedef pair<char,char> cc;
 66 typedef vector<ii> vii;
 67 typedef long long ll;
 68 typedef unsigned long long ull;
 69 typedef pair<ll, ll> l4;
 70 const double pi = acos(-1.0);
 71 
 72 
 73 int board[15];
 74 int n;
 75 ll ans = 0;
 76 int ALL;
 77 void dfs(int pos, int c, int d1, int d2)
 78 {
 79     if (pos == n)
 80     {
 81         ++ans;
 82         return;
 83     }
 84     int nxt = ~(board[pos] | c | d1 | d2);
 85     int lb = nxt & -nxt & ALL;
 86     while (lb)
 87     {
 88         dfs(pos+1, c|lb, (d1|lb)<<1, (d2|lb)>>1);
 89         nxt -= lb;
 90         lb = nxt & -nxt & ALL;
 91     }
 92 }
 93 
 94 int main()
 95 {
 96     ios::sync_with_stdio(false);
 97     cin.tie(0);
 98     int kase = 0;
 99     while (cin >> n && n)
100     {
101         ALL = (1<<n)-1;
102         rep(i, n)
103         {
104             board[i] = 0;
105             string str; cin >> str;
106             rep(j, n) if (str[j] == \'*\') board[i] |= (1<<j);
107         }
108         ans = 0;
109         dfs(0, 0, 0, 0);
110         cout << "Case " << ++kase << ": ";
111         cout << ans << newline;
112     }
113 }
View Code

 

以上是关于UVa 11195 Another n-Queen Problem的主要内容,如果未能解决你的问题,请参考以下文章

UVA10516 Another Counting Problem大数+递推

UVA10516 Another Counting Problem大数+递推

UVa12186:Another Crisis(树形DP)

UVA 10689 Yet another Number Sequence

UVA12186 Another Crisis

UVa 11490 Just Another Problem