没事做水了一道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. $^,$@,$?,$<,$(@D),$(@F) of makefile

    makefile下$(wildcard $^),$^,$@,$?,$<,$(@D),$(@F)代表的不同含义 $(filter-out $(PHONY) $(wildcard $^),$^)常用 ...

  2. BZOJ 1803 Query on a tree III

    树上主席树. 我靠这是第k小吧..... #include<iostream> #include<cstdio> #include<cstring> #includ ...

  3. xunsearch迅搜体验

    安装与启动 http://www.xunsearch.com/doc/php/guide/start.installation 编写配置文件 http://www.xunsearch.com/doc/ ...

  4. swun 1184

    解题思路:这题其实还是有点麻烦的,思路要清晰,关键是要找出中间的那个点. 已知不共线的三点:A(x1,y1),B(x2,y2),C(x3,y3),平行四边形ABCD的点D的坐标由对角线AC与BD互相平 ...

  5. hadoop——配置eclipse下的map-reduce运行环境 1

    1.通过修改实例模板程序来实现自己的map-reduce: 为了让示例程序run起来: 1)安装eclipse 2)安装map-reduce的eclipse插件 eclipse的map-reduce插 ...

  6. WebView增加一个水平Progress,位置、长相随意

    实际效果可以参看微信的web页面进度条 本质就是通过addView及对WebView 页面进度进行监听 先看看这个自定义的DrawableId,我们参照系统默认实现的方法写一个自己的   <la ...

  7. Eclipse将android项目打包jar文件

    Eclipse+android打包jar文件 蔡建良 2016-3-12 以Android-SlideExpandableListView开源框架为例,将源码Library打包成jar文件并包含R.c ...

  8. JS 中document.URL 和 window.location.href 的区别

    实际上,document 和 window 这两个对象的区别已经包含了这个问题的答案. document 表示的是一个文档对象,window 表示一个窗口对象. 一个窗口下面可以有很多的documen ...

  9. centos mysq table is read only

    1.进入mysql数据库目录,使用命令"chown -R mysql <数据库文件夹名称>" 2. "chgrp -R mysql <数据库文件夹名称& ...

  10. N人报数第M人出列游戏问题(约瑟夫问题)

    这是一道华为的机试题,后来才知道也叫约瑟夫问题,题目是这样的:有n个人围成一圈,玩一个游戏,规则为将该n个人编号为1,2,......n, 从编号为1的人开始依次循环报数,报道第m的时候将第m个人从队 ...