HDU 3088 WORM [BFS]
来源:互联网 发布:查看店铺数据的插件 编辑:程序博客网 时间:2024/06/02 21:23
Description
一个虫子,有三种颜色,rgb,它可以执行一种变换,两个不同的颜色变成一个相同的颜色,问,能不能变成全是一种颜色
Algorithm
BFS,每次变变变
同时用三进制r = 0, g = 1, b = 2来记录每一次的情况。
Code
#include <cstring>#include <iostream>#include <queue>using namespace std;struct V{ string s; int step, h;};const int maxh = 1e6;bool h[maxh];int main(){ int n; cin >> n; for (int i = 0; i < n; i++) { memset(h, 0, sizeof(h)); string s; cin >> s; queue<V> q; V t; t.s = s; t.step = 0; int ss = 1; // 阶 int hh = 0; // 3进制表示值 for (int j = 0; j < s.size(); j++) { if (s[j] == 'g') hh += ss; if (s[j] == 'b') hh += ss * 2; ss *= 3; } h[hh] = true; q.push(t); int ans = -1; while (!q.empty()) { V now = q.front(); q.pop(); //检验是否同色 bool flag = true; for (int j = 1; j < now.s.size(); j++) if (now.s[j] != now.s[j - 1]) { flag = false; break; } if (flag) { ans = now.step; break; } //变变变 for (int j = 1; j < now.s.size(); j++) if (now.s[j] != now.s[j - 1]) { V t; t.s = now.s; char ch; if (now.s[j] == 'r' && now.s[j - 1] == 'g') ch = 'b'; if (now.s[j] == 'g' && now.s[j - 1] == 'r') ch = 'b'; if (now.s[j] == 'r' && now.s[j - 1] == 'b') ch = 'g'; if (now.s[j] == 'b' && now.s[j - 1] == 'r') ch = 'g'; if (now.s[j] == 'g' && now.s[j - 1] == 'b') ch = 'r'; if (now.s[j] == 'b' && now.s[j - 1] == 'g') ch = 'r'; t.s[j] = ch; t.s[j - 1] = ch; ss = 1; // 阶 hh = 0; // 3进制表示 for (int k = 0; k < t.s.size(); k++) { if (t.s[k] == 'g') hh += ss; if (t.s[k] == 'b') hh += ss * 2; ss *= 3; } if (!h[hh]) { h[hh] = true; t.step = now.step + 1; q.push(t); } } } if (ans == -1) cout << "No solution!" << endl; else cout << ans << endl; }}
0 0
- HDU 3088 WORM [BFS]
- HDU 1049 Climbing Worm
- HDU 2151 Worm
- hdu 1049 jump worm
- hdu-1049 Climbing Worm
- HDU 1049 ( Climbing Worm )
- HDU 1049 Climbing Worm
- HDU 2151 Worm
- HDU 1049 Climbing Worm
- hdu 1049 Climbing Worm
- hdu 2151 Worm
- HDU 2151(Worm)
- HDU 1049 Climbing Worm
- HDU--1049 -- Climbing Worm
- hdu 2151- DP Worm
- hdu-1049-Climbing Worm
- hdu 1049 Climbing Worm
- HDU 2151 Worm DP
- 计算机网络1
- java学习之四——java面向对象的学习
- PRISM概率模型检测器初使用(2)--嵌入式控制系统模型
- 【Java设计模式】之桥接模式
- 关于PHP中构造函数以及子类继承父类构造函数的相关问题
- HDU 3088 WORM [BFS]
- jsp之request对象
- java异常处理
- Java day02 for循环练习 带星图案
- BestCoder Round #75 1002模拟
- 【hdu2899】Strange fuction——三分
- HttpClient设置编码类型
- Codeforces650B Image Preview
- 家用路由器性能指标