今天中午跑出去看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;
}