Impossible<br>9<br>
思路:
bfs檢索每個防守球員到4分位的位置,取出最小的。(剛開始理解錯的思路)
實際上這題直接bfs。
感想:
這題幹翻譯真他孃的操蛋!什麼身體,亂七八糟的。
就是有幾個D(<20)那麼所有的D都要一起移動,把所有的D看成一個整體。
剛開始以爲只要 存在D可以到達Q就 ok呢,(還以爲測試數據錯了,這題出的,是出題者翻譯的不行呢?還是我理解能力有問題?)
我已無力吐槽!
剛開始代碼:(題意理解錯了)
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<stdlib.h> #include<string.h> #include<fstream> #include<algorithm> #include<queue> using namespace std; char point[102][102]; int H, W; bool flag[102][102]; int direction[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } }; struct position { int x, y; int ans; }; int bfs(int i,int j) { position p; p.x = i; p.y = j; p.ans = 0; flag[p.x][p.y] = true; queue<position>Q; Q.push(p); while (!Q.empty()) { position q = Q.front(); Q.pop(); if (point[q.x][q.y] == 'Q') { return q.ans; } for (int i = 0; i < 4; i++) { position p; p.x = q.x + direction[i][0]; p.y = q.y + direction[i][1]; p.ans = q.ans + 1; if (point[p.x][p.y] == 'O' || flag[p.x][p.y]) continue; if (p.ans > 20) return 0; if (p.x>=H || p.y >= W || p.x < 0 || p.y < 0) continue; Q.push(p); flag[p.x][p.y] = true; } } return 0; } int main() { freopen ("C:\\Users\\liuzhen\\Desktop\\11.txt", "r", stdin); while (1) { cin >> H >> W; if (H == 0 && W == 0) break; for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) { cin >> point[i][j]; }
int cou[100]; memset(cou, 0, sizeof(cou)); int m = 0; for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) { if (point[i][j] == 'D') { memset(flag, false, sizeof(flag));
cou[m++]=bfs(i, j); } }
sort(cou, cou + m); if (cou[0]!=0) cout << cou[0] << endl; else cout << "Impossible" << endl; } freopen("CON", "r", stdin); system("pause"); return 0; }
AC代碼:(需要將上面代碼中結構體改一下,用於存放所有的D;對於標記的問題,只需要標記第一個D的位置
即可(若都標記,則會出錯,自己考慮,不解釋))。
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<stdlib.h> #include<string.h> #include<fstream> #include<algorithm> #include<queue> using namespace std; char point[102][102]; int H, W; bool flag[102][102]; int direction[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } }; struct position { int x[21], y[21]; int ans; }; int bfs(int ax[],int ay[],int m) { position p; for (int i = 0; i < m; i++) { p.x[i] = ax[i]; p.y[i] = ay[i]; } p.ans = 0; flag[p.x[0]][p.y[0]] = true; queue<position>Q; Q.push(p); while (!Q.empty()) { position q = Q.front(); Q.pop(); for (int i = 0; i < m; i++) { if (point[q.x[i]][q.y[i]] == 'Q') { return q.ans; } } for (int i = 0; i < 4; i++) { position p; int mm = 0; for (int j = 0; j < m; j++) { p.x[j] = q.x[j] + direction[i][0]; p.y[j] = q.y[j] + direction[i][1]; p.ans = q.ans + 1; if (p.x[j] >= H || p.y[j] >= W || p.x[j] < 0 || p.y[j] < 0) { mm++; break; } else { if (point[p.x[j]][p.y[j]] == 'O' || flag[p.x[0]][p.y[0]]) { mm++; break; } } } if (mm == 0) { Q.push(p); flag[p.x[0]][p.y[0]] = true; } } } return 0; } int main() { freopen ("C:\\Users\\liuzhen\\Desktop\\11.txt", "r", stdin); while (1) { cin >> H >> W; if (H == 0 && W == 0) break; for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) { cin >> point[i][j]; } int cou[100]; memset(cou, 0, sizeof(cou)); memset(flag, false, sizeof(flag)); int m = 0; int ax[21],ay[21]; for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) { if (point[i][j] == 'D') { ax[m] = i; ay[m] = j; m++; } } int ans = bfs(ax,ay,m); if (ans!=0) cout << ans << endl; else cout << "Impossible" << endl; } freopen("CON", "r", stdin); system("pause"); return 0; }