HDOJ-1010 Tempter of the Bone

DFS  深度搜索,常常跟奇偶剪枝一块用

    #include       
    #include      
    #include 
    #include      
    #include           
    #include
	#include         
    using namespace std;
    int sx,sy,ex,ey,n,m,t;
    int flag;
    char map[10][10];
    int vis[10][10];
    int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
    void DFS(int x,int y,int step)
    {
    	if(flag)
    	return;
    	if(x==ex&&y==ey&&step==t)
    	{
    		flag=1;return;
		}
		int temp=t-step-abs(x-ex)-abs(y-ey);//奇偶性剪枝 
		if(temp<0||temp&1)
		return;
		int i,dx,dy;
		for(i=0;i<4;i++)
		{
			dx=x+dir[i][0];
			dy=y+dir[i][1];
			if(dx<0||dx>=n||dy<0||dy>=m)
			continue;
			if(map[dx][dy]!='X'&&vis[dx][dy]==0)
			{
				vis[dx][dy]=1;
				step=step+1;
				DFS(dx,dy,step);
				if(flag) return;
				vis[dx][dy]=0;
			}
		}
		return ;
	}
	int main()
	{
		while(~scanf("%d%d%d",&n,&m,&t))
		{
			if(n==0&&m==0&&t==0)
			break;
			int j,i,wall=0;
			int flag=0;
			memset(vis,0,sizeof(vis));
			for(i=0;i

 

你可能感兴趣的:(DFS,搜索,acm)