poj 1753【枚举+dfs(位向量法)】
来源:互联网 发布:淘宝卖家注册流程图 编辑:程序博客网 时间:2024/06/07 17:48
Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 46519 Accepted: 19915
Description
Flip 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:
Consider 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.
- 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).
Consider 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.
Input
The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.
Output
Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).
Sample Input
bwwbbbwbbwwbbwww
Sample Output
4题意:给你一个4*4的棋盘,黑棋是‘b’,白棋是‘w’,问最少翻转多少次,可以使棋盘颜色统一。棋子有黑白两面可以翻转,翻转一个棋子,周围的棋子都要翻转。思路:《算法竞赛入门经典》上提到枚举子集的方法之一---位向量法。这种方法就是把棋盘所有可能的翻转子集情况枚举一遍,如果在翻转过程中,棋盘颜色得以统一,就将现在的步数与最小值进行比较,如果小于最小值,就更新它。
#include<stdio.h>#define inf 0x3f3f3f3fint map[5][5],min,n;int Check(int map[][5])//检查棋盘颜色是否统一 { int i,j; for(i = 0; i < 4; i ++) for(j = 0; j < 4; j ++) if(map[0][0] != map[i][j]) return 0; return 1;}void Reverse(int num)//翻转棋子 { int x,y; x = num/4; y = num%4; map[x][y]=!map[x][y]; if(x>0) map[x-1][y]=!map[x-1][y]; if(x<3) map[x+1][y]=!map[x+1][y]; if(y>0) map[x][y-1]=!map[x][y-1]; if(y<3) map[x][y+1]=!map[x][y+1]; return;}void dfs(int num,int step){ if(Check(map)) { if(step < min) min = step; return; } if(num >= n) return; dfs(num+1,step); Reverse(num);//翻转棋子 dfs(num+1,step+1);//step记录已经翻转过的棋子 Reverse(num);//回溯 }int main(){ int i,j; char ch; for(i = 0; i < 4; i ++) { for(j = 0; j < 4; j ++) { scanf("%c",&ch); if(ch == 'b')//构建棋盘 map[i][j] = 0; else map[i][j] = 1; } getchar(); } n = 16; min = inf; dfs(0,0); if(min == inf) printf("Impossible\n"); else printf("%d\n",min); return 0;}
阅读全文
0 0
- poj 1753【枚举+dfs(位向量法)】
- POJ 1753(DFS+枚举||BFS+位运算)
- POJ 1753 - 位运算(异或)+BFS / DFS+枚举
- POJ 1753 dfs+枚举
- poj 1753 枚举+dfs
- poj 1753 Flip Game(dfs+枚举)
- poj 1753 Flip Game (dfs + 枚举)
- POJ 1753 Flip Game(dfs+枚举)
- POJ 1753 Flip Game(dfs+枚举)
- POJ 1753-Flip Game(枚举&&DFS)
- (枚举,dfs)Flip Game poj 1753
- POJ 1753 Flip Game(枚举+dfs)
- poj 1753 枚举加dfs
- POJ 2965(DFS+枚举)
- poj 1753||poj 2965 枚举+dfs
- POJ 1753 Flip Game 翻转(枚举+dfs)
- POJ 1753 Flip Game 暴力搜索(dfs加枚举)
- 文章标题 POJ 1753 : Flip Game(枚举+dfs)
- 总结一下安装LoadRunner会遇到的坑
- linux中kconfig-kbuild-makefile分析
- 机器学习(11)-大数据训练
- FFMPEG学习【组件文档——视频缩放和像素格式转换器】
- 分类与检索
- poj 1753【枚举+dfs(位向量法)】
- VS2015 Git 源码管理工具简单入门
- 北交大选课系统抢课的GIT地址
- 26. Remove Duplicates from Sorted Array(从排序数组中删除重复) —— Java
- 大数据的核心价值是什么
- Bitmap: Error, cannot access an invalid/free'd bitmap here!
- Android消息机制底层原理
- 《oracle虚拟机配置流程(四)测试网站并登录》共四步
- test1