分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

POJ 3009 Curling 2.0

发布时间:2023-11-01 15:52责任编辑:傅花花关键词:url

题目链接:https://vjudge.net/problem/POJ-3009

题目大意:

  冰壶比赛。

给定一个 n * m 的矩阵A,其中:

  1. A[i][j] == 0,代表通路。
  2. A[i][j] == 1,代表冰块。
  3. A[i][j] == 2,代表冰壶起点。
  4. A[i][j] == 3,代表冰壶所要到达的点。

冰壶只能往上下左右四个方向移动,且碰到冰块才会停下来,同时冰块也会碎掉,冰壶也随之停在原地,冰壶一旦打出界,比赛就结束了,冰壶只要在10步以内经过终点,就算成功。

求每场冰壶比赛的最小移动步数。

分析:

  dfs基础题,读懂题目最重要。

代码如下:

 ?1 #pragma GCC optimize("Ofast") ?2 #include <bits/stdc++.h> ?3 using namespace std; ?4 ???5 #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0); ?6 #define Rep(i,n) for (int i = 0; i < (n); ++i) ?7 #define For(i,s,t) for (int i = (s); i <= (t); ++i) ?8 #define rFor(i,t,s) for (int i = (t); i >= (s); --i) ?9 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i) 10 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i) 11 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i) 12 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) 13 ??14 #define pr(x) cout << #x << " = " << x << " ?" 15 #define prln(x) cout << #x << " = " << x << endl 16 ??17 #define LOWBIT(x) ((x)&(-x)) 18 ??19 #define ALL(x) x.begin(),x.end() 20 #define INS(x) inserter(x,x.begin()) 21 ??22 #define ms0(a) memset(a,0,sizeof(a)) 23 #define msI(a) memset(a,inf,sizeof(a)) 24 #define msM(a) memset(a,-1,sizeof(a)) 25 ?26 #define MP make_pair 27 #define PB push_back 28 #define ft first 29 #define sd second 30 ??31 template<typename T1, typename T2> 32 istream &operator>>(istream &in, pair<T1, T2> &p) { 33 ????in >> p.first >> p.second; 34 ????return in; 35 } 36 ??37 template<typename T> 38 istream &operator>>(istream &in, vector<T> &v) { 39 ????for (auto &x: v) 40 ????????in >> x; 41 ????return in; 42 } 43 ??44 template<typename T1, typename T2> 45 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) { 46 ????out << "[" << p.first << ", " << p.second << "]" << "\n"; 47 ????return out; 48 } 49 ??50 typedef long long LL; 51 typedef unsigned long long uLL; 52 typedef pair< double, double > PDD; 53 typedef pair< int, int > PII; 54 typedef pair< LL, LL > PLL; 55 typedef set< int > SI; 56 typedef vector< int > VI; 57 typedef map< int, int > MII; 58 typedef vector< LL > VL; 59 typedef vector< VL > VVL; 60 const double EPS = 1e-10; 61 const int inf = 1e9 + 9; 62 const LL mod = 1e9 + 7; 63 const int maxN = 1e5 + 7; 64 const LL ONE = 1; 65 const LL evenBits = 0xaaaaaaaaaaaaaaaa; 66 const LL oddBits = 0x5555555555555555; 67 ?68 int n, m, ans; 69 int M[23][23]; 70 PII S, G; 71 ?72 // 从 x 点到 y 点是否经过目标点 ?73 bool isPassed(PII x, PII y) { 74 ????if(x.ft == y.ft) 75 ????????return x.ft == G.ft && min(x.sd, y.sd) <= G.sd && G.sd <= max(x.sd, y.sd); 76 ????else ?77 ????????return x.sd == G.sd && min(x.ft, y.ft) <= G.ft && G.ft <= max(x.ft, y.ft); 78 } ?79 ?80 // 找到 x 最左边的可到达点 ?81 PII leftMost(PII x) { 82 ????while(x.sd > 0 && M[x.ft][x.sd - 1] != 1) --x.sd; 83 ????return x; 84 } 85 ?86 // 找到 x 最上边的可到达点 ?87 PII upMost(PII x) { 88 ????while(x.ft > 0 && M[x.ft - 1][x.sd] != 1) --x.ft; 89 ????return x; 90 } 91 ?92 // 找到 x 最右边的可到达点 ?93 PII rightMost(PII x) { 94 ????while(x.sd < m - 1 && M[x.ft][x.sd + 1] != 1) ++x.sd; 95 ????return x; 96 } 97 ?98 // 找到 x 最下边的可到达点 ?99 PII downMost(PII x) {100 ????while(x.ft < n - 1 && M[x.ft + 1][x.sd] != 1) ++x.ft;101 ????return x;102 }103 104 // 以数组方式存储函数 105 PII (*func[4])(PII) = {leftMost, upMost, rightMost, downMost};106 int dx[] = {0, -1, 0, 1};107 int dy[] = {-1, 0, 1, 0};108 109 void dfs(PII x, int cnt) {110 ????if(cnt > 10) return;111 ????if(x == G) ans = min(ans, cnt);112 ????113 ????Rep(i, 4) {114 ????????PII tmp = func[i](x);115 ????????// 是否经过G 116 ????????if(isPassed(x, tmp)) ans = min(ans, cnt + 1);117 ????????// 如果能移动到不同地方且不再边界上 118 ????????if(x != tmp && tmp.ft + dx[i] >= 0 && tmp.ft + dx[i] < n && tmp.sd + dy[i] >= 0 && tmp.sd + dy[i] < m) {119 ????????????M[tmp.ft + dx[i]][tmp.sd + dy[i]] = 0;120 ????????????dfs(tmp, cnt + 1);121 ????????????M[tmp.ft + dx[i]][tmp.sd + dy[i]] = 1;122 ????????}123 ????}124 }125 126 int main(){127 ????INIT();128 ????while(cin >> m >> n) {129 ????????if(m == 0) break;130 ????????ans = inf;131 ????????Rep(i, n) {132 ????????????Rep(j, m) {133 ????????????????cin >> M[i][j];134 ????????????????if(M[i][j] == 2) S = MP(i, j);135 ????????????????if(M[i][j] == 3) G = MP(i, j);136 ????????????}137 ????????}138 ????????dfs(S, 0);139 ????????if(ans > 10) ans = -1;140 ????????cout << ans << endl;141 ????}142 ????return 0;143 }
View Code

POJ 3009 Curling 2.0

原文地址:https://www.cnblogs.com/zaq19970105/p/10803395.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved