题目链接:点击打开链接
题目大意:跑毒,跑到安全区,每个地方有敌人,输出路线经过的最少敌人的数量;-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. 1_Command 游戏开发命令模式

    A dead simple implementation looks like: ``` // simple void InputHandler::handleInput() { if (isPres ...

  2. leetcode 3 Longest Substring Without Repeating Characters(滑动窗口)

    用滑动窗口的思想来做.用一个unordered_map来查询之前的char有没有在现在的窗口中. class Solution { public: int lengthOfLongestSubstri ...

  3. Silk codec的一些资料

    Skype表示它最近将开始向第三方开发人员和硬件制造商提供免版税认证(RF)的Silk宽带音频编码器. Silk下载地址如下 http://developer.skype.com/silk/SILK_ ...

  4. POJ2774Long Long Message (后缀数组&后缀自动机)

    问题: The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to ...

  5. 【转】Pro Android学习笔记(十二):了解Intent(下)

    解析Intent,寻找匹配Activity 如果给出component名字(包名.类名)是explicit intent,否则是implicit intent.对于explicit intent,关键 ...

  6. Framework配置错误

    转自:http://blog.csdn.net/ked/article/details/25052955 VS2012命令提示符无法使用的解决方法 打开VS2012命令提示符时报错“ERROR: Ca ...

  7. lua 函数调用 -- 闭包详解和C调用

    转自:http://www.cnblogs.com/ringofthec/archive/2010/11/05/luaClosure.html 这里, 简单的记录一下lua中闭包的知识和C闭包调用 前 ...

  8. win10系统的简单优化

    1.关闭自带杀毒软件Windows Defender操作简要:在gpedit.msc 组策略-计算机管理——>管理模板——>windows组件——>windows defender ...

  9. Python-RabbitMQ消息队列实现rpc

    客户端通过发送命令来调用服务端的某些服务,服务端把结果再返回给客户端 这样使得RabbitMQ的消息发送端和接收端都能发送消息 返回结果的时候需要指定另一个队列 服务器端 # -*- coding:u ...

  10. JavaScript设模式---单例模式

    单例模式也称为单体模式,其中: 1,单体模式用于创建命名空间,将系列关联的属性和方法组织成一个逻辑单元,减少全局变量. 逻辑单元中的代码通过单一的变量进行访问. 2,三个特点: ① 该类只有一个实例: ...