Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

做了一天了,总是WA,多余的空格也注意到了。请高手指点一下吧,感激不尽!!!

Posted by 201041842103 at 2012-09-22 18:43:42 on Problem 3026 and last updated at 2012-09-22 18:44:44
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator