国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

藍橋杯 2015 決賽 4 穿越雷區

2019-11-11 00:11:56
字體:
來源:轉載
供稿:網友

X星的坦克戰車很奇怪,它必須交替地穿越正能量輻射區和負能量輻射區才能保持正常運轉,否則將報廢。 某坦克需要從A區到B區去(A,B區本身是安全區,沒有正能量或負能量特征),怎樣走才能路徑最短? 已知的地圖是一個方陣,上面用字母標出了A,B區,其它區都標了正號或負號分別表示正負能量輻射區。 例如: A + - + - - + - - + - + + + - + - + - + B + - + - 坦克車只能水平或垂直方向上移動到相鄰的區。 數據格式要求: 輸入第一行是一個整數n,表示方陣的大小, 4<=n<100 接下來是n行,每行有n個數據,可能是A,B,+,-中的某一個,中間用空格分開。 A,B都只出現一次。 要求輸出一個整數,表示坦克從A區到B區的最少移動步數。 如果沒有方案,則輸出-1 例如: 用戶輸入: 5 A + - + - - + - - + - + + + - + - + - + B + - + - 則程序應該輸出: 10 資源約定: 峰值內存消耗 < 512M CPU消耗 < 1000ms

簡單BFS.

#include <bits/stdc++.h>using namespace std;vector< vector<char> > g;vector< vector<bool> > b;int n;const int dx[] = {1, 0, -1, 0};const int dy[] = {0, 1, 0, -1};struct Node{ int x, y, step;};bool exist(int x, int y){ return x >= 0 && x < n && y >= 0 && y < n;}int main(){ //freopen("in", "r", stdin); scanf("%d/n", &n); string s; g.resize(n); for (int i = 0; i < n; i++) { getline(cin, s); for (int j = 0; j < s.size(); j++) { if (s[j] != ' ') g[i].push_back(s[j]); } } Node A, B; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (g[i][j] == 'A') A.x = i, A.y = j, A.step = 0; } } b.resize(n); for (int i = 0; i < n; i++) b[i].resize(n); queue<Node> q; q.push(A); b[A.x][A.y] = true; while (!q.empty()) { Node node = q.front(); q.pop(); b[node.x][node.y] = true; for (int i = 0; i < 4; i++) { int xx = node.x + dx[i], yy = node.y + dy[i]; if (exist(xx, yy)) { if (b[xx][yy]) continue; if (g[xx][yy] == 'B') { cout << node.step + 1 << endl; return 0; } if (g[xx][yy] != g[node.x][node.y]) { q.push((Node){xx, yy, node.step + 1}); } } } } cout << "-1" << endl;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泰兴市| 城口县| 鄂温| 满城县| 拜泉县| 涡阳县| 石景山区| 彩票| 南通市| 海城市| 南充市| 亚东县| 阳春市| 新巴尔虎左旗| 邯郸市| 青龙| 阳东县| 景谷| 娱乐| 同心县| 田东县| 浦北县| 金昌市| 萍乡市| 合水县| 祁阳县| 岑巩县| 玛沁县| 安图县| 修水县| 泸西县| 崇礼县| 汉阴县| 托克逊县| 江城| 开原市| 新邵县| 天柱县| 资讯 | 镇巴县| 普兰县|