POJ_1753
来源:互联网 发布:汉字域名有什么坏处 编辑:程序博客网 时间:2024/05/21 19:22
Poj 1753 flip game
无需储存每一步的用时,只需储存到每一个状态的最短用时(记忆化搜索)?。
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>#include <algorithm>#include <queue>using namespace std;#define MAXN 17#define SIZE 16#define MAXLEN 1<<17#define MIN(a, b) (a < b? a : b)#define INF 0x7fffffffconst int go[] = { 0xc800, 0xe400, 0x7200, 0x3100, 0x8c80, 0x4e40, 0x2720, 0x1310, 0x08c8, 0x04e4, 0x0272, 0x0131, 0x008c, 0x004e, 0x0027, 0x0013}, len = 16;int time[MAXLEN]; // save the mini time to "NUM";char maps[MAXN];int ans;int abs(int k){ return k >= 0 ? k : -k;}class node{public: int num; bool flags[SIZE]; node() { memset(flags, 0, sizeof(flags)); } node(const int& k):num(k) { memset(flags, 0, sizeof(flags)); } node(const node &k) { this->num = k.num; for(register int i = 0; i < MAXN; ++i) this->flags[i] = k.flags[i]; } void kick(const node &father, const int &k, const int &c) { this->num = (father.num ^ k); for(register int i = 0; i < SIZE; ++i) this->flags[i] = father.flags[i]; this->flags[c] = true; } bool operator == (const node &k) { return this->num == k.num; }};queue<node> que;node start;void bfs(){ node a, b; while(!que.empty()) que.pop(); time[start.num] = 0; que.push(start); while(!que.empty()) { a = que.front(); que.pop(); if(a.num == 0 ||a.num == 0xffff) { return; } for(register int i = 0; i < len; ++i) { if(!a.flags[i]) { b.kick(a, go[i], i); if(time[b.num] < 0 || (time[b.num] > time[a.num] + 1)) //若未到达过或有更短时间到达该状态则入队列 { time[b.num] = time[a.num] + 1; que.push(b); } } } }}void igets(char *s){ char *p = s; for(int i = 0; i < 4; ++i) { for(int j = 0; j < 4; ++j) *p++ = getchar(); getchar(); } *p = 0;}void solve(){ igets(maps); memset(time, -1, sizeof(time)); ans = -1; start.num = 0; for(register int i = 0; i < SIZE; ++i) { start.num <<= 1; start.num += (maps[i] == 'w' ? 1 : 0); } bfs(); if(time[0] < 0) time[0] = INF; if(time[0xffff] < 0) time[0xffff] = INF; ans = MIN(time[0], time[0xffff]); if(ans >= 0 && ans < INF) printf("%d\n", ans); else puts("Impossible");}int main(){ solve(); return 0;}
0 0
- poj_1753
- poj_1753
- POJ_1753
- poj_1753
- poj_1753
- POJ_1753
- poj_1753 递归+枚举
- Flip Game(POJ_1753)
- Flip Game(POJ_1753)
- POJ_1753(位运算)
- Poj_1753 Flip Game(状态压缩,bfs)
- poj_1753 Flip Game(dfs+枚举)/(bfs+位运算)
- cocos2dx[3.2](15)——颜色混合BlendFunc
- 【考研备考】中山大学考研备考手册
- Javascript 严格模式详解 "use strict"
- 更新MBProgressHUD后的基本使用方法
- 微信测试号申请
- POJ_1753
- linux下多线程多串口数据转发程序
- 吐血推荐珍藏的 Chrome 插件
- [software]Pomodairo 使用攻略
- signal hubs 404问题解决方案
- Delphi控件开发浅入深出(一)
- JAVA设计模式之:命令模式
- (二)02模拟应用商店创建应用程序下载界面
- 队列