没事做水了一道POJ的简单BFS的题目

这道题的数据范围是20,所以状态总数就是(1<<20)

第一次提交使用STL的queue,并且是在队首判断是否达到终点,达到终点就退出,超时:(其实这里我是很不明白的,,TM状态总数就只有1e6怎么也不应该超时的,,,,只能说STL的queue的常数实在是太大,完全没法弄。。。)

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <string.h>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1e9
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; }
template<class T> void SWAP(T& a, T& b) { T x = a; a = b; b = x; } //typedef __int64 LL;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
//const LL MOD = 1000000007; int step[<<];
bool vis[<<]; int BFS(int s)
{
vis[s] = ;
queue<int>q;
q.push(s);
step[s] = ;
while(!q.empty())
{
int u = q.front(); q.pop();
if(u == ) return step[u];
for(int i=;i<;i++)
{
int r = u;
r ^= (<<i-) | (<<i) | (<<i+);
if(!vis[r])
{
vis[r] = ;
step[r] = step[u] + ;
q.push(r);
}
}
int r = u ^ (<<) ^ (<<);
if(!vis[r]) { vis[r] = ; step[r] = step[u] + ; q.push(r); }
r = u ^ (<<) ^ (<<);
if(!vis[r]) { vis[r] = ; step[r] = step[u] + ; q.push(r); }
}
return -;
} int main()
{
//FOPENIN("in.txt");
int st = , x;
for(int i=;i<;i++)
{
scanf("%d", &x);
st |= (x<<i);
}
printf("%d\n", BFS(st));
return ;
}

TLE后马上把判断放到队尾(就是说在一个状态进队列前先判断是不是终点状态,是的话就退出),跑了875ms,勉强过了,,(这里我就是乱改的了,代码没任何观赏性)

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <string.h>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1e9
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; }
template<class T> void SWAP(T& a, T& b) { T x = a; a = b; b = x; } //typedef __int64 LL;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
//const LL MOD = 1000000007; int step[<<];
bool vis[<<]; int BFS(int s)
{
vis[s] = ;
queue<int>q;
q.push(s);
step[s] = ;
while(!q.empty())
{
int u = q.front(); q.pop();
if(u == ) return step[u];
for(int i=;i<;i++)
{
int r = u;
r ^= (<<i-) | (<<i) | (<<i+);
if(!vis[r])
{
vis[r] = ;
step[r] = step[u] + ;
if(r==) return step[r];
q.push(r);
}
}
int r = u ^ (<<) ^ (<<);
if(!vis[r]) { vis[r] = ; step[r] = step[u] + ; q.push(r); if(r==) return step[r];}
r = u ^ (<<) ^ (<<);
if(!vis[r]) { vis[r] = ; step[r] = step[u] + ; q.push(r); if(r==) return step[r];}
}
return -;
} int main()
{
//FOPENIN("in.txt");
int st = , x;
for(int i=;i<;i++)
{
scanf("%d", &x);
st |= (x<<i);
}
printf("%d\n", BFS(st));
return ;
}

然后突然想起之前写过一个静态队列的模板,是用循环队列写的,想着正好去测试下,就改了队列的定义,其他使用完全一致,没任何修改,结果跑了250ms快了好多了啊有木有。。

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <string.h>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1e9
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; }
template<class T> void SWAP(T& a, T& b) { T x = a; a = b; b = x; } //typedef __int64 LL;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
//const LL MOD = 1000000007; //MyQueue<Type>q;
//定义了一个固定长度的队列, 不能动态增长
//构造时不传参数,队列大小为1e5,传入参数时为自定义大小
//如果队列不为空,front返回队首元素,
//如果队列为空,pop无效,front返回NULL
//clear将队列清空, 供多次使用
//如果push时产生冲突,即队列已满, 将加入失败
template <class T>
class MyQueue
{
private:
T* que;
int si, fr, re;
void setValue(int _size) {
fr = ; re = ;
si = _size;
que = (T*)malloc(si * sizeof(T));
}
public:
MyQueue() {
this->setValue();
}
MyQueue(int _size) {
this->setValue(_size);
}
T front() {
if(fr != re)
return que[fr];
return NULL;
}
void pop() {
if(fr != re)
fr = (fr + ) % si;
}
void push(T e) {
if((re + ) % si == fr) return ;
que[re] = e;
re = (re + ) % si;
}
bool empty() {
if(fr == re) return ;
return ;
}
void clear() {
fr = ;
re = ;
}
}; int step[<<];
bool vis[<<]; int BFS(int s)
{
vis[s] = ;
MyQueue<int>q(<<);//定义队列的大小为(1<<21),其他无任何修改
q.push(s);
step[s] = ;
while(!q.empty())
{
int u = q.front(); q.pop();
if(u == ) return step[u];
for(int i=;i<;i++)
{
int r = u;
r ^= (<<i-) | (<<i) | (<<i+);
if(!vis[r])
{
vis[r] = ;
step[r] = step[u] + ;
q.push(r);
}
}
int r = u ^ (<<) ^ (<<);
if(!vis[r]) { vis[r] = ; step[r] = step[u] + ; q.push(r); }
r = u ^ (<<) ^ (<<);
if(!vis[r]) { vis[r] = ; step[r] = step[u] + ; q.push(r); }
}
return -;
} int main()
{
//FOPENIN("in.txt");
int st = , x;
for(int i=;i<;i++)
{
scanf("%d", &x);
st |= (x<<i);
}
printf("%d\n", BFS(st));
return ;
}

最后试着把终点判断放在队头,依然360ms就跑出来了啊,,,我就哭了。。。

POJ3185(简单BFS,主要做测试使用)的更多相关文章

  1. 如何在springMVC 中对REST服务使用mockmvc 做测试

    如何在springMVC 中对REST服务使用mockmvc 做测试 博客分类: java 基础 springMVCmockMVC单元测试  spring 集成测试中对mock 的集成实在是太棒了!但 ...

  2. 【POJ 3669 Meteor Shower】简单BFS

    流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...

  3. 【微信转载】Google是如何做测试的

    就 目前的软件公司而言,Google无疑是在开放和创新力方面做得最好的.而如何支撑Google这种快速地扩张的研发能力以及迭代速度,并且产品质量总是 一如以往的能给人们很棒的用户体验?这是一个值得我们 ...

  4. 腾讯测试工程师:你以为会打LOL就能做测试了?

    周日参加完公司团建,回家路上拼到一个IT界的老司机,他和几个朋友组件团队承接开发项目,知道我是做测试的,问了我一个问题: “你们大公司的测试都做什么?” “测试应该不好模仿吧?” 刚开始我也不清楚他的 ...

  5. php求和为s的两个数字(多复制上面写的代码,有利于检查错误)(由浅入深,先写简单算法,做题的话够用就行)

    php求和为s的两个数字(多复制上面写的代码,有利于检查错误)(由浅入深,先写简单算法,做题的话够用就行) 一.总结 1.多复制上面写的代码,有利于检查错误 2.一层循环就解决了,前后两个指针,和大了 ...

  6. Google是如何做测试的?

    Google是如何做测试的?(一.二) 导读:本文译自 James Whittaker 在 Google 测试官方博客发表的文章<How Google TestsSoftware >. 在 ...

  7. React简单教程-6-单元测试

    前言 我想大部分人的前端测试,都是运行项目,直接在浏览器上操作,看看功能正不正常.虽然明明有测试库可以使用,但是因为"要快"的原因,让好好做测试变成了一件影响效率的事. 因为这种无 ...

  8. LightOJ 1012 简单bfs,水

    1.LightOJ 1012  Guilty Prince  简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstr ...

  9. 小米2S Mk6.0.1 [只能做测试体验,不能使用]

    上几张高清图片.. 说明: 此版本只能做测试体验,不能做实际使用. 开发者: laser杨万荣 感谢: 秋叶随风ivan, m1cha 及 MoKee Open Source的各位开发者 下载地址:链 ...

随机推荐

  1. POJ 1837 Balance 【DP】

    题意:给出一个天平,给出c个钩子,及c个钩子的位置pos[i],给出g个砝码,g个砝码的质量w[i],问当挂上所有的砝码的时候,使得天平平衡的方案数, 用dp[i][j]表示挂了前i个砝码时,平衡点为 ...

  2. 自己实现内存操作函数memset(),memcmp(),memcpy(),memmove()

    1.memset()内存设置函数(初始化) void *my_memset(void* dest, int c, size_t count) { assert(dest != NULL); char  ...

  3. 【同行说技术】Android图片处理技术资料汇总(一)

    对于Android开发的童鞋们来说,图片处理时或多或少都会遇到令人头疼和不满意的问题,今天小编收集了5篇Android图片处理的干货文章,一起来看看吧! 一.Android 高清加载巨图方案 拒绝压缩 ...

  4. A*寻路初探 GameDev.net 转载

    A*寻路初探 GameDev.net 译者序:很久以前就知道了A*算法,但是从未认真读过相关的文章,也没有看过代码,只是脑子里有个模糊的概念.这次决定从头开始,研究一下这个被人推崇备至的简单方法,作为 ...

  5. HDU 5327 Olympiad (水题)

    题意:beautiful数字定义为该数字中的十进制形式每一位都不同,给一个区间[L,R],求该区间中有多少个beautiful数字. 思路:数字不大,直接暴力预处理,再统计区间[1,i]有多少个,用c ...

  6. HDU 5335 Walk Out (BFS,技巧)

    题意:有一个n*m的矩阵,每个格子中有一个数字,或为0,或为1.有个人要从(1,1)到达(n,m),要求所走过的格子中的数字按先后顺序串起来后,用二进制的判断大小方法,让这个数字最小.前缀0不需要输出 ...

  7. 20160202.CCPP体系详解(0012天)

    内容概要:C语言控制语句题库.doc 第三章 控制语句 一.选择题 1. 以下语句中无限循环语句是[B]. A)for(;2&5;); B)while(1,2,3); -> while( ...

  8. 操作系统——CPU、计算机的构成

    CPU主要由什么构成? CPU包括运算逻辑部件.寄存器部件和控制部件等. 逻辑部件:可以执行点或浮点算术远算操作.移位操作以及逻辑操作,也可以执行地址运算和转换. 寄存器部件:存储程序.数据和各种信号 ...

  9. Mysql避免全表扫描sql查询优化 .

    对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引: .尝试下面的技巧以避免优化器错选了表扫描: ·   使用ANALYZE TABLE tbl_n ...

  10. C# 使用NPlot绘图技巧

    原文 C# 使用NPlot绘图技巧 图表控件一直是很难找的,特别是免费又强大的.NPlot是一款非常难得的.Net平台下的图表控件,能做各种曲线图,柱状图,饼图,散点图,股票图等,而且它免费又开源,使 ...