zoukankan      html  css  js  c++  java
  • poj 1753 flip game

    直接枚举

    //2011-11-1
    #include <iostream>
    #include <cstdio>

    using namespace std;

    void Replace(char a[], int b[]);
    int Search(int a[]);
    bool Judge(int a[]);
    void Flip(int b[], int k);

    int main()
    {
    char a[16], c;
    int b[16];
    int i = 0;
    while ((c = getchar()) != EOF && i < 16)
    if (c == 'b' || c == 'w')
    {
    a[i] = c;
    ++i;
    }
    Replace(a, b);
    int ans = Search(b);
    if (ans != -1) cout << ans << endl;
    else cout << "Impossible";

    return 0;
    }

    void Repeat(int n, int i[])
    {
    for (int k= 15; k >= 0; --k)
    {
    i[k] = n%2;
    n /= 2;
    }
    }
    int Search(int a[])
    {
    bool FLAG = false;
    int i[16], b[16];
    int min = 2000;
    for (int counter = 65535; counter >= 0; --counter)
    {
    for (int j = 0; j < 16; ++j)
    {
    b[j] = a[j];
    i[j] = 0;
    }
    Repeat(counter, i);
    int count = 0;
    for (int k = 0; k < 16; ++k)
    if (i[k] == 1)
    {
    Flip(b, k);
    ++count;
    }
    if (Judge(b) && count < min)
    {
    FLAG = true;
    min = count;
    }
    }
    if (FLAG) return min;
    return -1;
    }
    bool Judge(int a[])
    {
    int sum = 0;
    for (int i = 0; i < 16; ++i)
    sum += a[i];
    if (sum == 16 || sum == 0)
    return true;
    return false;
    }
    void Flip(int b[], int k)
    {
    b[k] = 1 - b[k];
    int i = k%4, j = k/4;
    if (i < 3) b[k+1] = 1 - b[k+1];
    if (i > 0) b[k-1] = 1 - b[k-1];
    if (j < 3) b[k+4] = 1 - b[k+4];
    if (j > 0) b[k-4] = 1 - b[k-4];
    }
    void Replace(char a[], int b[])
    {
    for (int i = 0; i < 16; ++i)
    {
    if (a[i] == 'b')
    b[i] = 1;
    else b[i] = 0;
    }
    }



  • 相关阅读:
    Markdown基本语法
    面向对象
    LeetCode739 每日温度
    LeetCode155 最小栈
    LeetCode279 完全平方数
    LeetCode752 打开转盘锁
    LeetCode622 设计循环队列
    LeetCode200 岛屿的个数
    LeetCode61 旋转链表
    LeetCode138 复制带随机指针的链表
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2357803.html
Copyright © 2011-2022 走看看