POJ 3009 原来写的不知道哪里错了。

今天中午跑出去看3D变形金刚了,不知道为什么,心里面还是有点过意不去。因为没有去实验室。。。。以后出去玩的日子应该没有多少了吧。。其实来这边,还是少点想着玩。

 最近在看深搜的一些题,大体思想还算基本清楚了,下周二还有比赛,我要加紧进度了!!!!!!!

这是POJ 3009的一道题目:  

  意思是给出一个矩阵只有1,0,2,3,而且2和3只有一个,表示start位置和goal位置,然后

Once thrown, the stone keeps moving to the same direction until one of the following occurs:

  • The stone hits a block (Fig. 2(b), (c)).
    • The stone stops at the square next to the block it hit.
    • The block disappears.
  • The stone gets out of the board.
    • The game ends in failure.
  • The stone reaches the goal square.
    • The stone stops there and the game ends in success。    意思就是从2这个位置出发直到遇见1或者走出界限。遇到一,就停止,并且1这个位置的数变为0;知道走到3这个位置,问最少走多少步。。。。。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int road[21][21];
int w,h,rest,step;
int xx[4]={-1,1,0,0};  //上下左右四种走法
int yy[4]={0,0,-1,1};

void dfs(int x,int y)  //深搜一般都用到递归,这个简单的搜索题不要求剪枝优化
{
	if(step>=10)
		return ;
	for(int i=0;i<4;i++)
	{
		int tx=x,ty=y;
		while(1)
		{
          tx=tx+xx[i];
	      ty=ty+yy[i];
	      if(tx<0||tx>=h||ty<0||ty>=w) break;
		  if(road[tx][ty]==3)
		  {
			  step++;
			  if(rest>step)
				  rest=step;
			  step--;
			  return ;
		  }
		   else if(road[tx][ty]==1)
		   {
              tx=tx-xx[i];ty=ty-yy[i];
			  if(tx!=x||ty!=y)
			  {
				  road[tx+xx[i]][ty+yy[i]]=0;
				  step++;
				  dfs(tx,ty);
				  step--;
				  road[tx+xx[i]][ty+yy[i]]=1;
			  }
			  break;
		   }
		  }
			
		}
}

int main()
{
	int x,y,i,j,n,m,flag,sx,sy;
	while(scanf("%d%d",&w,&h)!=EOF&&w)
	{
		step=0;rest=99999999;
		flag=0;
		for(i=0;i<h;i++)
		{
			for(j=0;j<w;j++)
				scanf("%d",&road[i][j]);
		}
		for(i=0;i<h;i++)
		{
			for(j=0;j<w;j++)
			{
              if(road[i][j]==2)
			  {
			      sx=i;sy=j;
			      break;
			  }
			}
		}
       dfs(sx,sy);
	   if(rest>10) printf("-1\n");
	   else printf("%d\n",rest);
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值