POJ3009 Curling 2.0

http://poj.org/problem?id=3009

扔冰球

最开始没看懂示例数据 才发现相邻有墙时不能扔

程序一定要有很好的可读性 要说清楚 不然越改越烦

具体思路:

每撞到墙 墙体会消失 地图在发生改变 所以不能广搜

深度+回溯

因为深搜会搜出所有可能的投掷方案 所以step <= 10作为剪枝

每一个格子有4种方向 4^10 = 10^6绰绰有余

选择一个方向投掷 检测是否越界 检测是否可以投掷 检测是否到终点---->>>.更新终点步数

继续搜索的情况---->>>撞到墙壁 ->击碎墙壁->继续搜索->回溯->修复墙壁->下一个方向

//终于在看了别人的代码后过了 这种混了模拟的题 一定不要慌 不要一堆条件
//条理is so important 不然就要找bug找死(流程图first)
//写得越简单越好 越清晰越好 god!!

复制代码
 1 #include <iostream>
 2 #include <stdio.h>
 3 
 4 using namespace std;
 5 
 6 const int INF = 0x3f3f3f3f;
 7 int N,M;
 8 int goal = INF;
 9 int board[25][25];
10 int d[4][2] = { {-1, 0}, {0, 1}, {1, 0}, {0, -1} };
11 int sx,sy,ex,ey;
12 
13 
14 bool check(int x, int y)//检查是否越界
15 {
16     if (x < 0 || y < 0 || x >= M || y >= N) return false;
17     return true;
18 }
19 
20 void dfs(int step, int x, int y)
21 {
22     int nx, ny;
23     if (step > 10) return ;//剪枝
24     for (int i = 0; i < 4; i++)
25     {
26         nx = x + d[i][0];
27         ny = y + d[i][1];
28         if (!check(nx,ny)) continue;//越界
29         if(board[nx][ny] == 1) continue;//直接遇到墙了
30         while (!board[nx][ny])
31         {
32             nx += d[i][0];
33             ny += d[i][1];
34             if (!check(nx,ny)) break;//如果越界就退出
35         }
36         if (check(nx,ny))//如果没有越界
37         {
38             if (board[nx][ny] == 3)//如果到终点了
39             {
40                 goal = min(goal, step);
41             }
42             if (board[nx][ny] == 1)
43             {
44                 board[nx][ny] = 0;//击碎岩石
45                 dfs(step+1, nx-d[i][0], ny-d[i][1]);
46                 board[nx][ny] = 1;//回溯修复岩石
47             }
48         }
49     }
50 }
51 
52 int main()
53 {
54     freopen("in.txt", "r", stdin);
55     while ( ~scanf("%d%d", &N, &M) )
56     {
57         if (M == 0 && N == 0)   break;
58         for(int i = 0; i < M; i++)
59         {
60             for (int j = 0; j < N; j++)
61             {
62                 scanf( "%d", &board[i][j] );
63                 if (board[i][j] == 2)
64                 {
65                     sx = i;
66                     sy = j;
67                     board[i][j] = 0;
68                 }//起点就是一个起始的地方和0没有区别
69                 if (board[i][j] == 3)
70                 {
71                     ex = i;
72                     ey = j;
73                 }
74             }
75         }
76         goal = INF;
77         dfs(1, sx, sy);
78         if (goal != INF) printf("%d\n", goal);
79         else printf("-1\n");
80     }
81     return 0;
82 }
复制代码

 

posted @   Lorazepam  阅读(276)  评论(6编辑  收藏  举报
编辑推荐:
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
阅读排行:
· “你见过凌晨四点的洛杉矶吗?”--《我们为什么要睡觉》
· C# 从零开始使用Layui.Wpf库开发WPF客户端
· 编程神器Trae:当我用上后,才知道自己的创造力被低估了多少
· C#/.NET/.NET Core技术前沿周刊 | 第 31 期(2025年3.17-3.23)
· 接口重试的7种常用方案!
点击右上角即可分享
微信分享提示