Enum:Fliptile(POJ 3279)

                

                  Fliptile

  题目大意:农夫想要测牛的智商,于是他把牛带到一个黑白格子的地,专门来踩格子看他们能不能把格子踩称全白

  这一题其实就是一个枚举题,只是我们只用枚举第一行就可以了,因为这一题有点像开关一样,一个翻了,另一个就要跟着一起翻,第一行会影响下面所有行,而且影响情况只有一种,所以枚举完了以后我们不断模拟就可以了

  

 1 #include <iostream>
 2 #include <functional>
 3 #include <algorithm>
 4
 5 using namespace std;
 6
 7 static int map[18][18], flip[18][18], ans_map[18][18], M, N;
 8 static int dirx[4] = { -1, 0, 0, 1 };
 9 static int diry[4] = { 0, -1, 0, 0 };
10
11 int solve(void);
12 int get_color(const int, const int);
13
14 int main(void)//开关问题,枚举第一行即可得到下面几行的关系
15 {
16     int ans, tmp;
17     while (~scanf("%d%d", &N, &M))//读图
18     {
19         for (int i = 0; i < N; i++)
20             for (int j = 0; j < M; j++)
21                 scanf("%d", &map[i][j]);
22         ans = INT_MAX;
23         for (int i = 0; i < 1 << M; i++)//枚举第一行
24         {
25             memset(flip, 0, sizeof(flip));
26             for (int j = 0; j < M; j++)//枚举结果填入第一行
27                 if ((1 << j & i) != 0)
28                     flip[0][M - j - 1] = 1;
29             tmp = solve();
30             if (tmp >= 0 && tmp < ans)
31             {
32                 ans = tmp;
33                 memcpy(ans_map, flip, sizeof(flip));
34             }
35         }
36
37         if (ans == INT_MAX)
38             printf("IMPOSSIBLE\n");
39         else
40         {
41             for (int i = 0; i < N; i++)
42             {
43                 for (int j = 0; j < M; j++)
44                     printf("%d ",ans_map[i][j]);
45                 printf("\n");
46             }
47         }
48     }
49     return 0;
50 }
51
52 int solve(void)
53 {
54     int ans = 0;
55     for (int i = 1; i < N; i++)//从第二行开始一行一行模拟
56         for (int j = 0; j < M; j++)
57             if (get_color(j, i - 1) == 1)//记住是上一行翻的牌子
58                 flip[i][j] = 1;//说明需要翻
59
60     for (int j = 0; j < M; j++)//得到最后一行的所有格子的颜色,如果还有黑色,则说明这一步无法把格子全部翻转成白色
61         if (get_color(j, N - 1) == 1)
62             return -1;
63
64     for (int i = 0; i < N; i++)//统计步数
65         for (int j = 0; j < M; j++)
66             if (flip[i][j])
67                 ans++;
68     return ans;
69 }
70
71 int get_color(const int x, const int y)
72 {
73     //得到(x,y)位置的颜色
74     int ny, nx, sum = map[y][x];
75
76     for (int i = 0; i < 4; i++)
77     {
78         nx = x + dirx[i]; ny = y + diry[i];
79         if ((0 <= nx&&nx < M) && (0 <= ny&&ny < N))
80             sum += flip[ny][nx];
81     }
82     return sum % 2;
83 }

  

时间: 2024-09-28 19:13:20

Enum:Fliptile(POJ 3279)的相关文章

Fliptile(POJ 3279)

原题如下: Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16494   Accepted: 6025 Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows i

翻转问题 Fliptile POJ - 3279

Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black

Fliptile POJ - 3279(状态压缩)

题意:给你一个n * m的矩阵,元素为0/1, 求把所有的元素变成0所需要的最少操作.(每对一个格子操作,该十字格的元素反转) 分析:一看数据量 <= 15, 就有点状态压缩的感jio.从小到大枚举第一行的操作,因为第一行的操作决定了后面所有的操作,所以最后判断对于第一行的操作是不是符合题意即可. 代码: 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <

Fliptile POJ 3279(反转)

原题 题目链接 题目分析 对于N行M列的矩阵,先看看左上角的点,能使该点翻转的有(1,1).(1,2)和(2,1),无法确定该通过哪个点来翻转(1,1).不妨先确定好第一行的翻转情况,也就是说该不该翻(1,1)和(1,2)已经确定了,则此时只有(2,1)能翻转点(1,1),这时候就能直接判断了,因此可以确定第二行的翻转情况,同理迭代到最后一行,则整个矩阵的翻转情况都已确认完毕,此时只需要检验最后一行是否有黑色-1的即可.这里枚举第一行的翻转情况可用二进制枚举,则总的复杂度为O(N*M*2M).

D - Fliptile POJ - 3279

Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black

状态压缩+枚举 POJ 3279 Fliptile

题目传送门 1 /* 2 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 3 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 const int MAXN = 20; 11 const int INF = 0x3f3f3f3

POJ 3279(Fliptile)题解

以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑色所需翻转的是哪些棋子(这句话好长...). [题目分析] 盯着奇怪的题目看了半天发现和奶牛没什么卵关系..奶牛智商高了产奶高是什么鬼说法... 这题刚开始被放到搜索的分类下了..然而这和搜索有什么关系..没经验所以想了各种和搜索沾边的方法,结果没想出解法,直到看了网上的题解,压根不是搜索啊有木有.

POJ 3279 Fliptile (二进制枚举+模拟)

Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3992   Accepted: 1518 Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which

poj 3279 Fliptile (简单搜索)

Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16558   Accepted: 6056 Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in whic