牛客假日团队赛2 F.跳跃
链接:
https://ac.nowcoder.com/acm/contest/924/F
题意:
Farmer John为了满足奶牛对美的享受而安装了人工湖。矩形的人工湖分成M行N列(1 <= M <= 30; 1 <= N <= 30)的方形小格子。有些格子有美丽的荷叶,有些有岩石,剩下的格子有的只是美丽的蓝色湖水。
Bessie通过从一片荷叶跳到另一片荷叶上来练习芭蕾。它现在正站在一片荷叶上(看输入数据了解具体位置)。它希望通过在荷叶上跳跃来到达另一片荷叶。它既不能跳到水里也不能跳到岩石上。
只有新手才会感到吃惊:Bessie的跳跃有点类似国际象棋中马那样的移动,在一个方向上移动M1(1 <= M1 <= 30)“格”,然后再在斜方向上移动M2 (1 <= M2 <= 30; M1 != M2)格(或者也许在一个方向上移动M2格,然后在斜方向上移动M1格)。Bessie有时可能有多达8中的跳跃选择。
给出池塘的构造以及Bessie跳跃的形式,找出Bessie从一个位置移动到另一个位置所需的最小的跳跃次数。这个跳跃对于所给的测试数据总是可能的。
思路:
BFS模板,唯一注意的就是下一步的位置。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 50 + 10;
const int MOD = 1e9 + 7;
struct Node
{
int x_, y_;
int step_;
Node(int x, int y, int step):x_(x), y_(y), step_(step){}
};
int n, m, k, t;
int m1, m2, s, e;
int Map[MAXN][MAXN];
int Bfs(int Next[8][2])
{
queue<Node> que;
que.emplace(s, e, 0);
while (!que.empty())
{
Node now = que.front();
que.pop();
for (int i = 0;i < 8;i++)
{
int nx = now.x_+Next[i][0];
int ny = now.y_+Next[i][1];
if (nx < 1 || nx > n || ny < 1 || ny > m)
continue;
if (Map[nx][ny] == 4)
return now.step_+1;
if (Map[nx][ny] != 1)
continue;
que.emplace(nx, ny, now.step_+1);
Map[nx][ny] = 0;
}
}
return 0;
}
int main()
{
cin >> n >> m >> m1 >> m2;
for (int i = 1;i <= n;i++)
{
for (int j = 1; j <= m; j++)
{
cin >> Map[i][j];
if (Map[i][j] == 3)
s = i, e = j;
}
}
int Next[8][2] = {{-m1, -m2}, {-m1, m2}, {-m2, -m1}, {-m2, m1},
{m1, -m2}, {m1, m2}, {m2, -m1}, {m2, m1}};
int res = Bfs(Next);
cout << res << endl;
return 0;
}
牛客假日团队赛2 F.跳跃的更多相关文章
- 牛客假日团队赛5 F 随机数 BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数 (dfs记忆化搜索的数位DP)
链接:https://ac.nowcoder.com/acm/contest/984/F 来源:牛客网 随机数 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
- 牛客假日团队赛10 L 乘积最大 (dp,大数)
链接:https://ac.nowcoder.com/acm/contest/1072/L?&headNav=acm&headNav=acm 来源:牛客网 乘积最大 时间限制:C/C+ ...
- P5200 [USACO19JAN]Sleepy Cow Sorting 牛客假日团队赛6 D 迷路的牛 (贪心)
链接:https://ac.nowcoder.com/acm/contest/993/E 来源:牛客网 对牛排序 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...
- 牛客假日团队赛6 D 迷路的牛 (思维)
链接:https://ac.nowcoder.com/acm/contest/993/D 来源:牛客网 迷路的牛 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...
- 牛客假日团队赛5J 护城河 bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 (凸包的周长)
链接:https://ac.nowcoder.com/acm/contest/984/J 来源:牛客网 护城河 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
- 牛客假日团队赛5 K 金币馅饼 (DP 基础题)
链接:https://ac.nowcoder.com/acm/contest/984/K 来源:牛客网 金币馅饼 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...
- 牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)
链接:https://ac.nowcoder.com/acm/contest/984/L 来源:牛客网 Catch That Cow 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 3 ...
- 洛谷 P2866 [USACO06NOV]糟糕的一天Bad Hair Day 牛客假日团队赛5 A (单调栈)
链接:https://ac.nowcoder.com/acm/contest/984/A 来源:牛客网 题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,00 ...
- 「BZOJ1669」D 饥饿的牛 [Usaco2006 Oct] Hungry Cows 牛客假日团队赛5 (LIS,离散化树状数组)
链接:https://ac.nowcoder.com/acm/contest/984/D 来源:牛客网 饥饿的牛 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...
随机推荐
- BZOJ 1673 [Usaco2005 Dec]Scales 天平:dfs 启发式搜索 A*搜索
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1673 题意: 有n个砝码(n <= 1000),重量为w[i]. 你要从中选择一些砝 ...
- ES设置字段搜索权重——Query-Time Boosting
Query-Time Boosting In Prioritizing Clauses, we explained how you could use the boost parameter at s ...
- dead reckoning variation
Targeting A target is a structure of information that describes the state, and change of state, of a ...
- appium 特殊操作
一.触摸操作 1.driver.tap([坐标],持续点击时间) 除了定位到元素的点击外,也可以通过tab实现坐标的点击 driver.tap(driver.tap([(216,1776)], ...
- 【Lintcode】104.Merge k Sorted Lists
题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- thinkjs,promise
thinkjs是奇舞团开源的一款NodejsMVC框架,该框架底层基于Promise来实现,很好的解决了Nodejs里异步回调的问题. 可参考: http://www.thinkjs.org/ htt ...
- Python3解leetcode Same TreeBinary Tree Level Order Traversal II
问题描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...
- Code:Base64 编码/解码
ylbtech-Code:Base64 编码/解码 1. C#返回顶部 1.编码 byte[] inArray = new byte[msgTxt.Length]; int x; ; x < m ...
- cassandra自启动服务器脚本
新建一个文件. vi /etc/rc.d/init.d/vi cassandra2 #!/bin/sh # chkconfig: # description: service -- # process ...
- lwip 分析一
一.接收端 1.通过ehternetif_input void ethernetif_input(struct netif *netif) { struct ethernetif *etherne ...