Vjudge攻略——POJ1753
来源:互联网 发布:光大证券金阳光mac版 编辑:程序博客网 时间:2024/06/13 01:11
POJ1753
POJ1753是一个点灯游戏,注意每个点最多只能被点一次(多点则还原),然后难点就在如何每次从16个点中取出N个点(递归实现)。
#include<iostream>using namespace std;class map{ public: bool s[4][4]; map(){} void turn(int x, int y) { if(s[x][y])s[x][y] = false; else s[x][y] = true; } void flip(int x, int y) { if(x - 1 >= 0)turn(x - 1, y); if(x + 1 <= 3)turn(x + 1, y); if(y - 1 >= 0)turn(x, y - 1); if(y + 1 <= 3)turn(x, y + 1); turn(x, y); } bool finish() { int sum = 0; for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) if(s[i][j])++sum; if(sum == 0 || sum == 16)return true; return false; } bool dfs(int x, int y, map m) { if(y == 0) { if(m.finish())return true; return false; } map ma; for(int i = x; i < 17 - y; i++) { ma = m; ma.flip(i/4, i%4); if(dfs(i + 1, y - 1, ma))return true; } return false; } int solve() { for(int i = 0; i <= 16; i++) if(dfs(0, i, *this))return i; return -1; } friend istream& operator>>(istream& is, map& m) { char s[4][4]; for(int i = 0; i < 4; i++) cin >> s[i]; for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) { if(s[i][j] == 'b')m.s[i][j] = true; else m.s[i][j] = false; } return is; }};int main(){ map m; cin >> m; int result = m.solve(); if(result == -1)cout << "Impossible" << endl; else cout << result << endl; return 0;}
阅读全文
0 0
- Vjudge攻略——POJ1753
- Vjudge攻略——POJ2965
- Vjudge攻略——ACdream-1099
- 枚举——POJ1753
- poj1753——Flip Game
- POJ1753——Flip Game
- poj1753——Flip Game
- POJ1753——Flip Game
- POJ1753——Robot Motion
- Vjudge
- 【dfs】poj1753——Flip Game
- poj1753 Flip Game —— bfs
- https://vjudge.net/contest/161167#problem/F——dp
- POJ1753
- poj1753
- poj1753
- poj1753
- POJ1753
- Docker阿里云镜像加速
- 04-树5 Root of AVL Tree (25分)
- 循环:素数和
- msql数据库分区
- Zookeeper教程(二):ZooKeeper与Dubbo结合以及原理讲解
- Vjudge攻略——POJ1753
- IntelliJ IDEA 2017激活(Mac版本)
- 利用MFC绘制Excel数据的波形
- 循环:念整数
- 【TED】The psychology of self-motivation--Scott Geller
- 模拟实现printf
- 装饰者模式
- 【Caffe的C++接口使用说明(一)】caffe_windows下的第一个测试程序学习教程
- #300. 【CTSC2017】吉夫特