Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
做了一天了,总是WA,多余的空格也注意到了。请高手指点一下吧,感激不尽!!!#include<stdio.h> #include<queue> #define MAXN 55 #define MAXINT 999999999 typedef struct myNode { int x; int y; int step; }Node; char map[MAXN][MAXN]; Node node[2*MAXN]; int nrow, ncolumn, nvertex; int g[2*MAXN][2*MAXN], id[MAXN][MAXN], move[4][2]={{0,1},{0,-1},{-1,0},{1,0}}; using namespace std; void init() { int i,j; for(i=0;i<=nrow;i++) { for(j=0;j<=ncolumn;j++) { (i==j) ? (g[i][j]=0) : (g[i][j]=MAXINT); id[i][j]=0; } } } void bfs(Node s) { int i,nx,ny,step; int vis[MAXN][MAXN]={{0}}; Node temp,p; queue<Node> que; que.push(s); vis[s.x][s.y]=1; while(!que.empty()) { temp=que.front(); que.pop(); for(i=0;i<4;i++) { nx=temp.x + move[i][0]; ny=temp.y + move[i][1]; step=temp.step; if(!vis[nx][ny] && nx>=0 && nx<ncolumn && ny>=0 && ny<nrow) { if('A'==map[nx][ny] || 'S'==map[nx][ny]) { p.x=nx; p.y=ny; p.step=++step; que.push(p); vis[nx][ny]=1; g[id[s.x][s.y]][id[nx][ny]]=g[id[nx][ny]][id[s.x][s.y]]=step; } if(' '==map[nx][ny]) { p.x=nx; p.y=ny; p.step=++step; que.push(p); vis[nx][ny]=1; } } } } } void generateGraph() { int i,j; for(i=0;i<nrow;i++) gets(map[i]); nvertex=0; for(i=0;i<nrow;i++) { for(j=0;j<ncolumn;j++) { if('A'==map[i][j] || 'S'==map[i][j]) { node[nvertex].x=i; node[nvertex].y=j; node[nvertex].step=0; id[i][j]=nvertex++; } } } for(i=0;i<nvertex;i++) bfs(node[i]); } int prim(int start) { int v,i,distance; int visited[MAXN]={0},dis[MAXN]; for(i=0;i<nvertex;i++) dis[i]=MAXINT; dis[start]=0; v=start; while(!visited[v]) { visited[v]=1; for(i=0;i<nvertex;i++) { if(!visited[i] && dis[i]>g[v][i]) dis[i]=g[v][i]; } distance=MAXINT; for(i=0;i<nvertex;i++) { if(!visited[i] && dis[i]<distance) { distance=dis[i]; v=i; } } } distance=0; for(i=0;i<nvertex;i++) distance+=dis[i]; return distance; } int main() { int T; char str[100]; scanf("%d",&T); while(T--) { scanf("%d%d",&ncolumn,&nrow); gets(str); init(); generateGraph(); printf("%d\n", prim(0)); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator