您的位置:首页 > 其它

poj3009

2016-07-02 21:28 344 查看
本题目唯一需要注意的一点就是终点只要在冰壶的移动路径上就可以,不用最终一定要落在冰壶上。

而且因为难以保存中间的矩阵状态,于是广搜似乎难以实现。

#include <iostream>
#include <stdio.h>
using namespace std;
#define INF 100000000

int W,H;
int M[32][32];
int minstep = INF;
int sx,sy,ex,ey;
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,-1,1};
void dfs(int step,int posx,int posy){
if(step==10){
return;
}
for(int i=0;i<4;i++){
int newx = posx + dx[i];
int newy = posy + dy[i];
if(newx>=0 && newx<H && newy>=0 && newy<W && M[newx][newy]!=1){
while(newx>=0 && newx<H && newy>=0 && newy<W && M[newx][newy]==0){
newx = newx + dx[i];
newy = newy + dy[i];
}
if(newx<0 || newx>=H || newy<0 || newy>=W){
if( (newx-dx[i] == ex) && (newy-dy[i] == ey) && (step+1)<minstep )minstep = step+1;
continue;
}
else if(newx==ex && newy == ey){
if((step+1)<minstep) minstep = step+1;
return;
}else{
M[newx][newy] = 0;
newx -= dx[i];
newy -= dy[i];
dfs(step+1,newx,newy);
M[newx+dx[i]][newy+dy[i]] = 1;
}
}else{
continue;
}
}
}
int main(int argc, const char * argv[]) {
cin>>W>>H;
int i,j;

while(W!=0 || H!=0){
for(i=0;i<H;i++){
for(j=0;j<W;j++){
cin>>M[i][j];
if(M[i][j]==2) { M[i][j] = 0; sx = i ; sy = j ; }
if(M[i][j]==3) { ex = i ; ey = j ; }
}
}
dfs(0,sx,sy);
if(minstep == INF)minstep = -1;
cout<<minstep<<endl;
cin>>W>>H;
minstep = INF;
}
return 0;
}


reference:

[1]Poj3009冰壶游戏
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj