I - Biridian Forest BFS+最短路
const int INF = 1000000000; const double eps = 1e-8; const int maxn = 1100; char maze[maxn][maxn]; int d[maxn][maxn]; int vis[maxn][maxn]; int s,t; int n,m; struct Point { int x; int y; Point(int _x,int _y):x(_x),y(_y){} }; void bfs() { queue<Point> q; q.push(Point(s,t)); vis[s][t] = 1; d[s][t] = 0; clr(d); while(!q.empty()) { Point p = q.front(); q.pop(); if(p.x > 0) { if(maze[p.x - 1][p.y] != 'T' && !vis[p.x - 1][p.y]) { d[p.x - 1][p.y] = d[p.x][p.y] + 1; vis[p.x - 1][p.y] = 1; q.push(Point(p.x - 1,p.y)); } } if(p.x + 1 < n) { if(maze[p.x + 1][p.y] != 'T' && !vis[p.x + 1][p.y]) { d[p.x + 1][p.y] = d[p.x][p.y] + 1; vis[p.x + 1][p.y] = 1; q.push(Point(p.x + 1,p.y)); } } if(p.y > 0) { if(maze[p.x][p.y - 1] != 'T' && !vis[p.x][p.y - 1]) { d[p.x][p.y - 1] = d[p.x][p.y] + 1; vis[p.x][p.y - 1] = 1; q.push(Point(p.x,p.y - 1)); } } if(p.y + 1 < m) { if(maze[p.x][p.y + 1] != 'T' && !vis[p.x][p.y + 1]) { d[p.x][p.y + 1] = d[p.x][p.y] + 1; vis[p.x][p.y + 1] = 1; q.push(Point(p.x,p.y + 1)); } } } } int main() { //freopen("in.txt","r",stdin); while(cin>>n>>m) { clr(vis); int ss,tt; rep(i,0,n) { scanf("%s",maze[i]); rep(j,0,m) { if(maze[i][j] == 'E') { s = i; t = j; } if(maze[i][j] == 'S') { ss = i; tt = j; } } } bfs(); int ans = 0; rep(i,0,n) rep(j,0,m) { if(maze[i][j] > '0' && maze[i][j] <= '9') { if(d[i][j] <= d[ss][tt] && d[i][j]) ans += maze[i][j] - '0'; } } cout<<ans<<endl; } return 0; }
posted on 2013-11-22 20:45 keep trying 阅读(275) 评论(0) 收藏 举报