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 2017777 at 2017-08-16 20:37:21 on Problem 3009 and last updated at 2017-08-16 20:39:41
#include<cstdlib>//注意w是列,h是行 
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int dx[4]={0,-1,1,0},dy[4]={-1,0,0,1},w,h,a[22][22],sx,sy,ex,ey,minn; 
void dfs(int x,int y,int step)
{
	if(step>=10)return;
	for(int i=0;i<4;i++)
	{
		int xx=x+dx[i];
		int yy=y+dy[i];
		if(a[xx][yy]==1)continue;
		while(a[xx][yy]==0)
		{
			xx+=dx[i];
			yy+=dy[i];
		} 
		if(xx>=0&&xx<h&&yy>=0&&yy<w)
		{
			if(a[xx][yy])
			a[xx][yy]=0;
			dfs(xx-dx[i],yy-dy[i],step+1);//遇障碍物后退 
			a[xx][yy]=1; 
		}
		if(xx==ex&&yy==ey)
		{
			if(step+1<minn)//step+1 
			minn=step+1;
		}
	}
}
int main()
{
	while(scanf("%d%d",&w,&h))
	{
		if(w==0&&h==0)return 0;
		minn=0x3f3f3f3f;
		memset(a,0,sizeof(a)); 
		for(int i=0;i<h;i++)
		{
			for(int j=0;j<w;j++)
			{
				scanf("%d",&a[i][j]);
				if(a[i][j]==2)
				{
					sx=i;
					sy=j;
					a[sx][sy]=0;
				}
				if(a[i][j]==3)
				{
					ex=i;
					ey=j;
				}
			}
		}
		dfs(sx,sy,0);
		if(minn<0x3f3f3f3f)
		printf("%d\n",minn);
		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