poj3009
2016-07-02 21:28
344 查看
本题目唯一需要注意的一点就是终点只要在冰壶的移动路径上就可以,不用最终一定要落在冰壶上。
而且因为难以保存中间的矩阵状态,于是广搜似乎难以实现。
reference:
[1]Poj3009冰壶游戏
而且因为难以保存中间的矩阵状态,于是广搜似乎难以实现。
#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冰壶游戏
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2159 Ancient Cipher
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- [数论]poj2635__The Embarrassed Cryptographer
- [二分图匹配]poj2446__Chessboard
- POJ1050 最大子矩阵和
- 用单调栈解决最大连续矩形面积问题