题目链接:点击打开链接
题目大意:跑毒,跑到安全区,每个地方有敌人,输出路线经过的最少敌人的数量;-1是起点。 -2是安全区

输入
5
6 6 0 -2 3
4 2 1 2 1
2 2 8 9 7
8 1 2 1 -1
9 7 2 1 2

输出
9
输入

5
62 33 18 -2 85
85 73 69 59 83
44 38 84 96 55
-1 11 90 34 50
19 73 45 53 95
输出
173

深搜,或广搜。还有用最短路径做的。

DFS超时代码
好好理解那个dfs里面的参数
#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f; int sx, sy, ex, ey, n, ans;
int mp[120][120];
int vis[120][120];
int des[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; bool judge(int x, int y) {//越界
if(x < 0 || x >= n || y < 0 || y >= n)
return false;
if(vis[x][y])//访问过
return false;
return true; //竟然忘写了
} void dfs(int x, int y, int cost) {
if(mp[x][y] == -2) {
ans = min(ans, cost);//到达安全区,取最少的敌人数
return;
}
for(int i = 0; i < 4; i++) {//四个方向遍历
int tempx = x + des[i][0];
int tempy = y + des[i][1];
if(judge(tempx, tempy)) {
vis[tempx][tempy] = 1;//标记
dfs(tempx, tempy, cost+mp[tempx][tempy]);//搜,注意那个重要参数
vis[tempx][tempy] = 0;//回溯
}
}
} int main() {
while(~scanf("%d", &n)) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
scanf("%d", &mp[i][j]);
if(mp[i][j] == -1) {//记录
sx = i;
sy = j;
}
}
}
ans = INF;//因为可能有好几条路
memset(vis, 0, sizeof(vis));
dfs(sx, sy, 0);//这里注意,dfs的特点。参数中有一个是将要累加得到的结果
printf("%d\n", ans+2);//因为最后的-2也加了
}
}
BFS+优先级队列

可能会有疑问,为什么得用优先级队列,我看了几道题,感觉涉及时间(权)不同的,用优先级队列。而对于一般的没什么和值有关的,就不需要优先级队列。

AC代码
#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f; int n;
int mp[120][120];
int vis[120][120];
int des[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; bool judge(int x, int y) {
if(x < 0 || x >= n || y < 0 || y >= n)
return false;
if(vis[x][y])
return false;
return true;
} struct node{
int x;
int y;
int w;
bool friend operator < (node a, node b) {//排序规则
return a.w > b.w;
}
}Node, temptop; int bfs(int x, int y) {
int ans = -1;//结果
Node.x = x;
Node.y = y;
Node.w = 0;
priority_queue<node> pq;
pq.push(Node);
vis[x][y] = 1;
while(!pq.empty()) {
temptop = pq.top();
pq.pop();
for(int i = 0; i < 4; i++) {
Node.x = temptop.x + des[i][0];
Node.y = temptop.y + des[i][1];
if(judge(Node.x, Node.y)) {
if(mp[Node.x][Node.y] == -2) {//到了终点,肯定是最优解。
ans = temptop.w;
break;
}
vis[Node.x][Node.y] = 1;//访问过
Node.w = temptop.w + mp[Node.x][Node.y];//累加指
pq.push(Node);//入队
}
}
if(ans != -1)
break;
}
return ans;
} int sx, sy;
int main() {
while(~scanf("%d", &n)) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
scanf("%d", &mp[i][j]);
if(mp[i][j] == -1) {
sx = i;
sy = j;
}
}
}
memset(vis, 0, sizeof(vis));
printf("%d\n", bfs(sx, sy));
}
}
BFS+ 最短路 
思路很灵活,没见过这样的
AC代码
#include<bits/stdc++.h>
using namespace std;
const int MAX = 120;
const int INF = 0x3f3f3f3f; int n;
int mp[120][120];
int d[120][120];//距离数组
int des[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; bool judge(int x, int y) {
if(x < 0 || x >= n || y < 0 || y >= n)
return false;
return true;
} struct node{
int x;
int y;
}Node, temptop; int sx, sy, ex, ey;
int bfs(int x, int y) {
int ans = -1;
Node.x = x;
Node.y = y;
queue<node> pq;
pq.push(Node);
d[x][y] = 0;
while(!pq.empty()) {
temptop = pq.front();
pq.pop();
for(int i = 0; i < 4; i++) {
Node.x = temptop.x + des[i][0];
Node.y = temptop.y + des[i][1];
if(judge(Node.x, Node.y)) {//就像最短路,能更新就更新
if(d[Node.x][Node.y] > d[temptop.x][temptop.y] + mp[temptop.x][temptop.y]) {
d[Node.x][Node.y] = d[temptop.x][temptop.y] + mp[temptop.x][temptop.y];
pq.push(Node);//入队
}
}
}
}
return d[ex][ey];//返回终点值
} int main() {
while(~scanf("%d", &n)) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
scanf("%d", &mp[i][j]);
if(mp[i][j] == -1) {
mp[i][j] = 0;//很重要
sx = i;
sy = j;
}
if(mp[i][j] == -2) {//也存安全区的位置
mp[i][j] = 0;//很重要
ex = i;
ey = j;
}
}
}
fill(d[0], d[0] + MAX*MAX, INF);//初始化
printf("%d\n", bfs(sx, sy));
}
}

牛客 PUBG的更多相关文章

  1. 牛客小白月赛15 C 表单 ( map 使用)

    链接:https://ac.nowcoder.com/acm/contest/917/C来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...

  2. 牛客网程序员面试金典:1.1确定字符互异(java实现)

    问题描述: 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,True代表所有字符全都不同, ...

  3. 牛客网 --java问答题

    http://www.nowcoder.com/ 主要是自己什么都不怎么会.在这里可以学习很多的! 第一天看题自己回答,第二天看牛客网的答案! 1 什么是Java虚拟机?为什么Java被称作是“平台无 ...

  4. 【面试笔试算法】牛客网一站通Offer编程题2016.4.19

    牛客网一站通offer (一)字符串变形 1. 题目: 对于一个给定的字符串,我们需要在线性(也就是O(n))的时间里对它做一些变形.首先这个字符串中包含着一些空格,就像"Hello Wor ...

  5. 牛客网《BAT面试算法精品课》学习笔记

    目录 牛客网<BAT面试算法精品课>学习笔记 牛客网<BAT面试算法精品课>笔记一:排序 牛客网<BAT面试算法精品课>笔记二:字符串 牛客网<BAT面试算法 ...

  6. 牛客小白月赛13 小A买彩票 (记忆化搜索)

    链接:https://ac.nowcoder.com/acm/contest/549/C来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...

  7. 牛客小白月赛13-J小A的数学题 (莫比乌斯反演)

    链接:https://ac.nowcoder.com/acm/contest/549/J来源:牛客网 题目描述 小A最近开始研究数论题了,这一次他随手写出来一个式子,∑ni=1∑mj=1gcd(i,j ...

  8. C++版 - HDUoj 2010 3阶的水仙花数 - 牛客网

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - ...

  9. 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D

    目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...

随机推荐

  1. JSTL前台报错

    报错信息: jsp页面报错 Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core&qu ...

  2. STL stl_construct.h

    stl_construct.h // Filename: stl_construct.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog ...

  3. Hibernate - POJO 类和数据库的映射文件*.hbm.xml

    POJO 类和关系数据库之间的映射可以用一个XML文档来定义. 通过 POJO 类的数据库映射文件,Hibernate可以理解持久化类和数据表之间的对应关系,也可以理解持久化类属性与数据库表列之间的对 ...

  4. 【遍历二叉树】08判断两个二叉树是否相同【Same Tree】

    迭代版本用的是二叉树的DFS,中的root->right->left +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  5. 微信非全屏播放设置(仅Iphone)

    由于微信X5内核强制视频全屏,用X5自带内核播放,一般内嵌视频打开播放就会被全屏. ihpone里面可以通过设置 x-webkit-airplay="true" webkit-pl ...

  6. 继续学习C:运算符

     " / "  两整数相除,结果为整数,有一方是实数形式,结果保留小数         " % "  求余运算符要求两侧均为整型数据, 数值取余,符号与被除数一 ...

  7. freeMarker(八)——程序开发指南之配置(Configuration)

    学习笔记,选自freeMarker中文文档,译自 Email: ddekany at users.sourceforge.net 1.基本内容 配置(configuration)就是 freemark ...

  8. 【LeetCode】013. Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  9. Sublime Text 全程指南(转载)

    摘要(Abstract) 本文系统全面的介绍了Sublime Text,旨在成为最优秀的Sublime Text中文教程. 更新记录 2014/09/27:完成初稿 2014/09/28: 更正打开控 ...

  10. C++STL 库中set容器应用

    #include<iostream> #include<cstdio> #include<set> using namespace std; set<int& ...