1753题目链接

题目大意:

一个4乘4的棋盘,上面放满了正反两面分别为黑和白的棋子,翻转一个棋子会让这个棋子上下左右的棋子也翻转,给定一个初始状态,求使所有棋子颜色相同所需的最少翻转次数。

解题思路:

先检查翻转0个棋子时是否所有棋子颜色一致,若不一致则翻转1个棋子,依次类推,若翻转某n个棋子后成功则n即为所求解,否则直到翻转16个棋子后仍未成功则输出“Impossible”。

翻转某n个棋子可用递归的方法,若递归函数中当前层翻转的是(i, j),则下一层递归函数从(i, j+ 1)开始选择,这样可以保证不重复。若j等于4(说明第i行已结束,)则应让j = 0; i++;到下一行中搜索。

用一个一维数组board[6]代表棋盘,其中board[i](i >=  1 && i <= 4)代表第i行,board[i]的二进制数最右边四位从左到右分别代表棋盘第i行的第1到4列。这样对棋子取反更方便。

 #include <iostream>
#include <cstdio>
using namespace std; int board[];
int state[][] = { { , , , }, { , , , } }; void read() {
char c;
for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
board[i] <<= ;
cin >> c;
if (c == 'b')
board[i] |= ;
}
}
} bool judge() {
if (board[] == && board[] == && board[] == && board[] == ||
board[] == && board[] == && board[] == && board[] == )
return true;
return false;
} void flip(int i, int j) {
i++; //下标从1开始
board[i] ^= state[][j];
board[i - ] ^= state[][j];
board[i + ] ^= state[][j];
} bool work(int n, int i, int j) {//还有n个棋子需翻转
if (n == )
return judge();
if (j == ) {
j = ; i++;
}
if ( - j + ( - i) * < n)
return false;
for (; i < ; i++) {
for (; j < ; j++) {
flip(i, j);
if(work(n - , i, j + ))
return true;
flip(i, j);
}
j = ;
}
return false;
} int main() {
read();
int i;
for (i = ; i <= ; i++) {
if (work(i, , ))
break;
}
if (i == )
cout << "Impossible" << endl;
else
cout << i << endl;
return ;
}

2965题目链接

与上题基本相同,只不过需要一个栈来记录翻转的坐标。

 #include <iostream>
#include <cstdio>
#include <stack>
using namespace std; int board[];
int state[] = { , , , };
stack<int> s; void read() {
char c;
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
board[i] <<= ;
scanf_s("%c", &c);
if (c == '-')
board[i] |= ;
}
scanf_s("%c", &c); //读入换行符
}
} void change(int i, int j) {
for (int t = ; t < ; t++) {
if (t != i)
board[t] ^= state[j];
}
board[i] ^= ;
} bool judge() {
for (int i = ; i < ; i++) {
if (board[i] != )
return false;
}
return true;
} bool work(int n, int i, int j) { //n为还需转换的个数
if (n == )
return judge();
if (j == ) {
j = ;
i++;
}
if (n > ( - j) + ( - i) * )
return false;
for (; i < ; i++) {
for (; j < ; j++) {
change(i, j);
if (work(n - , i, j + )) {
s.push(j + ); s.push(i + );
return true;
}
change(i, j);
}
j = ;
}
return false;
} int main() {
read();
int i, n = ;
for (i = ; i <= ; i++) {
if (work(i, , ))
break;
}
printf("%d\n", i);
while (!s.empty()) {
printf("%d", s.top());
s.pop();
printf(" %d\n", s.top());
s.pop();
}
return ;
}

poj 1753、2965枚举的更多相关文章

  1. [ACM训练] 算法初级 之 基本算法 之 枚举(POJ 1753+2965)

    先列出题目: 1.POJ 1753 POJ 1753  Flip Game:http://poj.org/problem?id=1753 Sample Input bwwb bbwb bwwb bww ...

  2. poj 1753 2965

    这两道题类似,前者翻转上下左右相邻的棋子,使得棋子同为黑或者同为白.后者翻转同行同列的所有开关,使得开关全被打开. poj 1753 题意:有一4x4棋盘,上面有16枚双面棋子(一面为黑,一面为白), ...

  3. poj—1753 (DFS+枚举)

                                                                                                        ...

  4. 枚举 POJ 1753 Flip Game

    题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...

  5. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  6. POJ 1753 Flip Game(高斯消元+状压枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45691   Accepted: 19590 Descr ...

  7. POJ 1753 Flip Game DFS枚举

    看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...

  8. POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题

    http://poj.org/problem?id=1222 http://poj.org/problem?id=1830 http://poj.org/problem?id=1681 http:// ...

  9. poj 1873 凸包+枚举

    The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6198   Accepted: 1 ...

  10. 穷举(四):POJ上的两道穷举例题POJ 1411和POJ 1753

    下面给出两道POJ上的问题,看如何用穷举法解决. [例9]Calling Extraterrestrial Intelligence Again(POJ 1411) Description A mes ...

随机推荐

  1. fis中的数据结构模块Config

    /* * config * caoke */ 'use strict'; Object.extend=function(props){ //继承父类 var prototype=Object.crea ...

  2. naginx安装入门

    一.nginx是什么 nginx是一个开源的,支持高性能,高并发的www服务和代理服务软件.它是一个俄罗斯人lgor sysoev开发的,作者将源代码开源出来供全球使用. nginx比它大哥apach ...

  3. WPF Binding的值转换器

    注意:值转换器中用于传入额外信息的参数 parameter 在 Binding 时使用 Binding 对象的 ConverterParameter 属性指定,但是设置了 ConverterParam ...

  4. html5使用local storage存储的数据在本地是以何种形式保存的

    html5使用local storage存储的数据是如何保存在本地的?(我使用的是chrome浏览器,chrom浏览器是用sqlite来保存本地数据的) Html5 的local storage 是通 ...

  5. (转)linux shell单引号、双引号及无引号区别

    原文:http://blog.csdn.net/woshizhangliang999/article/details/50132265 3.描述linux shell中单引号.双引号及不加引号的简单区 ...

  6. Python 中的反射和自省

    本文主要介绍Python中的反射和自省,以及该机制的简单应用 熟悉Java的程序员,一定经常和Class.forName打交道.即使不是经常亲自调用这个方法,但是在很多框架中(spring,eclip ...

  7. [转]how to use both JDK 7 and JDK 8 in one build

    Note: This article is original from https://gist.github.com/aslakknutsen/9648594 JDK 8 Released Most ...

  8. How to Configure Tomcat/JBoss and Apache HTTPD for Load Balancing and Failover

    http://java.dzone.com/articles/how-configure-tomcatjboss-and In this post we will see how to setup a ...

  9. smarty中函数的使用以及二维数组的使用

    1.虽然讲究前后台分离,但是如果如果有的项目,前后台分离的不彻底,或者有些必须要在HTML中处理,还是要用到PHP中的函数的: <% if $Role|in_array:$menuRole[$c ...

  10. git clone时的各种报错汇总

    npm ERR! path E:\aawork\1work\2019.2\package.json 没有在项目路径下 npm ERR! missing script: dev 需要 vue init ...