P1985 [USACO07OPEN]翻转棋

Posted kma093

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P1985 [USACO07OPEN]翻转棋相关的知识,希望对你有一定的参考价值。

题目链接:

翻转棋

题目分析:

先状压/\(dfs\)枚举第一排状态,然后在每个\(1\)下面翻,即确定了第一排就确定了后面的状态
最后验证一下最后一排是不是全0即可

代码:

#include<bits/stdc++.h> 
#define N 50
using namespace std;
inline int read() 
    int cnt = 0, f = 1; char c = getchar();
    while (!isdigit(c)) if (c == '-') f = -f; c = getchar();
    while (isdigit(c)) cnt = (cnt << 3) + (cnt << 1) + (c ^ 48); c = getchar();
    return cnt * f;

int mapp[N][N], m, n, cnt, ans[N][N], res = (1 << 30);
bool vis[N][N], flag;
void rev(int x, int y) 
    mapp[x][y] ^= 1;
    mapp[x][y - 1] ^= 1;
    mapp[x - 1][y] ^= 1;
    mapp[x][y + 1] ^= 1;
    mapp[x + 1][y] ^= 1; 

void cpy() 
    for (register int i = 1; i <= m; ++i)
        for (register int j = 1; j <= n; ++j) ans[i][j] = vis[i][j];

void work() 
    for (register int i = 2; i <= m; ++i)
        for (register int j = 1; j <= n; ++j) if (mapp[i - 1][j]) rev(i, j), vis[i][j] = 1, ++cnt;
    flag = 0;
    for (register int i = 1; i <= m; ++i)
        for (register int j = 1; j <= n; ++j) if (mapp[i][j]) flag = 1;
    if (!flag) if (cnt < res) cpy(), res = cnt;

    for (register int i = 2; i <= m; ++i)
        for (register int j = 1; j <= n; ++j) if (vis[i][j]) rev(i, j), vis[i][j] = 0, --cnt; 

void dfs_(int k) 
    if (!k) work(); return;
    dfs_(k - 1), rev(1, k), vis[1][k] = 1, ++cnt;
    dfs_(k - 1), rev(1, k), vis[1][k] = 0, --cnt;

int main() 
    m = read(), n = read();
    for (register int i = 1; i <= m; ++i)
        for (register int j = 1; j <= n; ++j) mapp[i][j] = read();
    dfs_(n); 
    if (res == (1 << 30)) return printf("IMPOSSIBLE"), 0; 
    for (register int i = 1; i <= m; ++i) 
        for (register int j = 1; j <= n; ++j) printf("%d ", ans[i][j]);
        printf("\n");
    
    return 0;
#include<bits/stdc++.h> 
#define N 50
using namespace std;
inline int read() 
    int cnt = 0, f = 1; char c = getchar();
    while (!isdigit(c)) if (c == '-') f = -f; c = getchar();
    while (isdigit(c)) cnt = (cnt << 3) + (cnt << 1) + (c ^ 48); c = getchar();
    return cnt * f;

int mapp[N][N], m, n, cnt, ans[N][N], res = (1 << 30);
bool vis[N][N], flag;
void rev(int x, int y) 
    mapp[x][y] ^= 1;
    mapp[x][y - 1] ^= 1;
    mapp[x - 1][y] ^= 1;
    mapp[x][y + 1] ^= 1;
    mapp[x + 1][y] ^= 1; 

void cpy() 
    for (register int i = 1; i <= m; ++i)
        for (register int j = 1; j <= n; ++j) ans[i][j] = vis[i][j];

void work() 
    for (register int i = 2; i <= m; ++i)
        for (register int j = 1; j <= n; ++j) if (mapp[i - 1][j]) rev(i, j), vis[i][j] = 1, ++cnt;
    flag = 0;
    for (register int i = 1; i <= m; ++i)
        for (register int j = 1; j <= n; ++j) if (mapp[i][j]) flag = 1;
    if (!flag) if (cnt < res) cpy(), res = cnt;

    for (register int i = 2; i <= m; ++i)
        for (register int j = 1; j <= n; ++j) if (vis[i][j]) rev(i, j), vis[i][j] = 0, --cnt; 

void dfs_(int k) 
    if (!k) work(); return;
    dfs_(k - 1), rev(1, k), vis[1][k] = 1, ++cnt;
    dfs_(k - 1), rev(1, k), vis[1][k] = 0, --cnt;

int main() 
    m = read(), n = read();
    for (register int i = 1; i <= m; ++i)
        for (register int j = 1; j <= n; ++j) mapp[i][j] = read();
    dfs_(n); 
    if (res == (1 << 30)) return printf("IMPOSSIBLE"), 0; 
    for (register int i = 1; i <= m; ++i) 
        for (register int j = 1; j <= n; ++j) printf("%d ", ans[i][j]);
        printf("\n");
    
    return 0;

以上是关于P1985 [USACO07OPEN]翻转棋的主要内容,如果未能解决你的问题,请参考以下文章

[题解] luogu P1985 [USACO07OPEN]翻转棋

BZOJ 1647 [Usaco2007 Open]Fliptile 翻格子游戏:部分枚举 位运算

USACO35 翻转奶牛(尺取法)

Fliptile (二进制压缩)

bzoj 1704: [Usaco2007 Mar]Face The Right Way 自动转身机贪心+差分

bzoj 1647: [Usaco2007 Open]Fliptile 翻格子游戏dfs