hdu 5012 bfs --- 慎用STL 比方MAP判重
http://acm.hdu.edu.cn/showproblem.php?pid=5012
发现一个问题 假设Sting s = '1'+'2'+'3';
s!="123"!!!!!! 而是有乱码
先贴一份自己的TLE 代码,
超时应该是由于:
1、cin
2、map判重 map find太花时间
3、string花时间
4、事实上不用两个都旋转,仅仅要旋转一个即可,这样能够省下非常多时间 包含少用了make_pair pair的判重等等....哎 二笔了 太暴力了
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <set>
#include <queue>
#include <map> using namespace std; #define IN(s) freopen(s,"r",stdin)
#define CL(a,b) memset(a,b,sizeof(a)) map< pair<string ,string>, int>mp; string left(string x)
{
string tmp;
tmp+=x[3];
tmp+=x[2];
tmp+=x[0];
tmp+=x[1];
tmp+=x[4];
tmp+=x[5];
//cout << "st" << tmp << endl;
//x=tmp;
return tmp;
} string right(string x)
{
string tmp;
//tmp=x[2]+x[3]+x[1]+x[0]+x[4]+x[5];
tmp+=x[2];
tmp+=x[3];
tmp+=x[1];
tmp+=x[0];
tmp+=x[4];
tmp+=x[5];
//x=tmp;
return tmp;
}
string front(string x)
{
string tmp;
//tmp= x[5]+x[4]+x[2]+x[3]+x[0]+x[1];
tmp+= x[5];
tmp+=x[4];
tmp+=x[2];
tmp+=x[3];
tmp+=x[0];
tmp+=x[1];
//x=tmp;
return tmp;
} string back(string x)
{
string tmp;
//tmp=x[4]+x[5]+x[2]+x[3]+x[1]+x[0];
tmp+=x[4];
tmp+=x[5];
tmp+=x[2];
tmp+=x[3];
tmp+=x[1];
tmp+=x[0];
//x=tmp;
return tmp;
} queue< pair<string,string> >q;
string a,b;
int solve()
{
while(!q.empty())q.pop();
mp[ make_pair(a,b) ]=0;
q.push( make_pair(a,b) );
int flag=0;
while(!q.empty())
{
pair <string,string> tp;
tp.first=q.front().first;
tp.second=q.front().second;
if(tp.first == tp.second){flag=1; return mp[tp];}
string tmp=left(tp.first);
if(mp.find(make_pair(tmp,tp.second)) == mp.end()) {mp[make_pair(tmp,tp.second)]=mp[tp]+1; q.push(make_pair(tmp,tp.second)); }
tmp=left(tp.second);if(mp.find(make_pair(tp.first,tmp))== mp.end()) { mp[make_pair(tp.first,tmp)]=mp[tp]+1;q.push(make_pair(tp.first,tmp)); }
tmp=right(tp.first);if(mp.find(make_pair(tmp,tp.second)) == mp.end()) {mp[make_pair(tmp,tp.second)]=mp[tp]+1; q.push(make_pair(tmp,tp.second)); }
tmp=right(tp.second);if(mp.find(make_pair(tp.first,tmp)) == mp.end()){ mp[make_pair(tp.first,tmp)]=mp[tp]+1;q.push(make_pair(tp.first,tmp)); }
tmp=front(tp.first);if(mp.find(make_pair(tmp,tp.second)) == mp.end()) {mp[make_pair(tmp,tp.second)]=mp[tp]+1; q.push(make_pair(tmp,tp.second)); }
tmp=front(tp.second);if(mp.find(make_pair(tp.first,tmp)) == mp.end()) { mp[make_pair(tp.first,tmp)]=mp[tp]+1;q.push(make_pair(tp.first,tmp)); }
tmp=back(tp.first);if(mp.find(make_pair(tmp,tp.second)) == mp.end()) {mp[make_pair(tmp,tp.second)]=mp[tp]+1; q.push(make_pair(tmp,tp.second)); }
tmp=back(tp.second);if(mp.find(make_pair(tp.first,tmp)) == mp.end()) { mp[make_pair(tp.first,tmp)]=mp[tp]+1;q.push(make_pair(tp.first,tmp)); }
q.pop();
}
if(!flag)
return -1;
} int main()
{
//IN("hdu5012.txt");
while(cin>>a)
{
b="";//
string tmp;
for(int i=0;i<5;i++)
{
cin>>tmp;
a+=tmp;
}
for(int i=0;i<6;i++)
{
cin>>tmp;
b+=tmp;
}
printf("%d\n",solve());
}
return 0;
}
看了别人的代码 由于这道题的技术含量不怎么高 懒得再写了。贴别人的把
用数组取代状态,至于判重 由于仅仅有六位数,所以化为整数然后判重即可了 代码来自http://blog.csdn.net/u011345461/article/details/39275009
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm> using namespace std; const int MAXN = 6; struct node{
node() {
memset(arr, 0, sizeof(arr));
d = 0;
}
int arr[MAXN], d;
}s, e; int vis[MAXN * 200000]; int change(node a) {
int num = 0;
for (int i = 0; i < MAXN; i++) {
num = num * 10 + a.arr[i];
}
return num;
} bool judge(node a, node b) {
for (int i = 0; i < MAXN; i++)
if (a.arr[i] != b.arr[i])
return false;
return true;
} node turn(node a, int i) {
node c;
if (i == 1) {
c.arr[0] = a.arr[3];
c.arr[1] = a.arr[2];
c.arr[2] = a.arr[0];
c.arr[3] = a.arr[1];
c.arr[4] = a.arr[4];
c.arr[5] = a.arr[5];
}
if (i == 2) {
c.arr[0] = a.arr[2];
c.arr[1] = a.arr[3];
c.arr[2] = a.arr[1];
c.arr[3] = a.arr[0];
c.arr[4] = a.arr[4];
c.arr[5] = a.arr[5];
}
if (i == 3) {
c.arr[0] = a.arr[5];
c.arr[1] = a.arr[4];
c.arr[2] = a.arr[2];
c.arr[3] = a.arr[3];
c.arr[4] = a.arr[0];
c.arr[5] = a.arr[1];
}
if (i == 4) {
c.arr[0] = a.arr[4];
c.arr[1] = a.arr[5];
c.arr[2] = a.arr[2];
c.arr[3] = a.arr[3];
c.arr[4] = a.arr[1];
c.arr[5] = a.arr[0];
}
return c;
} int bfs() {
memset(vis, 0, sizeof(vis));
queue<node> q;
q.push(s);
node tmp;
vis[change(s)] = 1;
while (!q.empty()) {
tmp = q.front();
q.pop();
if (judge(tmp, e)) {
return tmp.d;
}
for (int i = 1; i <= 4; i++) {
node c;
c = turn(tmp, i);
if (!vis[change(c)]) {
c.d = tmp.d + 1;
vis[change(c)] = 1;
q.push(c);
}
}
}
return -1;
} int main() {
while (scanf("%d", &s.arr[0]) != EOF) {
for (int i = 1; i < MAXN; i++)
scanf("%d", &s.arr[i]);
for (int i = 0; i < MAXN; i++)
scanf("%d", &e.arr[i]); printf("%d\n", bfs());
}
return 0;
}
hdu 5012 bfs --- 慎用STL 比方MAP判重的更多相关文章
- POJ3087 Shuffle'm Up —— 打表找规律 / map判重
题目链接:http://poj.org/problem?id=3087 Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- 逆向bfs搜索打表+康拓判重
HDU 1043八数码问题 八数码,就是1~8加上一个空格的九宫格,这道题以及这个游戏的目标就是把九宫格还原到从左到右从上到下是1~8然后最后是空格. 没了解康托展开之前,这道题怎么想都觉得很棘手,直 ...
- 八数码问题+路径寻找问题+bfs(隐式图的判重操作)
Δ路径寻找问题可以归结为隐式图的遍历,它的任务是找到一条凑够初始状态到终止问题的最优路径, 而不是像回溯法那样找到一个符合某些要求的解. 八数码问题就是路径查找问题背景下的经典训练题目. 程序框架 p ...
- hdu 1430 (BFS 康托展开 或 map )
第一眼看到这题就直接BFS爆搜,第一发爆了内存,傻逼了忘标记了,然后就改,咋标记呢. 然后想到用map函数,就8!个不同的排列,换成字符串用map标记.然后又交一发果断超时,伤心,最恨超时,还不如来个 ...
- HDU 4022 Bombing(stl,map,multiset,iterater遍历)
题目 参考了 1 2 #define _CRT_SECURE_NO_WARNINGS //用的是STL中的map 和 multiset 来做的,代码写起来比较简洁,也比较好容易理解. ...
- hdu 4821 字符串hash+map判重 String (长春市赛区I题)
http://acm.hdu.edu.cn/showproblem.php?pid=4821 昨晚卡了非常久,開始TLE,然后优化了之后,由于几个地方变量写混.一直狂WA.搞得我昨晚都失眠了,,. 这 ...
- hdu 5199 Gunner(STL之map,水)
Problem Description Long long ago, there is a gunner whose name is Jack. He likes to go hunting very ...
- hdu 5012 bfs 康托展开
Dice Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submi ...
- (通俗易懂小白入门)字符串Hash+map判重——暴力且优雅
字符串Hash 今天我们要讲解的是用于处理字符串匹配查重的一个算法,当我们处理一些问题如给出10000个字符串输出其中不同的个数,或者给一个长度100000的字符串,找出其中相同的字符串有多少个(这样 ...
随机推荐
- poj 3311 floyd+dfs或状态压缩dp 两种方法
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6436 Accepted: 3470 ...
- trickle charging current is 0A ?
Recently, I test trickle charging current of the smart phone. It's 0A. ?????????????????????? yes, i ...
- css之其它技巧和经验列表
其它技巧和经验列表(*以下实例默认运行环境都为Standard mode): 如何让层在falsh上显示? 方法: ``` 设置flash的wmode值为transparent或opaque ``` ...
- git的使用03
之前我们写的都是将代码存在本地,我们还可以将代码github官网上,放在github的服务器上去托管
- BestCoder 2nd Anniversary的前两题
Oracle Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Su ...
- AC日记——开关灯 codevs 1690
开关灯 思路: 线段树: bool懒标记维护: 更新区间时是区间总值减去当前值: 来,上代码: #include <cstdio> #include <cstring> #in ...
- Codeforces Round #445 B. Vlad and Cafes【时间轴】
B. Vlad and Cafes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 由"软件是干什么的"引发的思考
自工作以来,都只在进行模块的开发,很少站在整个项目的角度思考过.甚至,自己开发的软件,自己都没有去用过,包括开发的一些APP,都没有下载来认真体验过.思考过.却对自己手机上那些用过的A ...
- hiho一下第128周 后缀自动机二·重复旋律5
#1445 : 后缀自动机二·重复旋律5 时间限制:10000ms 单点时限:2000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数 ...
- 信息批量提取工具bulk-extractor
信息批量提取工具bulk-extractor 在数字取证中,通常需要面对海量的数据,如几百GB甚至TB级别的数据.从这些海量数据中,提取有价值的数据是一个漫长.枯燥.繁琐的过程.Kali Linu ...