POJ3185(簡單BFS,主要做測試使用)

沒事做水了一道POJ的簡單BFS的題目

這道題的數據范圍是20,所以狀態總數就是(1<<20)

第一次提交使用STL的queue,并且是在隊首判斷是否達到終點,達到終點就退出,超時:(其實這里我是很不明白的,,TM狀態總數就只有1e6怎么也不應該超時的,,,,只能說STL的queue的常數實在是太大,完全沒法弄。。。)

 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后馬上把判斷放到隊尾(就是說在一個狀態進隊列前先判斷是不是終點狀態,是的話就退出),跑了875ms,勉強過了,,(這里我就是亂改的了,代碼沒任何觀賞性)

 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

?

然后突然想起之前寫過一個靜態隊列的模板,是用循環隊列寫的,想著正好去測試下,就改了隊列的定義,其他使用完全一致,沒任何修改,結果跑了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 //定義了一個固定長度的隊列, 不能動態增長
 43     //構造時不傳參數,隊列大小為1e5,傳入參數時為自定義大小
 44     //如果隊列不為空,front返回隊首元素,
 45     //如果隊列為空,pop無效,front返回NULL
 46     //clear將隊列清空, 供多次使用
 47     //如果push時產生沖突,即隊列已滿, 將加入失敗
 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);//定義隊列的大小為(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

?

最后試著把終點判斷放在隊頭,依然360ms就跑出來了啊,,,我就哭了。。。

?

轉載于:https://www.cnblogs.com/gj-Acit/p/3890261.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/376775.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/376775.shtml
英文地址,請注明出處:http://en.pswp.cn/news/376775.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

tomcat站點配置

tomcat版本&#xff1a;tomcat5.5.91、打開tomcat\conf\server.xml&#xff0c;在里面找到<Engine name"Catalina" defaultHost"localhost">.....</Engine>2、在<Engine name"Catalina" defaultHost"localhost"><…

新的視頻會議模式:StarlineProject

目錄效果展示部分用戶參與度部分技術細節機械裝置以及硬件配置。視頻系統照明人臉跟蹤壓縮和傳輸圖像渲染音頻系統step1&#xff1a;捕獲音頻step2&#xff1a;音頻去噪處理step3&#xff1a;壓縮、傳輸、解壓step4&#xff1a;渲染可以改進的點效果展示部分 〔映維網〕谷歌光場…

HDU 3934

/*這是用的有旋轉卡殼的思想。 首先確定i&#xff0c;j&#xff0c;對k進行循環&#xff0c;知道找到第一個k使得cross(i,j,k)>cross(i,j,k1),如果ki進入下一次循環。 對j&#xff0c;k進行旋轉&#xff0c;每次循環之前更新最大值&#xff0c;然后固定一個j&#xff0c;同樣…

[ios] UILocalNotification實現本地的鬧鐘提醒【轉】

http://www.cnblogs.com/jiangshiyong/archive/2012/06/06/2538204.html轉載于:https://www.cnblogs.com/jinjiantong/archive/2013/04/01/2992624.html

sql server根據表中數據生成insert語句

幾個收藏的根據數據庫生成Insert語句的存儲過程[修正版]----根據表中數據生成insert語句的存儲過程--建立存儲過程&#xff0c;執行spGenInsertSQL 表名--感謝playyuer----感謝szyicol--CREATEproc[dbo].[spGenInsertSQL](tablenamevarchar(256))asbegindeclaresqlvarchar(8000…

Javascript eval()函數 基礎回顧

如果您想詳細了解ev al和JSON請參考以下鏈接&#xff1a; eval &#xff1a;https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Functions/Eval JSON&#xff1a;http://www.json.org/ eval函數的工作原理 eval函數會評估一個給定的含有JavaScript代碼的…

雜感無題|

今天中午和組里面的人吃飯&#xff0c;聊起了科興跳樓的事情。這事其實前幾天我華為的mentor就轉給我了&#xff0c;當時也沒太在意&#xff0c;在脈脈上看了看&#xff0c;也不知曉是誰&#xff0c;想著可能又是抑郁癥吧。 飯后依舊繞著食堂散步&#xff0c;ly說那個人好像還是…

uva1366_Martian Mining_簡單DP

題目不難&#xff0c;卻想了好長時間&#xff0c;目測自己DP還是很水。。。囧 思路&#xff1a;舍f[i][j]為前i行j列的最大礦總量不難推出狀態轉移方程為f[i][j]max(f[i-1][j]line[i][j],f[i][j-1]row[j][i]) 其中line[i][j]為第i行前j個A礦的和&#xff08;a[i][1]a[i][2]...a…

數學圖形之Boy surface

這是一個姓Boy的人發現的,所以取名為Boy surface.該圖形與羅馬圖形有點相似,都是三分的圖形.它甚至可以說是由羅馬曲面變化而成的. 本文將展示幾種Boy曲面的生成算法和切圖,使用自己定義語法的腳本代碼生成數學圖形.相關軟件參見:數學圖形可視化工具,該軟件免費開源.QQ交流群: …

開個定時器給echarts組件配置定時更新

我在js文件中開了個定時器&#xff0c;每1s從后端獲取數據并解析&#xff0c;然后用異步方法就渲染不出來&#xff0c;改成同步就可以了。 這個解決方法來自于這篇文章&#xff0c;我出的問題和他一樣&#xff1a;關于ajax中readyState的值一直為1的問題 這里將ajax參數修改為f…

SDK 操作 list-view control 實例 -- 遍歷進程

遍歷窗口&#xff0c;獲得控件句柄 1 EnumChildWindows(hwndDlg, (WNDENUMPROC)EnumChildProc, NULL); 回調函數 1 BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam )2 {3 char strCLSName[MAXBYTE] {0};4 GetClassName(hwnd, strCLSName, MAXBYTE);5 if (…

推薦一份不錯的清除默認樣式的CSS樣式

時間過得真快&#xff0c;離 Reset CSS 研究&#xff08;八卦篇&#xff09; 已經 3 個多月了。廢話少說&#xff0c;趕緊將技術篇寫完吧。 回顧與反思 第一份 reset css 是 Tantek 的 undohtml.css, 很簡單的代碼&#xff0c;Tantek 根據自己的需要&#xff0c;對瀏覽器的默認…

python深淺拷貝

在python中&#xff0c;對象賦值實際上是對象的引用。當創建一個對象&#xff0c;然后把它賦給另一個變量的時候&#xff0c;python并沒有拷貝這個對象&#xff0c;而只是拷貝了這個對象的引用。 所以一個結構類型被賦給另外一個對象的時候&#xff0c;盡可能不使用 &#xff…

Flash中的SLC/MLC/MLC--基礎

參考 1.http://www.upantool.com/jiaocheng/qita/2012/slc_mlc_tlc.html 2.http://www.2ic.cn/html/10/t-432410.html 3.http://kms.lenovots.com/kb/article.php?id15382 4.http://www.albertknight.com/222.html 5.http://ssd.zol.com.cn/371/3716632.html 6.這個圖比較多 h…

python定義對象的比較方法

有時候我們需要比較兩個對象。比如哪個對象大,哪個對象小。如果我們不告訴python如何比較,那么Python是不知道如何進行比較的。 下面提供實例 #__eq__(self,other)&#xff1a; #在使用比較運算符比較兩個對象是否相等的時候會調用這個方法。 #如果是相等&#xff0c;那么應該返…

關于Oracle Insert 語句的子查詢 和 with check option的用法

今日睇ocp教程 發現 insert語句還可以子查詢例如&#xff1a;INSERT INTO (SELECT employee_id, last_name, email, hire_date, job_id, salary, department_id FROM employees where department_id 50 )VALUES (9999…

apple mac 下使用機械鍵盤的辦法,鍵盤映射工具軟件,apple mac Mechanical keyboard

apple mac 下使用機械鍵盤的辦法&#xff0c;鍵盤映射工具軟件&#xff0c;apple mac Mechanical keyboard 想在蘋果電腦 mac 系統下使用 機械鍵盤&#xff0c;大部分機械鍵盤不是為mac設計的&#xff0c;所以需要用軟件做一下鍵盤映射。 推薦使用這個&#xff1a;https://pqrs…

Python中鍵映射多個值的方法:defaultdict

Python中鍵映射多個值的方法有兩種&#xff1a; 想保持元素的插入順序就應該使用列表&#xff1b; 想去掉重復元素就使用集合并且不關心元素的順序問題的話應該使用set from collections import defaultdictmapping defaultdict(list)mapping [key].append(value)mapping d…

該不該讓Google收錄WordPress的目錄頁和標簽頁?

只要有一點SEO知識的 站長都會注意利用相關文件和元標簽來控制Google對網站的收錄&#xff0c;對于WordPress網站來說&#xff0c;除了我們主動添加的內容頁面&#xff0c;Google還會收錄目錄歸檔頁&#xff0c;標簽歸檔頁&#xff0c;時間歸檔頁&#xff0c;以及作者歸檔頁。這…

【原創】MapReduce編程系列之表連接

問題描述需要連接的表如下&#xff1a;其中左邊是child&#xff0c;右邊是parent&#xff0c;我們要做的是找出grandchild和grandparent的對應關系&#xff0c;為此需要進行表的連接。 Tom Lucy Tom Jim Lucy David Lucy Lili Jim Lilei Jim SuSan Lily Green Lily Bians Green…