poj1753枚举
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 33670 | Accepted: 14713 |
Description
- 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
Output
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
bwwb
bbwb
bwwb
bwww
Sample Output
4
Source
解析
真是醉了,双重dfs,如果对dfs不是很了解的同学就放弃这道题吧,想了4个小时才想通,脑细胞都快没了,
其实就是step从0枚举到16,之后dfs结构中主要是对每一种可能进行位移式的枚举,一部一部向后推得枚举
附代码
ps:其实本人代码与网上的代码差不多,
本人也是参考网上代码才解决的这道问题
如过有谁不懂可以Q我,1097944404
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int map[5][5],step,flag;
int judge(){
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(map[i][j]!=map[0][0])
return 0;
}
}
return 1;
}
void update(int x,int y){
map[x][y]^=1;
if(x>0)
map[x-1][y]^=1;
if(x<3)
map[x+1][y]^=1;
if(y>0)
map[x][y-1]^=1;
if(y<3)
map[x][y+1]^=1;
}
void dfs(int x,int y,int step1){
if(step1==step){
flag=judge();
return ;
}
if(flag||x==4)
return;
update(x,y);
if(y<3)
dfs(x,y+1,step1+1);
else
dfs(x+1,0,step1+1);
update(x,y);
if(y<3)
dfs(x,y+1,step1);
else
dfs(x+1,0,step1);
}
int main(){
char z;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
scanf("%c",&z);
if(z=='b')
map[i][j]=0;
else
map[i][j]=1;
}
getchar();
}
flag=0;
for( step=0;step<=16;step++){
dfs(0,0,0);
if(flag)
break;
}
if(flag)
printf("%d\n",step);
else
printf("Impossible\n");
return 0;
}
、、、、、可以用位运算
把其化成一个整数,最大的时候是65535,最小的时候是0,当时的情况就是全是黑色的或是白色的
用数组标记形成每一个数的时候所需要的次数,当为0或是65535的时候就是正确的答案了,
change函数主要是改变相应的19个位置的某个位置的数,改变成位0或1,update函数同样则是更新改变的那个点和他周围需要改变的点
位运算真的很好用,今天才通过这个题对位运算了解了不少知识!
附代码
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<math.h>
#include<algorithm>
using namespace std;
int ans;
void change(int &t,int j){
t^=(1<<j);
}
int update(int count,int i){
change(count,i);
if(i>3)
change(count,i-4);
if(i<12)
change(count,i+4);
if(i%4!=0)
change(count,i-1);
if(i%4!=3)
change(count,i+1);
return count;
}
void dfs(){
queue<int>q;
int a[65536];
memset(a,0,sizeof(a));
q.push(ans);
while(!q.empty()){
int cnt=q.front();
q.pop();
if(cnt==0||cnt==65535){
printf("%d\n",a[cnt]);
return ;
}
for(int i=0;i<16;i++){
int temp=update(cnt,i);
if(!a[temp]){
a[temp]=a[cnt]+1;
q.push(temp);
}
}
}
printf("Impossible\n");
return ;
}
int main(){
char c;
int temp;
for(int i=0;i<16;i++){
cin>>c;
if(c=='b')
temp=1;
else
temp=0;
ans+=temp<<i;
}
dfs();
return 0;
}
poj1753枚举的更多相关文章
- POJ-1753 Flip Game---二进制枚举子集
题目链接: https://vjudge.net/problem/POJ-1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白-> ...
- POJ1753 Flip Game(bfs、枚举)
链接:http://poj.org/problem?id=1753 Flip Game Description Flip game is played on a rectangular 4x4 fie ...
- [POJ1753]Flip Game(异或方程组,高斯消元,枚举自由变量)
题目链接:http://poj.org/problem?id=1753 题意:同上. 这回翻来翻去要考虑自由变元了,假设返回了自由变元数量,则需要枚举自由变元. /* ━━━━━┒ギリギリ♂ eye! ...
- poj1753解题报告(枚举、组合数)
POJ 1753,题目链接http://poj.org/problem?id=1753 题意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白-> ...
- poj-3279 poj-1753(二进制枚举)
题目链接:http://poj.org/problem?id=3279 题目大意: 有一个m*n的棋盘(1 ≤ M ≤ 15; 1 ≤ N ≤ 15),每个格子有两面分别是0或1,每次可以对一个格子做 ...
- 二进制枚举例题|poj1222,poj3279,poj1753
poj1222,poj3279,poj1753 听说还有 POJ1681-画家问题 POJ1166-拨钟问题 POJ1054-讨厌的青蛙
- POJ1753 Flip Game(位运算+暴力枚举)
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...
- POJ1753(位操作和枚举)
题目:http://poj.org/problem?id=1753 题意:一块4*4的棋盘,黑白块不规律分布,翻动一个色块,其上下左右,都会被翻动,知道全黑全白为止.输出最小次数,达不到则输出“Imp ...
- [POJ1753]Flip Game(开关问题,枚举)
题目链接:http://poj.org/problem?id=1753 和上一个题一样,将初始状态存成01矩阵,就可以用位运算优化了.黑色白色各来一遍 /* ━━━━━┒ギリギリ♂ eye! ┓┏┓┏ ...
随机推荐
- 每天一个linux命令(39):iostat命令
Linux系统中的 iostat 是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视.它的特点是汇报磁盘活动统计情况,同时也会 汇报出CPU使用情况 ...
- git for windows 入门随笔
引言: Git 是当前最流行的集中化的版本控制程序之一(版本控制是一种记录若干文件内容变化,以便将来查阅特定版本修订情况的系统),Git 只关心文件数据的整体是否发生变化,而大多数其他系统则只关心文件 ...
- webstrom 中启用emmet插件的方法
参考页面:https://www.jetbrains.com/help/webstorm/2016.2/enabling-emmet-support.html Basics Native Emmet ...
- poj3107 树形dp
好久没更了.前段时间去ec-final,实力水一波,混了个铜,虽然很弱,但是可以算是对之前一段时间的回报吧. 现在每天忙着复习,逃课太多,啥都不会...不想挂科啊!!Orz... 题意(简化):警察想 ...
- PowerDesigner-制作Word导出模版
定制导出模版 当然这不是我们想要的word,下面看如何做一个自定义模版 1. 在工具栏中选择[Report -->Reports],如下图 点击第二个图标创建一个Template,如下图 2. ...
- Java设计模式-模板方法模式(Template Method)
解释一下模板方法模式,就是指:一个抽象类中,有一个主方法,再定义1...n个方法,可以是抽象的,也可以是实际的方法,定义一个类,继承该抽象类,重写抽象方法,通过调用抽象类,实现对子类的调用,先看个关系 ...
- 学习笔记-动态树Link-Cut-Tree
--少年你有梦想吗? --少年你听说过安利吗? 安利一个集训队讲解:http://wenku.baidu.com/view/75906f160b4e767f5acfcedb 关于动态树问题,有多种方法 ...
- BZOJ-1901 Zju2112 Dynamic Rankings 函数式线段树 套 树状数组+离线处理
1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec Memory Limit: 128 MB Submit: 6058 Solved: 2521 [Su ...
- TCP/IP详解 学习六
从ip层看路由表 选路策略 选路策略:决定把哪些路由放到路由表的规则. Ip执行选路机制,而路由守护程序则提供选路策略. Netstat –rn 打印路由表,如果没有-n命令会搜索配置文件,将网络地 ...
- 快速反射DataTable
public class SetterWrapper<TTarget, TValue> { private Action<TTarget, TValue> _setter; p ...