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

有没有哪位大神可以帮我看一下为什么超时......

Posted by windsin at 2014-01-27 23:36:22 on Problem 3669
#include<stdio.h>
#include<queue>
#include<memory.h>
using namespace std;
typedef struct
{
	int i,j,t;
}P;
int main()
{
	int shortest=-1;
	int board[301][301];
	bool visited[301][301];
	memset(visited,0,sizeof(visited));
	memset(board,-1,sizeof(board));
	queue<P> q;
	int M;
	scanf("%d",&M);
	int i,j,t;
	for(int ti=0;ti<M;ti++)
	{
		scanf("%d%d%d",&j,&i,&t);
		if(board[i][j]==-1)
			board[i][j]=t;
		if(i>0)
			if(board[i-1][j]==-1)
			board[i-1][j]=t;
		if(i<300)
			if(board[i+1][j]==-1)
			board[i+1][j]=t;
		if(j>0)
			if(board[i][j-1]==-1)
			board[i][j-1]=t;
		if(j<300)
			if(board[i][j+1]==-1)
			board[i][j+1]=t;
	}
	P tp,tp2;
	tp.i=tp.j=tp.t=0;
	visited[0][0]=1;
	q.push(tp);
	while(!q.empty())
	{
		tp=q.front();
		q.pop();
		if(board[tp.i][tp.j]==-1)	
			{
				shortest=tp.t;
				break;
		}
		else
		{
			if(tp.i>0&&(board[tp.i-1][tp.j]-1>tp.t||board[tp.i-11][tp.j]==-1)&&!visited[tp.i-1][tp.j])
			{
				tp2.i=tp.i-1;
			    tp2.j=tp.j;
				tp2.t=tp.t+1;
				q.push(tp2);
				visited[tp.i-1][tp.j]=1;
			}
			if(tp.j>0&&(board[tp.i][tp.j-1]-1>tp.t||board[tp.i][tp.j-1]==-1)&&!visited[tp.i][tp.j-1])
			{
				tp2.i=tp.i;
			    tp2.j=tp.j-1;
				tp2.t=tp.t+1;
				q.push(tp2);
				visited[tp.i][tp.j-1]=1;
			}
			if(tp.i<300&&(board[tp.i+1][tp.j]-1>tp.t||board[tp.i+1][tp.j]==-1)&&!visited[tp.i+1][tp.j])
			{
				tp2.i=tp.i+1;
			    tp2.j=tp.j;
				tp2.t=tp.t+1;
				q.push(tp2);
				visited[tp.i+1][tp.j]=1;
			}
			if(tp.j<300&&(board[tp.i][tp.j+1]-1>tp.t||board[tp.i][tp.j+1]==-1)&&!visited[tp.i][tp.j+1])
			{
				tp2.i=tp.i;
			    tp2.j=tp.j+1;
				tp2.t=tp.t+1;
				q.push(tp2);
				visited[tp.i][tp.j+1]=1;
			}
		}
	}
	if(shortest==-1)
		printf("%d\n",-1);
	else
	{
		printf("%d\n",shortest);
	}
}

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