[蓝桥杯2015决赛]穿越雷区-bfs
生活随笔
收集整理的這篇文章主要介紹了
[蓝桥杯2015决赛]穿越雷区-bfs
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
X星的坦克戰車很奇怪,它必須交替地穿越正能量輻射區和負能量輻射區才能保持正常運轉,否則將報廢。
某坦克需要從A區到B區去(A,B區本身是安全區,沒有正能量或負能量特征),怎樣走才能路徑最短?
已知的地圖是一個方陣,上面用字母標出了A,B區,其它區都標了正號或負號分別表示正負能量輻射區。
例如:
坦克車只能水平或垂直方向上移動到相鄰的區。
輸入
輸入第一行是一個整數n,表示方陣的大小, 4<=n<100
接下來是n行,每行有n個數據,可能是A,B,+,-中的某一個,中間用空格分開。
輸入保證A,B都只出現一次。
輸出
要求輸出一個整數,表示坦克從A區到B區的最少移動步數。
如果沒有方案,則輸出-1
樣例輸入
樣例輸出
10知識點:
gets()和scanf()的區別在于輸入的字符串是否中間有空格:對于前者,只有遇到"\n"時才停止輸入,而對于后者,出現"\n"或空格都停止輸入。
AC代碼如下:
#include <iostream> #include <cstring> #include <queue> using namespace std; const int N = 110; typedef pair<int, int>PII; #define x first #define y second char g[N][N]; int dis[N][N]; bool st[N][N]; int n;int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};int bfs(PII start, PII end) {queue<PII>q;q.push(start);dis[start.x][start.y] = 0;st[start.x][start.y] = true;while (q.size()) {PII t = q.front();q.pop();for (int i = 0; i < 4; i++) {int xx = t.x + dx[i], yy = t.y + dy[i];if (xx < 0 || xx >= n || yy < 0 || yy >= n)continue;if (st[xx][yy])continue;if (g[xx][yy] == g[t.x][t.y])continue;st[xx][yy] = true;dis[xx][yy] = dis[t.x][t.y] + 1;if (end == make_pair(xx, yy))return dis[xx][yy];q.push({xx, yy});}}return -1; }int main() {cin >> n;PII start, end;memset(dis, -1, sizeof(dis));for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {cin >> g[i][j];//因為cin輸入遇到空格會停下,所以用二重循環來讀入if (g[i][j] == 'A')start = {i, j};else if (g[i][j] == 'B')end = {i, j};}}int distance = bfs(start, end);if (distance == -1)cout << -1 << endl;elsecout << distance << endl;return 0;}錯誤代碼如下:
#include <iostream> #include <cstring> #include <queue> using namespace std; const int N = 110; typedef pair<int, int>PII; #define x first #define y second char g[N][N]; int dis[N][N]; bool st[N][N]; int n;int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};int bfs(PII start, PII end) {queue<PII>q;q.push(start);dis[start.x][start.y] = 0;st[start.x][start.y] = true;while (q.size()) {PII t = q.front();q.pop();for (int i = 0; i < 4; i++) {int xx = t.x + dx[i], yy = t.y + dy[i];if (xx < 0 || xx >= n || yy < 0 || yy >= n)continue;if (st[xx][yy])continue;if (g[xx][yy] == g[t.x][t.y])continue;st[xx][yy] = true;dis[xx][yy] = dis[t.x][t.y] + 1;if (end == make_pair(xx, yy))return dis[xx][yy];q.push({xx, yy});}}return -1; }int main() {cin >> n;PII start, end;memset(dis, -1, sizeof(dis));for (int i = 0;i<n;i++){ // scanf("%s",g[i]);cin>>g[i];//這兩種輸入寫法,都會遇到空格就停下來for (int j = 0;j<n;j++){if (g[i][j]=='A') start = {i,j};else if (g[i][j]=='B') end = {i,j};}}int distance = bfs(start, end);if (distance == -1)cout << -1 << endl;elsecout << distance << endl;return 0;}AC代碼如下:
#include <iostream> #include <queue> using namespace std; const int N = 110; bool vis[N][N]; char g[N][N];struct node {int x,y;int p; };int n;int dx[] ={0,0,1,-1},dy[] = {1,-1,0,0};int bfs(node s,node e) {queue<node>q;q.push(s);vis[s.x][s.y] = true;while(q.size()){node t = q.front();q.pop();if (t.x == e.x && t.y==e.y){return t.p;}for (int i = 0;i<4;i++){int xx = t.x+dx[i],yy =t.y+dy[i];if (xx <0 || xx>= n || yy < 0 || yy >= n || vis[xx][yy]) continue;if (g[xx][yy]==g[t.x][t.y]) continue;vis[xx][yy] = true;node n;n = {xx,yy,t.p+1};q.push(n);}}return -1; }int main() {cin>>n;node s,e;for (int i = 0;i<n;i++)for (int j = 0;j<n;j++)cin>>g[i][j];for (int i = 0;i<n;i++)for (int j = 0;j<n;j++){if (g[i][j]=='A')s = {i,j,0};if (g[i][j]=='B')e = {i,j,0};}cout<<bfs(s,e)<<endl;return 0; }錯誤代碼如下:
下面這個代碼為什么不行呢?因為int n兩次了,一次在main函數外,一次在main函數里面,有時候寫代碼快的時候,容易發生這樣的錯誤,還比較難找到。
#include <iostream> #include <queue> using namespace std; const int N = 110; bool vis[N][N]; char g[N][N];struct node {int x,y;int p; };int n; int dx[] ={0,0,1,-1},dy[] = {1,-1,0,0};int bfs(node s,node e) {queue<node>q;q.push(s);vis[s.x][s.y] = true;while(q.size()){node t = q.front();q.pop();if (t.x == e.x && t.y==e.y){return t.p;}for (int i = 0;i<4;i++){int xx = t.x+dx[i],yy =t.y+dy[i];if (xx <0 || xx>= n || yy < 0 || yy >= n || vis[xx][yy]) continue;if (g[xx][yy]==g[t.x][t.y]) continue;vis[xx][yy] = true;node n;n = {xx,yy,t.p+1};q.push(n);}}return -1; }int main() {int n;cin>>n;node s,e;for (int i = 0;i<n;i++)for (int j = 0;j<n;j++)cin>>g[i][j];for (int i = 0;i<n;i++)for (int j = 0;j<n;j++){if (g[i][j]=='A')s = {i,j,0};if (g[i][j]=='B')e = {i,j,0};}cout<<bfs(s,e)<<endl;return 0; }總結
以上是生活随笔為你收集整理的[蓝桥杯2015决赛]穿越雷区-bfs的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AcWing 1113. 红与黑
- 下一篇: Rust的安全系统编程