Fliptile 狀態壓縮搜索

狀態壓縮搜索:比如第一行有4個棋子,可以翻轉,翻轉的可能型就是都不翻,只翻第一個,只翻第二個,翻第一個和第二個,,,,很多種情況,每一種都有翻和不翻兩種,也就是2的4次方 這樣的話總不能去四重循環吧,壓縮法就是可以把以上的狀態簡化爲一重的循環,也就是0到15 就可以了

例題:
https://vjudge.net/problem/POJ-3279
Fliptile
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 on one side and white on the other side.

As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.

Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word “IMPOSSIBLE”.

Input
Line 1: Two space-separated integers: M and N
Lines 2… M+1: Line i+1 describes the colors (left to right) of row i of the grid with N space-separated integers which are 1 for black and 0 for white
Output
Lines 1… M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.
Sample Input
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
Sample Output
0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0

思路:
我下面代碼寫的思路是枚舉的第一行,然後往下走,看哪一個需要修改
其實還可以每一行都枚舉,也是用數字去枚舉,也是可以過得

開始的錯誤代碼:(找錯誤找了好久都沒有找到):

import java.util.Scanner;

class Test48{
    static int m;
    static int n;
    static int h;
    static int [][] graph ;
    static int [][] vis ;
    static int res;
    static int ans;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        m = sc.nextInt();
        n = sc.nextInt();
        res = m*n+1;
        ans = 0;
        graph = new int[m+1][n+1];
        vis = new int[m+1][n+1];
        for(int i =1;i<=m;i++){
            for(int j =1;j<=n;j++){
                graph[i][j] = sc.nextInt();
            }
        }

        double x = Math.pow(2,n);
        for(h=0;h<x;h++){
          dfs(h);
        }

        if(ans==res){
            System.out.println("IMPOSSIBLE");
        }else {
            for(int i =1;i<=m;i++){
                for(int j =1;j<=n;j++){
                    System.out.print(vis[i][j]+" ");
                }
                System.out.println();
            }
        }


    }

    static void dfs(int h){
        int ans = 0;
        for(int i=1;i<=n;i++){
            int g = h &1;
            if(g == 1){
                vis[1][i]=1;
                ans++;
            }
            h=h>>1;
        }
        for(int i=2;i<=m;i++){
            for(int j =1;j<=n;j++){
                if(is_black(i-1,j)){
                    vis[i][j] = 1;
                    ans++;
                }
            }
        }
        for(int i=1;i<=n;i++){
            if(is_black(m,i)){
                return;//直接返回到開始進入下一輪循環?
            }
        }

        if(ans<res){
            res = ans;
          //  return;
        }


    }

    static boolean is_black(int i,int j){
        int n = 0;
        int a[] ={-1,1,0,0};
        int b[] = {0,0,-1,1};
        if(graph[i][j]==1){
            for(int g= 0;g<4;g++){
                if((i+a[g])<0 || (i+a[g])> m  || (j+b[g])<0|| (j+b[g])>n) continue;
                if(vis[i+a[g]][j+b[g]]==1){
                    n++;
                }
            }
            if(n%2==0) return true;
            else return false;
        }

        if(graph[i][j]==0){
            for(int g= 0;g<4;g++){
                if((i+a[g])<0 || (i+a[g])> m  || (j+b[g])<0|| (j+b[g])>n) continue;
                if(vis[i+a[g]][j+b[g]]==1){
                    n++;
                }
            }
            if(n%2==0) return false;
            else return true;
        }
        return true;
    }
}

Ac 代碼:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 17;
const int dx[] = {-1, 0, 1, 0, 0};
const int dy[] = { 0,-1, 0, 1, 0};

int grid[N][N], state[N][N], tmp[N][N], rec[N][N];

int n, m, ans;
void flip(int x, int y) {
    tmp[x][y] = 1;
    int nx, ny;
    for(int i = 0; i < 5; i++) {
        nx = x+dx[i];
        ny = y+dy[i];
        state[nx][ny] = !state[nx][ny];
    }
}

bool isEmpty(int n) {
    for(int j = 1; j <= m; j++) {
        if(state[n][j])
            return false;
    }
    return true;
}

void solve(int st) {
    memcpy(state, grid, sizeof(grid));
    memset(tmp, 0, sizeof(tmp));

    int cnt = 0;
    for(int j = 0; j < m; j++) {
        if((st>>j) & 1) {
            flip(1, j+1);
            cnt++;
        }
    }

    for(int i = 2; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(state[i-1][j] == 1) {
                flip(i, j);
                cnt++;
            }
        }
    }

    if(isEmpty(n) && cnt < ans) {
        ans = cnt;
        memcpy(rec, tmp, sizeof(tmp));
    }
}

int main() {
    while(scanf("%d%d", &n, &m) != EOF) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= m; j++) {
                scanf("%d", &grid[i][j]);
            }
        }

        ans = INF;
        int end = 1 << m;

        for(int st = 0; st < end; st++)
            solve(st);

        if(ans == INF)
            puts("IMPOSSIBLE");
        else {
            for(int i = 1; i <= n; i++) {
                printf("%d", rec[i][1]);
                for(int j = 2; j <= m; j++)
                    printf(" %d", rec[i][j]);
                puts("");
            }
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章