POJ3185(简单BFS,主要做测试使用)
沒事做水了一道POJ的簡單BFS的題目
這道題的數(shù)據(jù)范圍是20,所以狀態(tài)總數(shù)就是(1<<20)
第一次提交使用STL的queue,并且是在隊(duì)首判斷是否達(dá)到終點(diǎn),達(dá)到終點(diǎn)就退出,超時(shí):(其實(shí)這里我是很不明白的,,TM狀態(tài)總數(shù)就只有1e6怎么也不應(yīng)該超時(shí)的,,,,只能說STL的queue的常數(shù)實(shí)在是太大,完全沒法弄。。。)
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype> 10 #include <cstring> 11 #include <string.h> 12 #include <cstdlib> 13 #include <iostream> 14 #include <algorithm> 15 using namespace std; 16 #define INF 1e9 17 #define inf (-((LL)1<<40)) 18 #define lson k<<1, L, mid 19 #define rson k<<1|1, mid+1, R 20 #define mem0(a) memset(a,0,sizeof(a)) 21 #define mem1(a) memset(a,-1,sizeof(a)) 22 #define mem(a, b) memset(a, b, sizeof(a)) 23 #define FOPENIN(IN) freopen(IN, "r", stdin) 24 #define FOPENOUT(OUT) freopen(OUT, "w", stdout) 25 template<class T> T CMP_MIN(T a, T b) { return a < b; } 26 template<class T> T CMP_MAX(T a, T b) { return a > b; } 27 template<class T> T MAX(T a, T b) { return a > b ? a : b; } 28 template<class T> T MIN(T a, T b) { return a < b ? a : b; } 29 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; } 30 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } 31 template<class T> void SWAP(T& a, T& b) { T x = a; a = b; b = x; } 32 33 //typedef __int64 LL; 34 typedef long long LL; 35 const int MAXN = 200010; 36 const int MAXM = 100005; 37 const double eps = 1e-10; 38 //const LL MOD = 1000000007; 39 40 int step[1<<21]; 41 bool vis[1<<21]; 42 43 int BFS(int s) 44 { 45 vis[s] = 1; 46 queue<int>q; 47 q.push(s); 48 step[s] = 0; 49 while(!q.empty()) 50 { 51 int u = q.front(); q.pop(); 52 if(u == 0) return step[u]; 53 for(int i=1;i<19;i++) 54 { 55 int r = u; 56 r ^= (1<<i-1) | (1<<i) | (1<<i+1); 57 if(!vis[r]) 58 { 59 vis[r] = 1; 60 step[r] = step[u] + 1; 61 q.push(r); 62 } 63 } 64 int r = u ^ (1<<0) ^ (1<<1); 65 if(!vis[r]) { vis[r] = 1; step[r] = step[u] + 1; q.push(r); } 66 r = u ^ (1<<18) ^ (1<<19); 67 if(!vis[r]) { vis[r] = 1; step[r] = step[u] + 1; q.push(r); } 68 } 69 return -1; 70 } 71 72 int main() 73 { 74 //FOPENIN("in.txt"); 75 int st = 0, x; 76 for(int i=0;i<20;i++) 77 { 78 scanf("%d", &x); 79 st |= (x<<i); 80 } 81 printf("%d\n", BFS(st)); 82 return 0; 83 } View Code?
TLE后馬上把判斷放到隊(duì)尾(就是說在一個(gè)狀態(tài)進(jìn)隊(duì)列前先判斷是不是終點(diǎn)狀態(tài),是的話就退出),跑了875ms,勉強(qiáng)過了,,(這里我就是亂改的了,代碼沒任何觀賞性)
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype> 10 #include <cstring> 11 #include <string.h> 12 #include <cstdlib> 13 #include <iostream> 14 #include <algorithm> 15 using namespace std; 16 #define INF 1e9 17 #define inf (-((LL)1<<40)) 18 #define lson k<<1, L, mid 19 #define rson k<<1|1, mid+1, R 20 #define mem0(a) memset(a,0,sizeof(a)) 21 #define mem1(a) memset(a,-1,sizeof(a)) 22 #define mem(a, b) memset(a, b, sizeof(a)) 23 #define FOPENIN(IN) freopen(IN, "r", stdin) 24 #define FOPENOUT(OUT) freopen(OUT, "w", stdout) 25 template<class T> T CMP_MIN(T a, T b) { return a < b; } 26 template<class T> T CMP_MAX(T a, T b) { return a > b; } 27 template<class T> T MAX(T a, T b) { return a > b ? a : b; } 28 template<class T> T MIN(T a, T b) { return a < b ? a : b; } 29 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; } 30 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } 31 template<class T> void SWAP(T& a, T& b) { T x = a; a = b; b = x; } 32 33 //typedef __int64 LL; 34 typedef long long LL; 35 const int MAXN = 200010; 36 const int MAXM = 100005; 37 const double eps = 1e-10; 38 //const LL MOD = 1000000007; 39 40 int step[1<<21]; 41 bool vis[1<<21]; 42 43 int BFS(int s) 44 { 45 vis[s] = 1; 46 queue<int>q; 47 q.push(s); 48 step[s] = 0; 49 while(!q.empty()) 50 { 51 int u = q.front(); q.pop(); 52 if(u == 0) return step[u]; 53 for(int i=1;i<19;i++) 54 { 55 int r = u; 56 r ^= (1<<i-1) | (1<<i) | (1<<i+1); 57 if(!vis[r]) 58 { 59 vis[r] = 1; 60 step[r] = step[u] + 1; 61 if(r==0) return step[r]; 62 q.push(r); 63 } 64 } 65 int r = u ^ (1<<0) ^ (1<<1); 66 if(!vis[r]) { vis[r] = 1; step[r] = step[u] + 1; q.push(r); if(r==0) return step[r];} 67 r = u ^ (1<<18) ^ (1<<19); 68 if(!vis[r]) { vis[r] = 1; step[r] = step[u] + 1; q.push(r); if(r==0) return step[r];} 69 } 70 return -1; 71 } 72 73 int main() 74 { 75 //FOPENIN("in.txt"); 76 int st = 0, x; 77 for(int i=0;i<20;i++) 78 { 79 scanf("%d", &x); 80 st |= (x<<i); 81 } 82 printf("%d\n", BFS(st)); 83 return 0; 84 } View Code?
然后突然想起之前寫過一個(gè)靜態(tài)隊(duì)列的模板,是用循環(huán)隊(duì)列寫的,想著正好去測試下,就改了隊(duì)列的定義,其他使用完全一致,沒任何修改,結(jié)果跑了250ms快了好多了啊有木有。。
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype> 10 #include <cstring> 11 #include <string.h> 12 #include <cstdlib> 13 #include <iostream> 14 #include <algorithm> 15 using namespace std; 16 #define INF 1e9 17 #define inf (-((LL)1<<40)) 18 #define lson k<<1, L, mid 19 #define rson k<<1|1, mid+1, R 20 #define mem0(a) memset(a,0,sizeof(a)) 21 #define mem1(a) memset(a,-1,sizeof(a)) 22 #define mem(a, b) memset(a, b, sizeof(a)) 23 #define FOPENIN(IN) freopen(IN, "r", stdin) 24 #define FOPENOUT(OUT) freopen(OUT, "w", stdout) 25 template<class T> T CMP_MIN(T a, T b) { return a < b; } 26 template<class T> T CMP_MAX(T a, T b) { return a > b; } 27 template<class T> T MAX(T a, T b) { return a > b ? a : b; } 28 template<class T> T MIN(T a, T b) { return a < b ? a : b; } 29 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; } 30 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } 31 template<class T> void SWAP(T& a, T& b) { T x = a; a = b; b = x; } 32 33 //typedef __int64 LL; 34 typedef long long LL; 35 const int MAXN = 200010; 36 const int MAXM = 100005; 37 const double eps = 1e-10; 38 //const LL MOD = 1000000007; 39 40 41 //MyQueue<Type>q; 42 //定義了一個(gè)固定長度的隊(duì)列, 不能動(dòng)態(tài)增長 43 //構(gòu)造時(shí)不傳參數(shù),隊(duì)列大小為1e5,傳入?yún)?shù)時(shí)為自定義大小 44 //如果隊(duì)列不為空,front返回隊(duì)首元素, 45 //如果隊(duì)列為空,pop無效,front返回NULL 46 //clear將隊(duì)列清空, 供多次使用 47 //如果push時(shí)產(chǎn)生沖突,即隊(duì)列已滿, 將加入失敗 48 template <class T> 49 class MyQueue 50 { 51 private: 52 T* que; 53 int si, fr, re; 54 void setValue(int _size) { 55 fr = 0; re = 0; 56 si = _size; 57 que = (T*)malloc(si * sizeof(T)); 58 } 59 public: 60 MyQueue() { 61 this->setValue(100005); 62 } 63 MyQueue(int _size) { 64 this->setValue(_size); 65 } 66 T front() { 67 if(fr != re) 68 return que[fr]; 69 return NULL; 70 } 71 void pop() { 72 if(fr != re) 73 fr = (fr + 1) % si; 74 } 75 void push(T e) { 76 if((re + 1) % si == fr) return ; 77 que[re] = e; 78 re = (re + 1) % si; 79 } 80 bool empty() { 81 if(fr == re) return 1; 82 return 0; 83 } 84 void clear() { 85 fr = 0; 86 re = 0; 87 } 88 }; 89 90 int step[1<<21]; 91 bool vis[1<<21]; 92 93 int BFS(int s) 94 { 95 vis[s] = 1; 96 MyQueue<int>q(1<<21);//定義隊(duì)列的大小為(1<<21),其他無任何修改 97 q.push(s); 98 step[s] = 0; 99 while(!q.empty()) 100 { 101 int u = q.front(); q.pop(); 102 if(u == 0) return step[u]; 103 for(int i=1;i<19;i++) 104 { 105 int r = u; 106 r ^= (1<<i-1) | (1<<i) | (1<<i+1); 107 if(!vis[r]) 108 { 109 vis[r] = 1; 110 step[r] = step[u] + 1; 111 q.push(r); 112 } 113 } 114 int r = u ^ (1<<0) ^ (1<<1); 115 if(!vis[r]) { vis[r] = 1; step[r] = step[u] + 1; q.push(r); } 116 r = u ^ (1<<18) ^ (1<<19); 117 if(!vis[r]) { vis[r] = 1; step[r] = step[u] + 1; q.push(r); } 118 } 119 return -1; 120 } 121 122 int main() 123 { 124 //FOPENIN("in.txt"); 125 int st = 0, x; 126 for(int i=0;i<20;i++) 127 { 128 scanf("%d", &x); 129 st |= (x<<i); 130 } 131 printf("%d\n", BFS(st)); 132 return 0; 133 } View Code?
最后試著把終點(diǎn)判斷放在隊(duì)頭,依然360ms就跑出來了啊,,,我就哭了。。。
?
轉(zhuǎn)載于:https://www.cnblogs.com/gj-Acit/p/3890261.html
總結(jié)
以上是生活随笔為你收集整理的POJ3185(简单BFS,主要做测试使用)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sharepoint 2013 发布功能
- 下一篇: HDU 3934