poj1753,暴力,二进制优化

106 篇文章 0 订阅
1 篇文章 0 订阅

poj1753

Language:DefaultFlip GameTime Limit: 1000MSMemory Limit: 65536KTotal Submissions: 65784Accepted: 26965DescriptionFlip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it’s black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules: Choose any one of the 16 pieces. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any). imgConsider the following position as an example: bwbw wwww bbwb bwwb Here “b” denotes pieces lying their black side up and “w” denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become: bwbw bwww wwwb wwwb The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.

#include <iostream>
#include <cstdio>
using namespace std;

int ret = 0;
char mp[10][10];

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

void nick(int x, int y)
{
    if(x < 0 || x >= 4 || y < 0 || y >= 4)
    {
        return ;
    }
    if(mp[x][y] == 'b')
    {
        mp[x][y] = 'w';
    }
    else
    {
        mp[x][y] = 'b';
    }
}

// 改变x y位置的颜色以及周边的颜色
void change(int x, int y) {
    nick(x, y);
    for(int i = 0; i < 4; i ++)
    {
        nick(x + dx[i], y + dy[i]);
    }
}

// 用来检测当前网格是否全部同色
bool check() {
    int a = mp[0][0];
    for(int i = 0; i < 4; i ++)
    {
        for(int j = 0; j < 4; j ++)
        {
            if(a != mp[i][j])
            {
                return false;
            }
        }
    }
    return true;
}

// 用来枚举所有的可能性
void dfs(int dep, int used) {
  if(dep == 16) {
      if(check())
      {
          if(used < ret)
          {
              ret = used;
          }
      }
      return ;
  }
    dfs(dep + 1, used);
    int x = dep/ 4;
    int y = dep % 4;
    change(x, y);
    dfs(dep + 1, used + 1);
    // 回溯,反悔
    change(x, y);
}

int main()
{
  for(int i = 0; i < 4; i ++)
  {
      scanf("%s", mp[i]);
  }
    
    ret = 20000000;
    
  dfs(0, 0);
  if(ret == 20000000) 
  {
  	puts("Impossible");
  	return 0;
  }
    printf("%d\n", ret);
  return 0;
}
#include <iostream>
#include <cstdio>
using namespace std;

char a[5][5];

int dx[] = {0, 1, 0, -1, 0};
int dy[] = {1, 0, -1, 0, 0};

bool check()
{
    char pivot = a[3][3];
    for(int i = 0; i < 4; i ++)
    {
        for(int j = 0; j < 4; j ++)
        {
            if(a[i][j] != pivot)
            {
                return false;
            }
        }
    }
    return true;
}

void flip(int x, int y)
{
    if(a[x][y] == 'b') a[x][y] = 'w';
    else               a[x][y] = 'b';
}

void change(int g)
{
    int x = g / 4;
    int y = g % 4;
    for(int i = 0; i < 5; i ++)
    {
        int nx = dx[i] + x;
        int ny = dy[i] + y;
        if(nx < 0 || nx >= 4 || ny < 0 || ny >= 4)
        {
            continue;
        }
        flip(nx, ny);
    }
}

int main()
{
    int ret = 101010101;
    int used;
    for(int i = 0; i < 4; i ++)
    {
        scanf("%s", a[i]);
    }
    for(int mark = 0; mark < 65536; mark ++)
    {
        int used = 0;
        for(int i = 0; i < 16; i ++)
        {
            if(mark & (1 << i))
            {
                change(i);
                used ++;
            }
        }
        if(check())
        {
            if(used < ret)
            {
                ret = used;
            }
        }
        for(int i = 0; i < 16; i ++)
        {
            if(mark & (1 << i))
            {
                change(i);
            }
        }
    }
    if(ret == 101010101) puts("Impossible");
    else                 printf("%d\n", ret);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值