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 774550615 at 2012-08-06 16:38:38 on Problem 1376
这是我的代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int m,n;
int map[50][55];
int sign[50][55][4];
int direct[4][2]={{3,1},{0,2},{1,3},{2,0}};
int f[4][3][2]={{{-1,0},{-2,0},{-3,0}},{{0,1},{0,2},{0,3}},
{{1,0},{2,0},{3,0}},{{0,-1},{0,-2},{0,-3}}};
struct node
{
	int xi,xj;
	int step;
	int dir;
}u,v,w;
int si,sj,di,dj;
int f2[4][2]={{0,0},{-1,0},{0,-1},{-1,-1}};
void bfs(int e)
{
	int i,j;
	int x,y;
	int k;
	queue<node>q;
	w.xi=si;
	w.xj=sj;
	w.step=0;
	w.dir=e;
	sign[si][sj][e]=1;
    q.push(w); 
    while(!q.empty())
    {
    	u=q.front();
    	q.pop();
    	//cout<<u.xi<<" "<<u.xj<<" "<<u.dir<<" "<<u.step<<endl;
    	if(u.xi==di&&u.xj==dj)
    	{
    		printf("%d\n",u.step);
    		return ;
    	}
    	bool check;
    	for(i=0;i<3;i++)
    	{
    		check=false;
    		v.xi=u.xi+f[u.dir][i][0];
    		v.xj=u.xj+f[u.dir][i][1];
    		v.step=u.step+1;
    		v.dir=u.dir;
    		if(v.xi<=0||v.xi>=m||v.xj<=0||v.xj>=n)break;
			if(sign[v.xi][v.xj][v.dir])break;
			if(map[v.xi][v.xj])break;
    		for(j=0;j<4;j++)
    		{
		        x=v.xi+f2[j][0];
		        y=v.xj+f2[j][1];
		       	if(x<0||x>=m||y<0||y>=n)
		       	{
		       		check=true;
		       		break;
		       	}
		       	if(map[x][y])
		       	{
		       	     check=true;
					 break;	
		       	}
    		}
    		if(check)break;
    		sign[v.xi][v.xj][v.dir]=1; 
    		q.push(v);
    	}
    	for(k=0;k<2;k++)
    	{
    		v.xi=u.xi;
    		v.xj=u.xj;
    		v.step=u.step+1;
    		v.dir=direct[u.dir][k];
    		if(sign[v.xi][v.xj][v.dir])continue;
    		sign[v.xi][v.xj][v.dir]=1;
    		q.push(v);
    	}
    }
    printf("-1\n");
}
int main()
{
	int i,j;
	int e;
	bool foud;
	while(scanf("%d%d",&m,&n))
	{ 
		if(m==0&&n==0)break;
		foud=false;
		for(i=0;i<m;i++)
		{
			for(j=0;j<n;j++)
			{
				scanf("%d",&map[i][j]);
			}
		}
		char s[20];
		scanf("%d%d%d%d",&si,&sj,&di,&dj);
		int x,y;
		for(j=0;j<4;j++)
		{
	        x=si+f2[j][0];
	        y=sj+f2[j][1];
	       	if(x<0||x>=m||y<0||y>=n)
	       	{
	       		foud=true;
	       		break;
	       	}
	       	if(map[x][y])
	       	{
	       	     foud=true;
				 break;	
	       	}
		}
		scanf("%s",s);
		if(strcmp(s,"north")==0)
		  e=0;
		else if(strcmp(s,"east")==0)
	      e=1;
	    else if(strcmp(s,"south")==0)
	      e=2;
	    else if(strcmp(s,"west")==0)
	      e=3;
		memset(sign,0,sizeof(sign));
	    if(si==0||si==m||sj==0||sj==n)
	    {
	    	foud=true;
	    }
	    if(di==0||di==m||dj==0||dj==n)
	    {
	    	foud=true;
	    }
	    if(!foud)bfs(e);
        else printf("-1\n");
	}
	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