POJ3185(简单BFS,主要做测试使用)
没事做水了一道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,主要做测试使用)的更多相关文章
- 如何在springMVC 中对REST服务使用mockmvc 做测试
如何在springMVC 中对REST服务使用mockmvc 做测试 博客分类: java 基础 springMVCmockMVC单元测试 spring 集成测试中对mock 的集成实在是太棒了!但 ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
- 【微信转载】Google是如何做测试的
就 目前的软件公司而言,Google无疑是在开放和创新力方面做得最好的.而如何支撑Google这种快速地扩张的研发能力以及迭代速度,并且产品质量总是 一如以往的能给人们很棒的用户体验?这是一个值得我们 ...
- 腾讯测试工程师:你以为会打LOL就能做测试了?
周日参加完公司团建,回家路上拼到一个IT界的老司机,他和几个朋友组件团队承接开发项目,知道我是做测试的,问了我一个问题: “你们大公司的测试都做什么?” “测试应该不好模仿吧?” 刚开始我也不清楚他的 ...
- php求和为s的两个数字(多复制上面写的代码,有利于检查错误)(由浅入深,先写简单算法,做题的话够用就行)
php求和为s的两个数字(多复制上面写的代码,有利于检查错误)(由浅入深,先写简单算法,做题的话够用就行) 一.总结 1.多复制上面写的代码,有利于检查错误 2.一层循环就解决了,前后两个指针,和大了 ...
- Google是如何做测试的?
Google是如何做测试的?(一.二) 导读:本文译自 James Whittaker 在 Google 测试官方博客发表的文章<How Google TestsSoftware >. 在 ...
- React简单教程-6-单元测试
前言 我想大部分人的前端测试,都是运行项目,直接在浏览器上操作,看看功能正不正常.虽然明明有测试库可以使用,但是因为"要快"的原因,让好好做测试变成了一件影响效率的事. 因为这种无 ...
- LightOJ 1012 简单bfs,水
1.LightOJ 1012 Guilty Prince 简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstr ...
- 小米2S Mk6.0.1 [只能做测试体验,不能使用]
上几张高清图片.. 说明: 此版本只能做测试体验,不能做实际使用. 开发者: laser杨万荣 感谢: 秋叶随风ivan, m1cha 及 MoKee Open Source的各位开发者 下载地址:链 ...
随机推荐
- Linux Autotools
/********************************************************************** * Linux Autotools * 说明: * 我们 ...
- python练习程序(c100经典例16)
题目: 输入两个正整数m和n,求其最大公约数和最小公倍数. def foo(a,b): if a<b: (a,b)=(b,a) aa=a; bb=b; while b!=0: tmp=a%b; ...
- nodejs简单层级结构配置文件
在NodeJS中使用配置文件,有几种比较不错的方案:第一种:文件格式使用json是毋容置疑的好方案.格式标准,易于理解,文件内容读取到内存之后,使用JSON的标准分析函数即可得到配置项.第二种:将配置 ...
- Python定时调度--多任务同一时间开始跑 scheduler.enterabs
Event Priorities If more than one event is scheduled for the same time their priority values are use ...
- C# 阳历转农历
你妹的sb 原文 C#(ASP.NET)公历转农历的简单方法 Dot Net 平台,对全球化的支持做的非常好,不得不称赞一个 通常,将公历转为农历,是个非常烦的事情,需要整理闰年.闰月等的对照表. 在 ...
- Spring3.0.6定时任务
项目使用的Spring版本比较旧是3.0.6版本,由于需要进行定时任务,就决定使用Spring自带的scheduled task. 在网上找了很多文章,也查看了Spring3.0.6的官方文档,按照网 ...
- FreeMarker笔记 第四章 其它
4.1 自定义指令 4.1.1 简介 自定义指令可以使用macro指令来定义.Java程序员若不想在模板中实现定义指令,而是在Java语言中实现指令的定义,这时可以使用freemarker.templ ...
- A.xml
pre{ line-height:1; color:#1e1e1e; background-color:#f0f0f0; font-size:16px;}.sysFunc{color:#627cf6; ...
- Prototype入门
官网地址:http://prototypejs.org/ Prototype降低了客户端web编程的复杂性.为了解决现实存在的一些问题,Prototype对浏览器的脚本环境做了一些扩展,对原先笨拙的A ...
- 【Unity入门】编辑器常用视图介绍
版权声明:本文为博主原创文章,转载请注明出处. 打开Unity编辑器的主窗口,在窗口的右上角可以看到有个“Layout”按钮.这是用来对Unity编辑器主窗口上面的各个窗口面板进行布局的.通常情况下我 ...