poj1753 Flip Game
/*
* Author: poj_whut_shz
* Problem: Flip Game
* Code: poj_1753
* URL: http://poj.org/problem?id=1753
* Data: 2019/9/5
*/
/*
* 【题意分析】:
* 有一个4 x 4的正方形,共有16个格子,每个格子要么是黑色,要么是白色。
* 当把一个格子的颜色改变时,其周围的格子的颜色也被反转,给定一个初始状态
* 问至少反转几个格子可以使这个4 x 4的正方形变为纯白或者纯黑?
* 如果初始状态已经达到要求输出0。如果不可能完成,就输出Impossible。
*
*/
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
//visited记录不同状态下的矩阵是否已经出现过,总共有65536种可能
//为了避免重复翻牌,也是避免回路重复翻牌的情况
private static boolean visited[] = new boolean[65536];
// 棋盘状态掩码:当翻转位置i的棋子时,flip[i]为所有受影响的相邻位置
// 位置i:在4x4棋盘内,从左到右、从上到下按0-15依次编码
private static int[] flip = new int[] {
0x0000C800, 0x0000E400, 0x00007200, 0x00003100,
0x00008C80, 0x00004E40, 0x00002720, 0x00001310,
0x000008C8, 0x000004E4, 0x00000272, 0x00000131,
0x0000008C, 0x0000004E, 0x00000027, 0x00000013
};
//记录初始状态
private static int start;
//读取输入数据并对其进行二进制压缩
private static void readExternalData() {
//读取输入数据
Scanner sc = new Scanner(System.in);
String[] strs = new String[4];
for (int i = 0; i < 4; i++) {
strs[i] = sc.nextLine();
}
//对输入数据进行二进制压缩,黑0白1
int state = 0;
for (int i = 0; i < strs.length; i++) {
int len = strs[i].length();
for (int j = 0; j < len; j++) {
state <<= 1;
if (strs[i].charAt(j) == 'b') {
state += 1;
}
}
}
//压缩结果传递给start参数
start = state;
sc.close();
}
//广度优先搜索
private static int bfs(int state) {
//如果输入的是全黑或全白
if (state == 0 || state == 0xFFFF) {
return 0;
}
Queue<Chess> queue = new LinkedList<Chess>();//创建状态队列
Chess chess = new Chess(state, 0);//创建初始状态
visited[state] = true;//记录状态
queue.add(chess);//初始状态入队
while (!queue.isEmpty()) {
Chess cur = queue.poll();
for (int i = 0; i < 16; i++) {
int result = cur.state ^ flip[i];//从第一个棋子开始翻转
//如果状态存在则舍去
if (visited[result]) {
continue;
}
//如果结果为全白或全黑
if (result == 0 || result == 0xFFFF) {
return cur.step + 1;
}
//否则入队并记录状态
Chess newChess = new Chess(result, cur.step + 1);
visited[result] = true;
queue.add(newChess);
}
}
return -1;
}
public static void main(String[] args) {
readExternalData();//读取输入数据并对其进行二进制压缩
int end = bfs(start);//广度优先搜索
if (end == -1) {
System.out.println("Impossible");
} else {
System.out.println(end);
}
}
}
class Chess {
public int state;//矩阵状态
public int step;//翻转次数
public Chess() {
}
public Chess(int state, int step) {
this.state = state;
this.step = step;
}
public String toString() {
return "Node1753 [state=" + state + ", step=" + step + "]";
}
}
poj2965 The Pilots Brothers' refrigerator
这题和POJ1753翻转棋的思想是一致的,但由于要求输出翻转过程,因此需要使用DFS(找到目标后,还要过程回溯)。
同时通过看别人的思路发现该题还有一种更巧妙的解法,太强了。
方法一:巧妙解法
参考链接:https://www.cnblogs.com/Java-tp/p/3873557.html
方法二:DFS+回溯
/*
* Author: poj_whut_shz
* Problem: The Pilots Brothers' refrigerator
* Code: poj_2965
* URL: http://poj.org/problem?id=2965
* Data: 2019/9/5
*/
/*
* 【题意分析】:
* 一个冰箱上有4*4共16个开关,改变任意一个开关的状态(即开变成关,关变成开)时,
* 此开关的同一行、同一列所有的开关都会自动改变状态,
* 要想打开冰箱,要所有开关全部打开才行。
* 输入:一个4×4的矩阵,+表示关闭,-表示打开;
* 输出:使冰箱打开所需要执行的最少操作次数,以及所操作的开关坐标。
*
*/
import java.util.Scanner;
public class Main {
//记录棋盘状态
private static int chess;
private static int step;
private static boolean flag=false;
private static int[] ri = new int[16];
private static int[] cj = new int[16];
//读取输入数据并对其进行二进制压缩
private static void readExternalData() {
//读取输入数据
Scanner sc = new Scanner(System.in);
String[] strs = new String[4];
for (int i = 0; i < 4; i++) {
strs[i] = sc.nextLine();
}
//对输入数据进行二进制压缩,关0开1
int state = 0;
for (int i = 0; i < strs.length; i++) {
int len = strs[i].length();
for (int j = 0; j < len; j++) {
state <<= 1;
if (strs[i].charAt(j) == '-') {
state += 1;
}
}
}
//压缩结果传递给chess参数
chess = state;
sc.close();
}
//判断冰箱是否已经打开
private static boolean isOpen() {
if(chess == 0xFFFF) {
return true;
}
else {
return false;
}
}
//对开关进行翻转操作
private static void flip(int bit) {
chess = chess ^ (0x1 << bit); //对翻转位取反
int row = bit / 4; //翻转位所在行
int col = bit % 4; //翻转位所在列
//对全行取反
for(int c = 0; c < 4; c++) {
chess = chess ^ (0x1 << (row * 4 + c));
}
//对全列取反
for(int r = 0; r < 4; r++) {
chess = chess ^ (0x1 << (r * 4 + col));
}
return;
}
//深度优先搜索
private static void dfs(int bit, int deep) {
//遍历到底,返回
if(deep == step) {
flag = isOpen();
return;
}
//冰箱打开或操作开关位置大于总开关个数,返回
if(flag || bit > 15) {
return;
}
int row = ri[deep] = bit / 4; //开关所在行数
int col = cj[deep] = bit % 4; //开关所在列数
//深度搜索
flip(bit); //翻转开关
if(col < 4) { //继续搜索该行下一开关
dfs(bit + 1, deep + 1);
}
else { //如果搜索完则跳转至下一行
dfs((bit + 4) / 4 * 4, deep + 1);
}
//深度不变
flip(bit); //翻转开关
if(col < 4) { //如果横向未搜索完,则继续搜索横向的下一个开关
dfs(bit + 1, deep);
}
else { //否则跳转至下一行
dfs((bit + 4) / 4 * 4, deep);
}
return;
}
public static void main(String[] args) {
readExternalData();//读取输入数据并对其进行二进制压缩
System.out.println(chess);
for(step = 0; step < 16; step++) {
dfs(0, 0);
//直到为真则退出
if(flag) {
break;
}
}
System.out.println(step);
for(int i = 0; i < step; i++) {
System.out.println((ri[i] + 1) + " " + (cj[i] + 1));
}
}
}