poj 1753 2965

状态压缩+枚举

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <map>
 7 #include <algorithm>
 8 #include <list>
 9 #include <ctime>
10 #include <set>
11 #include <queue>
12 
13 using namespace std;
14 typedef long long ll;
15 
16 int relation[4][4];
17 int dx[] = { 1, 0, -1, 0 };
18 int dy[] = { 0, 1, 0, -1 };
19 void addone(int x, int y, int& res) {
20     if (x < 0 || x >= 4 || y < 0 || y >= 4) {
21         return;
22     }
23     int offset = x * 4 + y;
24     res = (res | (1 << offset));
25 
26 }
27 int main() {
28     string data[4];
29     for (int i = 0; i < 4; i++) {
30         cin >> data[i];
31     }
32     int dp, dpori;
33     dp = dpori = 0;
34     for (int i = 0; i < 4; i++) {
35         for (int j = 0; j < 4; j++) {
36             if (data[i][j] == 'b') {
37                 addone(i, j, dp);
38             }
39         }
40     }
41     dpori = dp;
42     for (int i = 0; i < 4; i++) {
43         for (int j = 0; j < 4; j++) {
44             addone(i, j, relation[i][j]);
45             for (int k = 0; k < 4; k++) {
46                 addone(i + dx[k], j + dy[k], relation[i][j]);
47             }
48         }
49     }
50     int res = 1000;
51     int allstate = (1 << 16) - 1;
52     for (int i = 0; i <= allstate; i++) {
53         dp = dpori;
54         int cun = 0;
55 
56         for (int a = 0; a < 4; a++) {
57             for (int b = 0; b < 4; b++) {
58                 int tnum = a * 4 + b;
59                 if (i & (1 << tnum)) {
60                     cun++;
61                     dp = dp ^ (relation[a][b]);
62                 }
63             }
64         }
65         if (0 == dp || allstate == dp) {
66             res = min(res, cun);
67         }
68 
69     }
70     if (1000 == res) {
71         cout << "Impossible";
72         return 0;
73     }
74     cout << res;
75     return 0;
76 }

 

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <map>
 7 #include <algorithm>
 8 #include <list>
 9 #include <ctime>
10 #include <set>
11 #include <queue>
12 
13 using namespace std;
14 typedef long long ll;
15 
16 int relation[4][4];
17 int dx[] = { 1, 0, -1, 0 };
18 int dy[] = { 0, 1, 0, -1 };
19 void addone(int x, int y, int& res) {
20     if (x < 0 || x >= 4 || y < 0 || y >= 4) {
21         return;
22     }
23     int offset = x * 4 + y;
24     res = (res | (1 << offset));
25 
26 }
27 int main() {
28     string data[4];
29 /*    string data[4]={"-+--","----","----","-+--"};*/
30     for (int i = 0; i < 4; i++) {
31         cin >> data[i];
32     }
33     int dp, dpori;
34     dp = dpori = 0;
35     for (int i = 0; i < 4; i++) {
36         for (int j = 0; j < 4; j++) {
37             if (data[i][j] == '+') {
38                 addone(i, j, dp);
39             }
40         }
41     }
42     dpori = dp;
43     for (int i = 0; i < 4; i++) {
44         for (int j = 0; j < 4; j++) {
45             for (int k = 0; k < 4; k++) {
46                 addone(i, k, relation[i][j]);
47                 addone(k, j, relation[i][j]);
48             }
49         }
50     }
51     int res = 1000;
52     int restag=0;
53     int allstate = (1 << 16) - 1;
54     for (int i = 0; i <= allstate; i++) {
55         dp = dpori;
56         int cun = 0;
57 
58         for (int a = 0; a < 4; a++) {
59             for (int b = 0; b < 4; b++) {
60                 int tnum = a * 4 + b;
61                 if (i & (1 << tnum)) {
62                     cun++;
63                     dp = dp ^ (relation[a][b]);
64                 }
65             }
66         }
67         if (0 == dp ) {
68             if(res>cun){
69             res = min(res, cun);
70             restag=i;
71             }
72         }
73 
74     }
75     if (1000 == res) {
76         cout << "Impossible";
77         return 0;
78     }
79     cout << res<<endl;
80     for(int i=0;i<16;i++){
81         if(restag&(1<<i)){
82             int x=i/4;
83             int y=i%4;
84             cout<<(x+1)<<" "<<(y+1)<<endl;
85         }
86     }
87     return 0;
88 }

 

 

 

posted on 2013-03-31 22:21  kakamilan  阅读(135)  评论(0编辑  收藏  举报

导航