标签:
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 797 Accepted Submission(s):
361
# include <iostream> # include <fstream> # include <cstring> # include <queue> # include <vector> using namespace std; struct Info { int x, y, jishu; }info; int m, n; char map[101][101]; int is[101][101]; int dx[4] = {1,0,-1,0}; int dy[4] = {0,-1,0,1}; vector <Info> v; int bfs(); bool border(Info & a) { if(a.x >= 1 && a.x <= m && a.y >= 1 && a.y <= n) return true; return false; } int main() { //fstream cin("aaa.txt"); while(cin >> m >> n) { v.clear(); if(m == 0 && n == 0) break; for(int i = 1; i <= m; i++) for(int j = 1; j <= n; j++) { cin >> map[i][j]; if(map[i][j] == ‘D‘) { info.x = i; info.y = j; info.jishu = 0; v.push_back(info); } } int h = bfs(); if(h < 0) cout << "Impossible" << endl; else cout << h << endl; } return 0; } int bfs() { is[v[0].x][v[0].y] = 1; memset(is, 0, sizeof(is)); queue <vector <Info> > Q; Q.push(v); vector <Info> now, next; while(!Q.empty()) { now = Q.front(); Q.pop(); for(int i = 0; i < 4; i++) { next = now; next[0].jishu ++; for(int j = 0; j < next.size(); j++) { next[j].x = now[j].x + dx[i]; next[j].y = now[j].y + dy[i]; } int flag = 0; for(int j = 0; j < next.size(); j++) { if(!border(next[j]) || map[next[j].x][next[j].y] == ‘O‘) flag = 1; // cout << map[next[j].x][next[j].y]; } //cout << endl; if(flag) continue; if(is[next[0].x][next[0].y]) continue; for(int j = 0; j < next.size(); j++) if(map[next[j].x][next[j].y] == ‘Q‘) return next[0].jishu; is[next[0].x][next[0].y] = 1; Q.push(next); } } return -1; }
标签:
原文地址:http://www.cnblogs.com/lyf-acm/p/5399320.html