HDU 1728 逃離迷宮

這道題做的我想哭啊。。WA了將近十次了吧

一開始我用數組模擬的隊列,后來和老大代碼對拍,感覺改的是基本都一模一樣了,還是WA

實在沒有辦法了,改用queue了

?

題目里的x是列y是行,和代碼里的反過來的,要注意!

題目里面說在起點的時候無論朝哪個方向走都不算一次轉彎,所以我們將方向和轉彎次數都賦值為-1,這樣就不用特殊處理了

入隊條件,拓展后的轉彎次數小于或等于vis數組中記錄的最小轉彎次數即可入隊

輸出結果,不要一搜到終點便急著輸出,應為可能后面再一次搜到終點的時候轉彎次數小于k

因此可以遍歷完以后再輸出,或者輸出yes的時候加一個限制條件:轉彎次數小于等于k

兩種處理方法,第二種更快一點

?

這里先把數組WA掉的代碼貼上,留著以后再找錯。

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 struct Point
 8 {
 9     int x, y;
10     int di, times;
11 }qu[20000 + 10];
12 
13 const int MAX = 200;
14 char map[105][105];
15 int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
16 int row, col, vis[105][105];
17 int sx, sy, ex, ey, k;
18 int head, tail;
19 
20 bool islegal(int x, int y)
21 {
22     if(x>=0 && x<row && y>=0 && y<col && map[x][y]=='.')
23         return true;
24     return false;
25 }
26 
27 void BFS(void)
28 {
29     head = 0, tail = 1;
30     vis[sx][sy] = 0;
31     qu[0].x = sx, qu[0].y = sy;
32     qu[0].di = -1, qu[0].times = -1;
33     while(head < tail)
34     {
35         //if(qu[head].x==ex && qu[head].y==ey)
36             //{printf("yes\n");    return;}
37         for(int i = 0; i < 4; ++i)
38         {
39             int temp;
40             int xx = qu[head].x + dir[i][0];
41             int yy = qu[head].y + dir[i][1];
42             if(!islegal(xx, yy))    continue;
43             if(i != qu[head].di)
44                 temp = qu[head].times + 1;
45             else
46                 temp = qu[head].times;
47             if(temp > k)    continue;
48             if(temp <= vis[xx][yy])
49             {
50                 vis[xx][yy] = temp;
51                 qu[tail].x = xx, qu[tail].y = yy;
52                 qu[tail].di = i;
53                 qu[tail++].times = temp;
54             }
55         }
56         ++head;
57     }
58     if(vis[ex][ey] <= k)
59         printf("yes\n");
60     else
61         printf("no\n");
62 }
63 
64 int main(void)
65 {
66     #ifdef LOCAL
67         freopen("1728in.txt", "r", stdin);
68     #endif
69 
70     int T;
71     scanf("%d", &T);
72     while(T--)
73     {
74         scanf("%d%d", &row, &col);
75         getchar();
76         for(int i = 0; i < row; ++i)
77         {
78             for(int j = 0; j < col; ++j)
79             {
80                 scanf("%c", &map[i][j]);
81                 vis[i][j] = MAX;
82             }
83             getchar();
84         }
85         scanf("%d%d%d%d%d", &k, &sy, &sx, &ey, &ex);
86         --sx, --sy, --ex, --ey;
87         BFS();
88     }
89     return 0;
90 }
代碼君

?

queue的AC代碼:

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <queue>
 6 using namespace std;
 7 
 8 struct Point
 9 {
10     int x, y;
11     int di, times;
12 }qu[20000 + 10];
13 
14 const int MAX = 200;
15 char map[105][105];
16 int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
17 int row, col, vis[105][105];
18 int sx, sy, ex, ey, k;
19 
20 bool islegal(int x, int y)
21 {
22     return(x>=0 && x<row && y>=0 && y<col && map[x][y]=='.');
23 }
24 
25 void BFS(void)
26 {
27     queue<Point> qu;
28     vis[sx][sy] = 0;
29     Point cur;
30     cur.x = sx, cur.y = sy;
31     cur.di = cur.times = -1;
32     qu.push(cur);
33     while(!qu.empty())
34     {
35         cur = qu.front();
36         qu.pop();
37         if(cur.x==ex&&cur.y==ey&&cur.times<=k)
38         {
39             printf("yes\n");
40             return;
41         }
42         for(int i = 0; i < 4; ++i)
43         {
44             Point next = cur;
45             next.x += dir[i][0];
46             next.y += dir[i][1];
47             if(!islegal(next.x, next.y))    continue;
48             if(i != cur.di)
49             {
50                 next.di = i;
51                 ++next.times;
52             }
53             if(next.times > k)    continue;
54             if(next.times <= vis[next.x][next.y])
55             {
56                 vis[next.x][next.y] = next.times;
57                 qu.push(next);
58             }
59         }
60     }
61     printf("no\n");
62 }
63 
64 int main(void)
65 {
66     #ifdef LOCAL
67         freopen("1728in.txt", "r", stdin);
68     #endif
69 
70     int T;
71     scanf("%d", &T);
72     while(T--)
73     {
74         scanf("%d%d", &row, &col);
75         getchar();
76         for(int i = 0; i < row; ++i)
77         {
78             for(int j = 0; j < col; ++j)
79             {
80                 scanf("%c", &map[i][j]);
81                 vis[i][j] = MAX;
82             }
83             getchar();
84         }
85         scanf("%d%d%d%d%d", &k, &sy, &sx, &ey, &ex);
86         --sx, --sy, --ex, --ey;
87         BFS();
88     }
89     return 0;
90 }
代碼君

?

對比了一下別人的代碼,才知道自己寫的代碼有多么挫。。

http://972169909-qq-com.iteye.com/blog/1244218

這里講了兩種方法,因為題目只是問能不能在轉k個彎之內到達,而不是轉的最小的彎,所以DFS也是能做的。30MS多點

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 const int MAX = 200;
 8 char map[105][105];
 9 int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
10 int row, col, vis[105][105];
11 int sx, sy, ex, ey, k;
12 bool flag; 
13 
14 bool islegal(int x, int y)
15 {
16     return(x>=0 && x<row && y>=0 && y<col && map[x][y]=='.');
17 }
18 
19 void DFS(int x, int y, int di)
20 {
21     if(x==ex && y==ey && vis[ex][ey]<=k)
22         {flag = true;   return;}
23     if(vis[x][y] > k)
24         return;
25     if(x!=ex && y!=ey && vis[x][y]==k)
26         return;
27     for(int i = 0; i < 4; ++i)
28     {
29         int tx = x + dir[i][0];
30         int ty = y + dir[i][1];
31         if(!islegal(tx, ty))
32             continue;
33         if(vis[tx][ty] < vis[x][y])
34             continue;
35         if(i!=di && vis[tx][ty] < vis[x][y]+1)
36             continue;
37         vis[tx][ty] = vis[x][y];
38         if(i != di)
39             ++vis[tx][ty];
40         DFS(tx, ty, i);
41         if(flag)
42             return;
43     }
44 }
45 
46 int main(void)
47 {
48     #ifdef LOCAL
49         freopen("1728in.txt", "r", stdin);
50     #endif
51 
52     int T;
53     scanf("%d", &T);
54     while(T--)
55     {
56         scanf("%d%d", &row, &col);
57         getchar();
58         for(int i = 0; i < row; ++i)
59         {
60             for(int j = 0; j < col; ++j)
61             {
62                 scanf("%c", &map[i][j]);
63                 vis[i][j] = MAX;
64             }
65             getchar();
66         }
67         scanf("%d%d%d%d%d", &k, &sy, &sx, &ey, &ex);
68         --sx, --sy, --ex, --ey;
69         vis[sx][sy] = -1;
70         flag = false;
71         DFS(sx, sy, -1);
72         if(flag)    printf("yes\n");
73         else    printf("no\n");
74     }
75     return 0;
76 }
代碼君

?

下面重頭戲來了,解法永遠都是只有更好沒有最好。博主講了一個單方向優先擴展的廣搜

把一般的廣搜比作一個石子在起點激起一圈圈漣漪,單方向優先的就是向開車一樣“橫沖直撞”、直來直往

因為我們是要找轉彎次數最小的,因此這樣的搜索方式應該也能較快的找到符合條件的路徑,運行時間15MS

感覺代碼的有些小細節還沒有理解透,等以后回過頭來慢慢消化

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <queue>
 6 using namespace std;
 7 
 8 struct Point
 9 {
10     int x, y;
11 }cur, next;
12 
13 const int MAX = 200;
14 char map[105][105];
15 int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
16 int row, col, vis[105][105];
17 int sx, sy, ex, ey, k;
18 
19 bool islegal(int x, int y)
20 {
21     return(x>=0 && x<row && y>=0 && y<col && map[x][y]=='.');
22 }
23 
24 void BFS(void)
25 {
26     queue<Point> qu;
27     vis[sx][sy] = -1;
28     Point cur;
29     cur.x = sx, cur.y = sy;
30     qu.push(cur);
31     while(!qu.empty())
32     {
33         cur = qu.front();
34         qu.pop();
35         if(cur.x==ex&&cur.y==ey&&vis[cur.x][cur.y]<=k)
36         {
37             printf("yes\n");
38             return;
39         }
40         for(int i = 0; i < 4; ++i)
41         {
42             next = cur;
43             next.x += dir[i][0];
44             next.y += dir[i][1];
45             while(islegal(next.x, next.y))
46             {
47                 if(vis[next.x][next.y] < vis[cur.x][cur.y] + 1)
48                     break;
49                 vis[next.x][next.y] = vis[cur.x][cur.y] + 1;
50                 if(vis[next.x][next.y] > k)
51                     break;
52                 if(vis[next.x][next.y] == k && next.x!=ex && next.y!=ey)
53                     break;
54                 qu.push(next);
55                 next.x += dir[i][0];
56                 next.y += dir[i][1];
57             }
58         }
59     }
60     printf("no\n");
61 }
62 
63 int main(void)
64 {
65     #ifdef LOCAL
66         freopen("1728in.txt", "r", stdin);
67     #endif
68 
69     int T;
70     scanf("%d", &T);
71     while(T--)
72     {
73         scanf("%d%d", &row, &col);
74         getchar();
75         for(int i = 0; i < row; ++i)
76         {
77             for(int j = 0; j < col; ++j)
78             {
79                 scanf("%c", &map[i][j]);
80                 vis[i][j] = MAX;
81             }
82             getchar();
83         }
84         scanf("%d%d%d%d%d", &k, &sy, &sx, &ey, &ex);
85         --sx, --sy, --ex, --ey;
86         BFS();
87     }
88     return 0;
89 }
代碼君

?

轉載于:https://www.cnblogs.com/AOQNRMGYXLMV/p/3918065.html

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

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

相關文章

Nginx(六)-- 配置文件之Gzip

1.概念及作用 Gizp主要對內容、靜態文件做壓縮&#xff0c;用來提升網站訪問速度&#xff0c;節省帶寬。 2.使用方法 gzip既可以配置在server中&#xff0c;也可以配置在server外&#xff0c;此處配置在server中&#xff0c;如下&#xff1a; 說明&#xff1a;  gizp on|off 是…

誤碼率越高越好還是越低越好_夜間護理步驟越多越好還是越少越好?NFF

現在很多人都知道了夜晚是護膚的黃金護膚時間&#xff0c;有些很聰明的姐妹就從夜晚著手&#xff0c;使用很多種護膚品&#xff0c;希望達到事半功倍的效果&#xff0c;但好皮膚不常有&#xff0c;皮膚問題卻常有&#xff01;既然如此&#xff0c;不少人就問了&#xff0c;夜間…

【隨機森林】random forests 簡單介紹

Random Forest&#xff0c;顧名思義 Random 就是隨機抽取&#xff1b; Forest 就是說這里不止一棵樹&#xff0c;而由 一群決策樹組成的一片森林 &#xff0c;連起來就是用隨機抽取的方法訓練出一群決策樹來完成分類任務。RF用了兩次隨機抽取, 一次是對訓練樣本的隨機抽取; 另一…

側邊工具開發2

1.使用圖片的形式會出現大量的圖片&#xff0c;影響性能&#xff0c;而且不易修改&#xff0c;所有使用圖標加文字的形式進行 <a href"javacript:;" class"toolbar-item"><span class"toolbar-btn"><i class"toolbar-icon&q…

斐波那契?

斐波那契&#xff1f; Time Limit: 1000ms Memory limit: 32768K 有疑問&#xff1f;點這里^_^ 題目描述 給出一個數列的遞推公式&#xff0c;希望你能計算出該數列的第N個數。遞推公式如下&#xff1a; F(n)F(n-1)F(n-2)-F(n-3). 其中&#xff0c;F(1)2, F(2)3, F(3)5. 很熟…

clustalw序列比對_序列比對之Clustalx與Clustalw使用指南

相關專題這幾天實驗需要做多序列比對&#xff0c;很久不做了&#xff0c;一時之間不知道如何使用clustal這個工具了。在網上搜集了一些資料&#xff0c;做個整理&#xff0c;總結了Clustalx和Clustalw的使用&#xff0c;省得以后久不使用又生疏了&#xff0c;又要去整理了&…

信息安全系統設計基礎第三周學習總結—20135227黃曉妍

一.Vim編輯器 1.Vim的六種模式 2.Vim三種常用模式的使用方式&#xff0c;以及三者的切換。打開Vim即默認進入普通模式&#xff0c;按i進入插入模式&#xff0c;按esc從插入模式退出普通模式&#xff0c;再按&#xff1a;進入命令行模式。 普通模式下游標的移動 按鍵 說明 h …

關于指定日期的獲取

java使用Calendar類獲得指定日期 關于指定日期的獲取&#xff0c;是根據指定日期和當前日期相差的天數&#xff0c;然后使用set方法設置Calendar.DAY_OF_MONTH的值。Calendar cal Calendar.getInstance();cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - da…

nodejs的package.json依賴dependencies中 ^ 和 ~ 的區別

nodejs的package.json定義了一個模塊&#xff0c;包括其依賴關系的一個簡單的JSON文件&#xff0c;該文件可以包含多個不同的指令來告訴Node包管理器如何處理模塊。 dependencies則表示此模塊依賴的模塊和版本&#xff0c;其中常常可以看到類似 ^1.2.0 或 ~1.2.0 這樣的版本范圍…

腳本命令_SAP HANA數據庫備份命令腳本

需求場景&#xff1a;HANA數據庫版本 2.044 &#xff0c; SYSTEMDB庫1個&#xff0c;Tenant庫有3個 PRD、POP、HAP需要用命令行備份。備份原理說明&#xff1a;1、腳本同hana studio 一樣&#xff0c;用SYSTEM用戶去備份所有的數據庫。2、備份腳本工作在數據庫管理員用戶下&…

Spring 基于Java的Bean聲明

Spring 基于Java的Bean聲明 使用Configuration進行設置&#xff1b; Xml&#xff1a; <?xml version"1.0" encoding"UTF-8"?> <beans xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xmlns"http://www.springframework.…

手機音頻通道被占用_關于凱叔講故事APP的音頻導出下載

孩子喜歡聽凱叔講故事&#xff0c;起先是三國演義和博物學&#xff0c;在網上聽了個開頭后&#xff0c;毫不猶豫買了正版,心想著購買app可以下載音頻&#xff0c;完了拷貝到其他播放器聽。然而......然而......大失所望&#xff0c;美其名曰保護正版&#xff0c;可這么個玩意&a…

編譯安裝 apache 2.4.6

如果配置apr&#xff0c;需要預先安裝apr 以下是安裝apache 步驟: groupadd webuser useradd -g webuser webuser 下載apache2 下載鏈接&#xff1a;http://pan.baidu.com/s/1ntiGWvZ 配置 ./configure --prefix/server/apache2 \ --enable-mods-sharedmost \ --enable-so \ --…

CSS3中border-radius、box-shadow與gradient那點事兒

一、border-radius border-radius用于添加圓角邊框&#xff0c;用處非常廣泛。 1&#xff09;一個值&#xff0c;代表了四個角 .radius-one {/* Safari 3-4, iOS 1-3.2, Android 1.6- */-webkit-border-radius: 12px; /* Firefox 1-3.6 */-moz-border-radius: 12px; /* Opera 1…

編程 跳臺階_Java版劍指offer編程題第8題--跳臺階

跟learnjiawa一起每天一道算法編程題&#xff0c;既可以增強對常用API的熟悉能力&#xff0c;也能增強自己的編程能力和解決問題的能力。算法和數據結構&#xff0c;是基礎中的基礎&#xff0c;更是筆試的重中之重。不積硅步&#xff0c;無以至千里&#xff1b;不積小流&#x…

獲取漢字的首字母(轉)

轉換 獲取一個漢字的拼音首字母。 GB碼兩個字節分別減去160&#xff0c;轉換成10進制碼組合就可以得到區位碼例如漢字“你”的GB碼是0xC4/0xE3&#xff0c;分別減去0xA0&#xf…

$ionicPopup

轉自&#xff1a;http://www.ionicframework.com/docs/api/service/%24ionicPopup/ Usage A few basic examples, see below for details about all of the options available. angular.module(mySuperApp, [ionic]) .controller(PopupCtrl,function($scope, $ionicPopup, $tim…

目標規劃運籌學例題doc_運籌學之目標規劃(胡運權版).doc

運籌學之目標規劃(胡運權版).doc第七章 目標規劃1 目標規劃的提出線性規劃問題是討論一個給定的線性目標函數在一組線性約束條件下的最大值或最小值問題。對于一個實際問題&#xff0c;管理科學者根據管理層決策目標的要求&#xff0c;首先確定一個目標函數以衡量不同決策的優劣…

Deep Learning(深度學習) 學習筆記(四)

神經概率語言模型&#xff0c;內容分為三塊&#xff1a;問題&#xff0c;模型與準則&#xff0c;實驗結果。[此節內容未完待續...] 1&#xff0c;語言模型問題 語言模型問題就是給定一個語言詞典包括v個單詞&#xff0c;對一個字串做出二元推斷&#xff0c;推斷其是否符合該語言…

Java Virtual Machine

后續完善轉載于:https://www.cnblogs.com/fight-tao/p/4849167.html