枚举_百炼 2813 画家问题(枚举函数的应用,用熄灯问题的模板)

 1 #define _CRT_SECURE_NO_WARNINGS  
 2 #include <stdio.h>
 3 #include <math.h>
 4 #include <algorithm>
 5 #include <stdlib.h>
 6 #include <vector>
 7 #include <map>
 8 #include <queue>
 9 #include <string>
10 #include <iostream>
11 #include <ctype.h>
12 #include <string.h>
13 #include <set>
14 #include <stack>
15 #include<functional>
16 using namespace std;
17 #define Size 18
18 #define maxn  1<<30
19 #define minn  1e-6
20 int n;
21 int a[Size][Size];
22 int press[Size][Size];
23 int  ans=maxn;
24 int isFini = 0;
25 bool guest(){
26     int steps = 0;
27     for (int i = 1; i <= n; i++){
28         if (press[1][i]) steps++;
29     }
30     if (steps == n) isFini = 1;
31     for (int i = 1; i <n; i++)
32         for (int j = 1; j <= n; j++){
33             press[i + 1][j] = (press[i][j] + a[i][j] + press[i - 1][j] + press[i][j - 1] + press[i][j + 1])%2;
34         }
35     for (int i = 1; i <= n; i++){
36         if (a[n][i] != (press[n][i] + press[n-1][i] + press[n][i - 1] + press[n][i + 1])%2) return false;
37     }
38     steps = 0;
39     for (int i = 1; i <= n; i++)
40         for (int j = 1; j <= n; j++){
41             if (press[i][j]) steps++;
42         }
43     if (steps < ans) ans = steps;
44     return false;
45 }
46 void solve(){
47     int c = 1;
48     while (guest()==false){
49         if (isFini) break;
50         press[1][1]++;
51         c = 1;
52         while (press[1][c] > 1){
53             press[1][c] = 0;
54             c++;
55             press[1][c]++;
56         }
57     }
58     if (ans == maxn) cout << "inf" << endl;
59         else cout << ans << endl;
60 }
61 int main(){
62     cin >> n;
63     for (int i = 1; i <= n; i++)
64         for (int j = 1; j <= n; j++){
65             char c;
66             cin >> c;
67             a[i][j] = c == 'w' ? 1 : 0;
68         }
69     solve();
70     system("pause");
71 }

 

转载于:https://www.cnblogs.com/MapReduce/p/8393056.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值