poj3009

  1 #include <iostream>
  2
  3 using namespace std;
  4
  5 int w, h;
  6 int MAP[41][41];
  7 int sx, sy, ex, ey;
  8 int res;
  9 int dx[4] = {
 10     1, -1, 0, 0
 11 };
 12 int dy[4] = {
 13     0, 0, 1, -1
 14 };
 15
 16 int runing(int &x, int &y, int dir)
 17 {
 18     for(;;)
 19     {
 20         int nx = x + dx[dir];
 21         int ny = y + dy[dir];
 22         if(nx >= 0 && nx < w && ny >= 0 && ny < h)
 23         {
 24             if(MAP[ny][nx] == 1)
 25             {
 26                 return 1;
 27             }
 28             else if(nx == ex && ny == ey)
 29             {
 30                 return 0;
 31             }
 32         }
 33         else
 34         {
 35             return -1;
 36         }
 37         x = nx;
 38         y = ny;
 39     }
 40 }
 41
 42 void dfs(int x, int y, int sum)
 43 {
 44     int nx, ny;
 45     //´óÓÚ10£¬¼ôÖ¦
 46     if(sum > 10)
 47         return ;
 48
 49
 50     //Ëĸö·½Ïò½øÐÐËÑÑ°
 51     for(int i = 0; i < 4; ++i)
 52     {
 53         nx = x + dx[i];
 54         ny = y + dy[i];
 55         if(nx >= 0 && nx < w && ny >= 0 && ny < h)
 56         {
 57             if(MAP[ny][nx] == 1)
 58             {
 59                 continue;
 60             }
 61         }
 62         else
 63             continue;
 64         nx = x; ny = y;
 65         int temp = runing(nx, ny, i);
 66         //ÍùÕâ¸ö·½ÏòÅöµ½µÄÊÇǽ
 67         if(temp == 1)
 68         {
 69             MAP[ny+dy[i]][nx+dx[i]] = 0;
 70             dfs(nx, ny, sum+1);
 71             MAP[ny+dy[i]][nx+dx[i]] = 1;
 72
 73         }
 74         //ÍùÕâ¸ö·½Ïò³ö½çÁË
 75         else if(temp == -1)
 76         {
 77             continue;
 78         }
 79         //ÍùÕâ¸ö·½Ïòµ½´ïÁËÖÕµã
 80         else if(temp == 0)
 81         {
 82              if(res > sum)
 83                  res = sum;
 84         }
 85     }
 86
 87 }
 88
 89 void solver()
 90 {
 91     res = 11;
 92     dfs(sx, sy, 1);
 93
 94 }
 95 int main()
 96 {
 97     while(cin >> w >> h)
 98     {
 99         if(w == 0 && h == 0)
100             break;
101         //input
102         for(int i = 0; i < h; ++i)
103         {
104             for(int j = 0; j < w; ++j)
105             {
106                 cin >> MAP[i][j];
107                 if(MAP[i][j] == 2)
108                 {
109                     sx = j;
110                     sy = i;
111                 }
112                 if(MAP[i][j] == 3)
113                 {
114                     ex = j;
115                     ey = i;
116                 }
117             }
118         }
119         //solver
120         solver();
121         //output
122         if(res > 10)
123             cout << -1 << endl;
124         else
125             cout << res << endl;
126     }
127
128     return 0;
129 } 

时间: 2024-08-05 11:13:20

poj3009的相关文章

POJ3009——Curling 2.0(DFS)

Curling 2.0 DescriptionOn 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

poj3009(Curling 2.0)

题目地址:Curling 2.0 题目大意: 一项在冰上网格的运动,在网格中2为起点,3为终点,1代表着有墙阻挡,0代表空白处.运动员从2点可以沿着上下左右四个方向出发,直至碰到墙才会改变自己的方向,但是注意碰到墙之后,此墙会消失变为空白处,意思是可以下次通过. 解题思路: 因为就四个方向可以枚举深搜. 代码; 1 #include <algorithm> 2 #include <iostream> 3 #include <sstream> 4 #include <

深度优先搜索DFS (poj2386,poj1979, poj3009,poj1321,aoj0033,aoj0118)

深度优先搜索(DFS) 往往利用递归函数实现(隐式地使用栈). 深度优先从最开始的状态出发,遍历所有可以到达的状态.由此可以对所有的状态进行操作,或列举出所有的状态. 1.poj2386 Lake Couting 题意:八连通被认为连接在一起,求总共有多少个水洼? Sample Input: 10 12 W........WW. .WWW.....WWW ....WW...WW. .........WW. .........W.. ..W......W.. .W.W.....WW. W.W.W.

[DFS] &amp; [BFS] poj1979 poj3009 poj3669

都比较简单,直接贴代码吧. poj1979 DFS 题目大意:给你一个二维数组,.表示可以到达,#表示障碍,@表示起始位置,问你能到达的最大地点有多少个,每次只能走上下左右 #include <iostream> #include <cstdio> #include <cstring> using namespace std; int n, m, sx, sy, ans; int pd[30][30]; char maze[30][30]; int dx[4] = {0

poj3009 Curling 2.0(很好的题 DFS)

https://vjudge.net/problem/POJ-3009 做完这道题,感觉自己对dfs的理解应该又深刻了. 1.一般来说最小步数都用bfs求,但是这题因为状态记录很麻烦,所以可以用dfs. 2.在用dfs的时候,mp时一个全局变量,对于平等的走法,每一个走法结束后一定要状态复原!!!(也就是代码36-38行)否则会对其他走法产生影响. 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #

poj3009 Curling 2.0(DFS回溯)

题目大意是:给你一个球,刚开始是静止的,可以通过触碰给他一个初速度,一旦球运动起来就不会停止,除非遇到一个石头.遇到石头以后小球就原地停止了,然后石头就被砸碎了.小球越界就算失败了.问你最少进行多少次操作,可以让小球到达终点.题中还有一个要求,如果超过10步,就算失败了. 这道题目做了好久啊.可能方法太麻烦了. #include<stdio.h> #include<string.h> int map[105][105],si,sj,ei,ej,w,h; //1 right    2

poj3009 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

poj3009 Curling 2.0 深搜

PS:以前看到题目这么长就没写下去了.今天做了半天,没做出来.准备看题解,打开了网站都忍住了,最后还是靠自己做出来的.算是一点进步吧. 分析: 题目的意思没明白或者理解有偏差都没办法做题.看样例3和样例4,数据差不多的,但是一个输出4,但是另外的一个却是-1.再去看题目就会发现,题目的意思是在撞碎石头之前必须走一个为值0的格子.我理解为需要加速.对样例4,答案4是这样出来的:初始位置为(1,3),第一步是到达(1,2),并且使得(1,1)点的值为0(撞碎了这里的石头,0代表可以通行):第二步是到

POJ3009:Curling 2.0(dfs)

http://poj.org/problem?id=3009 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. The