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 y05zh at 2007-08-17 16:33:02 on Problem 3009
#include<iostream>
using namespace std;
int  min;
int dir[4][2]={{1,0},{0,-1},{-1,0},{0,1}
};
int w,h;
int map[30][30];
int x1,y1,x2,y2;
int sum=0;
int dfs(int x,int y)
{	
	int i;
	int xx,yy;
	int xxx,yyy;
	if(sum>10)return 0;
	if(sum>min)return 0;
	if(map[x][y]==3)
	{
		sum++;
		if(sum<min)
		{
			min=sum;
		}
		sum--;
		return 0;
	}
	else
	{
		for(i=0;i<4;i++)
		{
			xx=x+dir[i][0];
			yy=y+dir[i][1];	
			
			if(xx>=0&&xx<h&&yy>=0&&yy<w)
			{
				if(map[xx][yy]==1)continue;
				if(map[xx][yy]==3)
				{
					sum++;
					if(sum<min)
					{
						min=sum;

					}
					sum--;
					continue;
				}
				if(map[xx][yy]==0)
				{
					xxx=xx+dir[i][0];
					yyy=yy+dir[i][1];
					while(xxx>=0&&xxx<h&&yyy>=0&&yyy<w)
					{
						xxx=xx+dir[i][0];
						yyy=yy+dir[i][1];
						if(map[xxx][yyy]==1)
						{
							map[xxx][yyy]=0;
							sum++;
							dfs(xx,yy);
							map[xxx][yyy]=1;
							sum--;
							break;
						}
						if(map[xxx][yyy]==3)
						{
							sum++;
							if(sum<min)
							{
								min=sum;
								break;
							}
							sum--;
						}
						xx=xxx;
						yy=yyy;
					}
				}
			}
		}
	}
	return 0;
}
			/*	if(xx>=0&&xx<h&&yy>=0&&yy<w)
			{
			if(map[xx][yy]==0||map[xx][yy]==3)
			{
			switch (i)
			{
			case 0:
			for(j=xx;j<h;j++)
			{
			if(map[j][yy]==3)
			{
			sum++;
			if(sum<min)
			{
			min=sum;
			}
			break;
			}
			if(map[j][yy]==1)
			{
			map[j][yy]=0;
			sum++;
			xxx=j-1;
			dfs(xxx,yy);
			map[j][yy]=1;
			//sum--;
			break;
			}
			}
			break;
			case 1:
			for(j=yy;j>=0;j--)
			{
			if(map[xx][j]==3)
			{
			sum++;
			if(sum<min)
			{
			min=sum;
			}
			break;
			}
			if(map[xx][j]==1)
			{
			map[xx][j]=0;
			sum++;
			yyy=j+1;
			dfs(xx,yyy);
			map[xx][j]=1;
			//sum--;
			break;
			}
			}
			break;
			case 2:
			for(j=xx;j>=0;j--)
			{
			if(map[j][yy]==3)
			{
			sum++;
			if(sum<min)
			{
			min=sum;
			}
			break;
			}
			if(map[j][yy]==1)
			{
			map[j][yy]=0;
			sum++;
			xxx=j+1;
			dfs(xxx,yy);
			map[j][yy]=1;
			//sum--;
			break;
			}
			}
			break;
			case 3:
			for(j=yy;j<w;j++)
			{
			if(map[xx][j]==3)
			{
			sum++;
			if(sum<min)
			{
			min=sum;
			}
			break;
			}
			if(map[xx][j]==1)
			{
			map[xx][j]=0;
			sum++;
			yyy=j-1;
			dfs(xx,yyy);
			map[xx][j]=1;
			//sum--;
			break;
			}
			}
			break;
			}
			}//if
			//sum--;
			}			
			}
			}
			
sum--;
return 0;

}*/
int main()
{
	int i,j;
	while(true)
	{
		cin>>w>>h;//y,x
		if(w==0&&h==0)break;
		for(i=0;i<h;i++)
		{
			for(j=0;j<w;j++)
			{
				cin>>map[i][j];
				if(map[i][j]==2)
				{
					x1=i;
					y1=j;
					map[i][j]=0;
				}
			}
		}
		sum=0;
		min=11;
		dfs(x1,y1);
		if(min>10)
		{
			min=-1;
		}
		cout<<min<<endl;
		
		
		
		
		
	}
	
	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