码迷,mamicode.com
首页 > 其他好文 > 详细

P1985 [USACO07OPEN]翻转棋

时间:2019-10-03 18:12:49      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:getch   tchar   name   har   pre   char   dfs   for   ++i   

题目链接:

翻转棋

题目分析:

先状压/\(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]翻转棋

标签:getch   tchar   name   har   pre   char   dfs   for   ++i   

原文地址:https://www.cnblogs.com/kma093/p/11620134.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!