poj1753 【枚举】
- 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
Sample Input
- bwwb
- bbwb
- bwwb
- bwww
Sample Output
- 4
思路:dfs
实现代码:
- #include<iostream>
- #include<cstdio>
- #include<string>
- #include<cstring>
- #include<cmath>
- #include<algorithm>
- #include<map>
- #include<queue>
- #include<stack>
- #include<set>
- #include<list>
- using namespace std;
- #define ll long long
- #define sd(x) scanf("%d",&x)
- #define sdd(x,y) scanf("%d%d",&x,&y)
- #define sddd(x,y,z) scanf("%d%d%d",&x,&y,&z)
- #define sf(x) scanf("%s",x)
- #define ff(i,x,y) for(int i = x;i <= y;i ++)
- #define fj(i,x,y) for(int i = x;i >= y;i --)
- #define mem(s,x) memset(s,x,sizeof(s));
- #define pr(x) printf("%d",x);
- const int Mod = 1e9+;
- const int inf = 1e9;
- const int Max = 1e5+;
- void exgcd(ll a,ll b,ll& d,ll& x,ll& y){if(!b){d=a;x=;y=;}else{exgcd(b,a%b,d,y,x);y-=x*(a/b);}}
- ll inv(ll a,ll n){ll d, x, y;exgcd(a,n,d,x,y);return (x+n)%n;}
- int gcd(int a,int b) { return (b>)?gcd(b,a%b):a; }
- int lcm(int a, int b) { return a*b/gcd(a, b); }
- //int mod(int x,int y) {return x-x/y*y;}
- char s[];
- int mp[][];
- int ans = inf;
- int check()
- {
- int x = mp[][];
- for(int i = ;i < ;i ++){
- for(int j = ;j < ;j ++){
- if(mp[i][j]!=x)
- return ;
- }
- }
- return ;
- }
- void fun(int x,int y)
- {
- mp[x][y] = !mp[x][y];
- if(x - >= ) mp[x-][y] = !mp[x-][y];
- if(x + <= ) mp[x+][y] = !mp[x+][y];
- if(y - >= ) mp[x][y-] = !mp[x][y-];
- if(y + <= ) mp[x][y+] = !mp[x][y+];
- }
- int dfs(int x,int y,int t)
- {
- if(check()){
- ans = min(ans,t);
- return ;
- }
- if(x>=||y>=)
- return ;
- int fx = (x+)%;
- int fy = y+(x+)/;
- dfs(fx,fy,t);
- //cout<<fx<<" "<<fy<<" "<<t<<endl;
- fun(x,y);
- dfs(fx,fy,t+);
- //cout<<fx<<" "<<fy<<" "<<t+1<<endl;
- fun(x,y);
- return ;
- }
- int main()
- {
- for(int i=;i<;i++){
- cin>>s;
- for(int j=;j<;j++){
- if(s[j]=='b')
- mp[i][j] = ;
- else
- mp[i][j] = ;
- }
- }
- dfs(,,);
- if(ans==inf)
- cout<<"Impossible"<<endl;
- else
- cout<<ans<<endl;
- return ;
- }
poj1753 【枚举】的更多相关文章
- poj1753枚举
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33670 Accepted: 14713 Descr ...
- 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! ┓┏┓┏ ...
随机推荐
- Recurrent Neural Network[CTC]
0. 背景 1. CTC原理 图 CTC结构图 CTC是看似和HMM有些联系,然后也采用DP来进行求解,将CTC结构图中<RNN输出,CTC层>单独拿出来,得到如下形式: 图 用前向-后向 ...
- VS2017上执行VS2013项目错误MSB802之解决方案
进行想把我编写的数字图像处理软件MagicHouse更新到最新的VS2017开发环境下,原来的开发环境是VS2013.但是用VS2017打开项目并编译时,系统报错误MSB802,如下图所示. 其实Vi ...
- JS-JS创建数组的三种方法
隐式创建 var arr=["Audi","BMW","Volvo"]; 直接实例化 var arr=new Array("Aud ...
- centos7 清除系统日志、历史记录(包括history)、登录信息
history: # echo > .bash_history //清除保存的用户操作历史记录 # history -cw //清除所有历史 centos7 清除系统日志.历史记录.登录信息: ...
- 基于Ping和Telnet/NC的监控脚本案例分析
案例一:单纯地对某些ip进行ping监控 [root@test opt]# cat /opt/hosts_ip_list 192.168.10.10 192.168.10.11 192.168.10. ...
- Jenkins新建项目中源码管理Repository URL使用Git报错:Failed to connect to repository : Command "git ls-remote -h......
之前部署了Gitlab+Gerrit+Jenkins持续集成环境,但在Jenkins中新建项目的源码管理"Repository URL"中添加git地址环节出现了问题,信息为&qu ...
- OpenStack构架知识梳理
OpenStack既是一个社区,也是一个项目和一个开源软件,提供开放源码软件,建立公共和私有云,它提供了一个部署云的操作平台或工具集,其宗旨在于:帮助组织运行为虚拟计算或存储服务的云,为公有云.私有云 ...
- 个人阅读作业Week5
一.总结体会 团队项目已经进行了很多周,我们团队从刚开始的基础薄弱到现在的大家都可以运用Android来编写程序,共同完成一个app的开发使用. 刚开始做团队项目之时,我们团队就开了一个会,确定了以后 ...
- Daily Scrumming* 2015.12.22(Day 14)
一.团队scrum meeting照片 二.成员工作总结 姓名 任务ID 迁入记录 江昊 任务1112 无 任务说明 今天没有写前端界面,而是完成了跨域请求的实现以及用户实名认证API 前后端大部分数 ...
- 软件项目第一次Sprint总结
成果评分表: 组名 分数 原因 9-652 6 界面和谐生动,可运行,在目前阶段可时间基本操作 hzsy -2 代码下载,但实现安卓和相机调用 JYJe族 -1 实现安卓界面,完成一项功能,做得少 结 ...