题目链接:点击打开链接
题目大意:跑毒,跑到安全区,每个地方有敌人,输出路线经过的最少敌人的数量;-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. 2018.5.31 nRF905 test

    1 试电机:自动控制测试流程(Labview程序,加载扫描仪,自动测试夹具,测试数据保存) 2 USB RF收发器: 含S/N码发送读取功能(S/N:) The specific use please ...

  2. codeforces 622A A. Infinite Sequence (二分)

    A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. os.path

  4. Java读取文件的时候,如何让指针重新回到文件的开头

    今天在测试IO流的使用的时候发现在reader读取文件之后,再向文件添加内容,再继续读文件,打印出的结果只能读取追加的文件. 如何才能重新读取呢?试了mark和reset,似乎会报异常.记在这以后看是 ...

  5. python suds 调用webservice 缓存

    在linux系统中 如果webservice更新了字段 suds调用有可能缓存以前的字段或方法,对新的字段报找不到类型 TypeNotFound,或者对 新加的方法找不到该方法的错误. 当更新或添加w ...

  6. Agc010_D Decrementing

    今天本人因调了上篇博客的题而脑壳不适,不想颓题,因此有了这篇博客. 但是博客毕竟得讲点什么,想想有没有什么代码短的. 哦,好像有,就Agc010_D Decrementing好了. Alice和Bob ...

  7. Marionettejs

    Marionette是牵线木偶的意思,这个库是对Backbone的一次更高层次封装.这样的封装有两个目标: 减少重复的工作,提高使用Backbonejs时的生产效率给复杂应用页面提供更多的结构,以支撑 ...

  8. 洛谷 P4547 & bzoj 5006 随机二分图 —— 状压DP+期望

    题目:https://www.luogu.org/problemnew/show/P4547 https://www.lydsy.com/JudgeOnline/problem.php?id=5006 ...

  9. Convolutional Neural Networks 笔记

    1 Foundations of Convolutional Neural Networks 1.1 cv问题 图像分类.目标检测.风格转换.但是高像素的图片会带来许多许多的特征. 1.2 边缘检测( ...

  10. nodejs 操作文件系统读取写入文件

    我们通过fs这个模块来对文件系统进行操作,对于文件系统操作一般都有同步.异步方法,两者区别,同步等有返回结果时候,在继续执行后面的代码,异步是不等返回结果,直接执行后面的代码,待有返回结果时候,通过回 ...