poj 3009 -- Curling 2.0
2014-08-03 09:57
337 查看
Curling 2.0
Description
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. 1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
View Code
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11389 | Accepted: 4818 |
On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. 1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
/*====================================================================== * Author : kevin * Filename : Curling2.cpp * Creat time : 2014-08-02 11:17 * Description : ========================================================================*/ #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #define M 25 #define INF 0x7f7f7f7f using namespace std; int grap[M][M]; int w,h; int sx,sy,ex,ey,ans; int d[4] = {0,1,2,3}; void DFS(int x,int y,int cnt,int d) { int i,j; if(cnt > 10) return ; if(x == ex && y == ey){ if(ans > cnt){ ans = cnt; } return ; } if(d == 0){ if(grap[x-1][y] == 1) return; for(i = x; i >= 1; i--){ if(grap[i][y] == 1){ grap[i][y] = 0; for(j = 0; j < 4; j++){ //if(d != j) DFS(i+1,y,cnt+1,j); } grap[i][y] = 1; break; } if(grap[i][y] == 3) DFS(i,y,cnt,d); } } if(d == 2){ if(grap[x+1][y] == 1) return; for(i = x; i <= h; i++){ if(grap[i][y] == 1){ grap[i][y] = 0; for(j = 0; j < 4; j++){ //if(d != j) DFS(i-1,y,cnt+1,j); } grap[i][y] = 1; break; } if(grap[i][y] == 3) DFS(i,y,cnt,d); } } if(d == 1){ if(grap[x][y+1] == 1) return; for(i = y; i <= w; i++){ if(grap[x][i] == 1){ grap[x][i] = 0; for(j = 0; j < 4; j++){ //if(d != j) DFS(x,i-1,cnt+1,j); } grap[x][i] = 1; break; } if(grap[x][i] == 3) DFS(x,i,cnt,d); } } if(d == 3){ if(grap[x][y-1] == 1) return; for(i = y; i >= 1; i--){ if(grap[x][i] == 1){ grap[x][i] = 0; for(j = 0; j < 4; j++){ //if(d != j) DFS(x,i+1,cnt+1,j); } grap[x][i] = 1; break; } if(grap[x][i] == 3) DFS(x,i,cnt,d); } } } int main(int argc,char *argv[]) { while(scanf("%d%d",&w,&h)!=EOF && w+h){ memset(grap,-1,sizeof(grap)); for(int i = 1; i <= h; i++){ for(int j = 1; j <= w; j++){ scanf("%d",&grap[i][j]); if(grap[i][j] == 2){ sx = i; sy = j; } if(grap[i][j] == 3){ ex = i; ey = j; } } } ans = INF; for(int i = 0; i < 4; i++){ DFS(sx,sy,1,d[i]); } if(ans == INF){ printf("-1\n"); } else{ printf("%d\n",ans); } } return 0; }
View Code
相关文章推荐
- POJ 3009 Curling 2.0
- POJ 3009 Curling 2.0 (dfs)
- poj 3009 Curling 2.0 (dfs )
- poj3009 Curling 2.0 DFS搜索
- POJ 3009 Curling 2.0(棋盘DFS)
- POJ 3009 Curling 2.0(DFS)
- POJ - 3009 Curling 2.0解题报告(dfs)
- POJ 3009 Curling 2.0
- POJ 3009 Curling 2.0(DFS深度优先搜索)
- poj 3009 Curling 2.0(dfs)
- poj 3009 Curling 2.0
- POJ 3009 Curling 2.0
- poj 3009 Curling 2.0(冰壶 直到遇到障碍才停下 dfs)
- POJ 3009 Curling 2.0
- poj 3009 Curling 2.0
- POJ3009——Curling 2.0(DFS)
- POJ 3009 深搜 Curling 2.0
- Curling 2.0 (POJ 3009, 深度优先搜索)
- POJ 3009 Curling 2.0 【dfs 而不是 bfs】
- poj 3009 Curling 2.0 dfs