poj 1753 Flip Game

poj 1753 Flip Game,第1张

poj 1753 Flip Game
#include <stdio.h>#include <stdlib.h>int all_white_or_black(int* bits, int len){  int i = 0;  for (i = 0; i < len - 1; i++)    if (bits[i] != bits[i + 1])      return 0;  return 1;}void change_color(int* arr, int i){  arr[i] = !(arr[i]);  int x = i/4;  int y = i%4;  if (y < 3)    arr[i + 1] = !(arr[i + 1]);  if (y > 0)    arr[i - 1] = !(arr[i - 1]);  if (x > 0)    arr[i - 4] = !(arr[i - 4]);  if (x < 3)    arr[i + 4] = !(arr[i + 4]);}void combine(int* arr, int len, int* result, int count, const int NUM, int* last){  int i;  for (i = len; i >= count; i--)  {    result[count - 1] = i - 1;    if (count > 1)      combine(arr, i - 1, result, count - 1, NUM, last);    else    {      int j = 0;      //在这里生成arr的副本      int* new_arr = (int*)malloc(sizeof(int)*16);      for (j = 0; j < 16; j++)        new_arr[j] = arr[j];      for (j = NUM - 1; j >=0; j--)      {         change_color(new_arr, result[j]);      }      if (all_white_or_black(new_arr, 16))      {         *last = NUM;         free(new_arr);         break;      }      free(new_arr);    }  }}int main(){  char str[5];  int bits[16];  int count = 15;  int lines = 4;  while (lines--)  {    scanf("%s", str);    int i;    for (i = 0; i < 4; i++)    {      if (str[i] == 'b')        bits[count--] = 1;      else        bits[count--] = 0;    }  }  if (all_white_or_black(bits, 16))    printf("%dn", 0);  else  {     int* new_bits = (int*)malloc(sizeof(int)*16);    int i;    for (i = 0; i < 16; i++)      new_bits[i] = bits[i];    int j;     int last = 0;    for (j = 1; j <= 16; j++)    {      int* result = (int*)malloc(sizeof(int)*j);      combine(new_bits, 16, result, j, j, &last);      if (last == j)      {        printf("%dn", last);        break;      }        for (i = 0; i < 16; i++)        new_bits[i] = bits[i];      free(result);    }    free(new_bits);    if (j == 17)      printf("Impossiblen");  }  return 0;}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4917923.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存