当前位置:网站首页>P2802 回家
P2802 回家
2022-07-06 05:32:00 【暴揍键盘的程序猿】
永远资瓷原创

CSDN原力榜排名第12

# 回家
## 题目描述
[](https://paste.ubuntu.com/p/DSg5bzrrjs/)
小 H 在一个划分成了 $n \times m$ 个方格的长方形封锁线上。 每次他能向上下左右四个方向移动一格(当然小 H 不可以静止不动), 但不能离开封锁线,否则就被打死了。 刚开始时他有满血 $6$ 点,每移动一格他要消耗 $1$ 点血量。一旦小 H 的血量降到 $0$, 他将死去。 他可以沿路通过拾取鼠标(什么鬼。。。)来补满血量。只要他走到有鼠标的格子,他不需要任何时间即可拾取。格子上的鼠标可以瞬间补满,所以每次经过这个格子都有鼠标。就算到了某个有鼠标的格子才死去, 他也不能通过拾取鼠标补满 HP。 即使在家门口死去, 他也不能算完成任务回到家中。
地图上有五种格子:
`0`:障碍物。
`1`:空地, 小 H 可以自由行走。
`2`:小 H 出发点, 也是一片空地。
`3`:小 H 的家。
`4`:有鼠标在上面的空地。
小 H 能否安全回家?如果能, 最短需要多长时间呢?
## 输入格式
第一行两个整数 $n,m$, 表示地图的大小为 $n \times m$。
下面 $n$ 行, 每行 $m$ 个数字来描述地图。
## 输出格式
一行, 若小 H 不能回家, 输出 `-1`,否则输出他回家所需最短时间。
## 样例 #1
### 样例输入 #1
```
3 3
2 1 1
1 1 0
1 1 3
```
### 样例输出 #1
```
4
```
## 提示
对于所有数据,$1 \le n,m \le 9$。
2021.9.2 增添一组 hack 数据 by @囧仙
【一堆64分代码】
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
const int N=9;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
void fwrite(int n)
{
if(n>9)fwrite(n/10);
putchar(n%10+'0');
}
int map[N][N],dx[4][2]/*=*/{0,1,0,-1,1,0,-1,0},a[N][N],n,m,x,y;
bool flag=true;
struct node
{
int n,m,x,y;
}z,p;
queue<node>q;
signed main()
{
n=fread(),m=fread();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
map[i][j]=fread();
if(map[i][j]==2)x=i,y=j;//记录出发位置
}
q.push((node){x,y,0,6}),a[x][y]=6;//初始位置设置其a值
while(!q.empty() and flag)//直到完全失败或成功
{
z=q.front()/*取队列最前面的节点*/,q.pop();
if(z.y==1)continue;//如果血量为1,则继续
for(int i=0;i<4 and flag;i++)//四个方向
{
if(map[z.n+dx[i][0]][z.m+dx[i][1]])//如果可以到达
if(a[z.n+dx[i][0]][z.m+dx[i][1]]<z.y-1)//如果血量更大
{
p.n=z.n+dx[i][0],p.m=z.m+dx[i][1],p.x=z.x+1/*更新新的节点*/,p.y=map[z.n][z.m]==4?6:z.y-1/*如果下一个节点是有鼠标的,那么有变成6*/,a[p.n][p.m]=z.y-1;//更新a
if(map[p.n][p.m]==3)flag=false;//如果任务完成,那么flag更新
q.push(p);
}
}
}
if(flag)printf("-1");
else fwrite(p.x);
return 0;
}
发现不加等号不会CE,就勇了一波。

#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
const int N=9;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
void fwrite(int n)
{
if(n>9)fwrite(n/10);
putchar(n%10+'0');
}
int map[N][N],dx[4][2]={0,1,0,-1,1,0,-1,0},a[N][N],n,m,x,y;
bool flag=true;
struct node
{
int n,m,x,y;
}z,p;
queue<node>q;
signed main()
{
n=fread(),m=fread();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
map[i][j]=fread();
if(map[i][j]==2)x=i,y=j;//记录出发位置
}
q.push((node){x,y,0,6}),a[x][y]=6;//初始位置设置其a值
while(!q.empty() and flag)//直到完全失败或成功
{
z=q.front()/*取队列最前面的节点*/,q.pop();
if(z.y==1)continue;//如果血量为1,则继续
for(int i=0;i<4 and flag;i++)//四个方向
{
if(map[z.n+dx[i][0]][z.m+dx[i][1]])//如果可以到达
if(a[z.n+dx[i][0]][z.m+dx[i][1]]<z.y-1)//如果血量更大
{
p.n=z.n+dx[i][0],p.m=z.m+dx[i][1],p.x=z.x+1/*更新新的节点*/,p.y=map[z.n][z.m]==4?6:z.y-1/*如果下一个节点是有鼠标的,那么有变成6*/,a[p.n][p.m]=z.y-1;//更新a
if(map[p.n][p.m]==3)flag=false;//如果任务完成,那么flag更新
q.push(p);
}
}
}
if(flag)printf("-1");
else fwrite(p.x);
return 0;
}
以为是等号的问题,结果不是。

【CE代码】
#include<cstdio>
#include<iostream>
#include<queue>
const int N=9;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
void fwrite(int n)
{
if(n>9)fwrite(n/10);
putchar(n%10+'0');
}
int n,m,v[N][N],g[N][N],dx[4]={0,1,0,-1},dy[4]={1,0,-1,0},s,s1;
struct node
{
int x,y,s,b;
node(){}
node(int x1,int yy1,int s1,int b1)
{
x=x1,y=yy1,s=s1,b=b1;
}
};
queue<node>q;
signed main()
{
n=fread(),m=fread();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
g[i][j]=fread();
if(g[i][j]==2)s=i,s1=j;
}
v[s][s1]=6,q.push(node(s,s1,0,6));
while(!q.empty())
{
node t=q.front();
q.pop();
int x=t.x,y=t.y,s=t.s,b=t.b;
for(int i=0;i<4;i++)
{
int x1=x+dx[i],yy1=y+dy[i],b1=b-1;
if(g[x1][yy1]==0 or b1==0)continue;
if(g[x1][yy1]==3)
{
fwrite(s+1);
return 0;
}
if(g[x1][yy1]==4)b1=6;
if(v[x1][yy1]>=b1)continue;
q.push(node(x1,yy1,s+1,b1)),v[x1][yy1]=b1;
}
}
printf("-1");
return 0;
}
忘打
using namespace std;
了

【80分代码】
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
const int N=9;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
void fwrite(int n)
{
if(n>9)fwrite(n/10);
putchar(n%10+'0');
}
int n,m,v[N][N],g[N][N],dx[4]={0,1,0,-1},dy[4]={1,0,-1,0},s,s1;
struct node
{
int x,y,s,b;
node(){}
node(int x1,int yy1,int s1,int b1)
{
x=x1,y=yy1,s=s1,b=b1;
}
};
queue<node>q;
signed main()
{
n=fread(),m=fread();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
g[i][j]=fread();
if(g[i][j]==2)s=i,s1=j;
}
v[s][s1]=6,q.push(node(s,s1,0,6));
while(!q.empty())
{
node t=q.front();
q.pop();
int x=t.x,y=t.y,s=t.s,b=t.b;
for(int i=0;i<4;i++)
{
int x1=x+dx[i],yy1=y+dy[i],b1=b-1;
if(g[x1][yy1]==0 or b1==0)continue;
if(g[x1][yy1]==3)
{
fwrite(s+1);
return 0;
}
if(g[x1][yy1]==4)b1=6;
if(v[x1][yy1]>=b1)continue;
q.push(node(x1,yy1,s+1,b1)),v[x1][yy1]=b1;
}
}
printf("-1");
return 0;
}
数组开小了。

【AC代码】
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
const int N=20;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
void fwrite(int n)
{
if(n>9)fwrite(n/10);
putchar(n%10+'0');
}
int n,m,v[N][N],g[N][N],dx[4]={0,1,0,-1},dy[4]={1,0,-1,0},s,s1;
struct node
{
int x,y,s,b;
node(){}
node(int x1,int yy1,int s1,int b1)
{
x=x1,y=yy1,s=s1,b=b1;
}
};
queue<node>q;
signed main()
{
n=fread(),m=fread();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
g[i][j]=fread();
if(g[i][j]==2)s=i,s1=j;
}
v[s][s1]=6,q.push(node(s,s1,0,6));
while(!q.empty())
{
node t=q.front();
q.pop();
int x=t.x,y=t.y,s=t.s,b=t.b;
for(int i=0;i<4;i++)
{
int x1=x+dx[i],yy1=y+dy[i],b1=b-1;
if(g[x1][yy1]==0 or b1==0)continue;
if(g[x1][yy1]==3)
{
fwrite(s+1);
return 0;
}
if(g[x1][yy1]==4)b1=6;
if(v[x1][yy1]>=b1)continue;
q.push(node(x1,yy1,s+1,b1)),v[x1][yy1]=b1;
}
}
printf("-1");
return 0;
}
又加了个火车头优化了一下,想冲个最优解,结果不让用火车头。

边栏推荐
- After the project is released, index Html is cached
- Jvxetable用slot植入j-popup
- LeetCode_ String inversion_ Simple_ 557. Reverse word III in string
- 指針經典筆試題
- Graduation design game mall
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Zoom and pan image in Photoshop 2022
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
猜你喜欢
[JVM] [Chapter 17] [garbage collector]
Remember an error in MySQL: the user specified as a definer ('mysql.infoschema '@' localhost ') does not exist
Notes, continuation, escape and other symbols
指针经典笔试题
Text classification still stays at Bert? The dual contrast learning framework is too strong
ByteDance program yuan teaches you how to brush algorithm questions: I'm not afraid of the interviewer tearing the code
59. Spiral matrix
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
巨杉数据库再次亮相金交会,共建数字经济新时代
In 2022, we must enter the big factory as soon as possible
随机推荐
nacos-高可用seata之TC搭建(02)
Vulhub vulnerability recurrence 67_ Supervisor
Vulhub vulnerability recurrence 72_ uWSGI
Easy to understand I2C protocol
[machine learning notes] univariate linear regression principle, formula and code implementation
HAC集群修改管理员用户密码
Summary of redis basic knowledge points
Cuda11.1 online installation
Solution of QT TCP packet sticking
C进阶-数据的存储(上)
Talking about the type and function of lens filter
Imperial cms7.5 imitation "D9 download station" software application download website source code
备忘一下jvxetable的各种数据集获取方法
Vulhub vulnerability recurrence 73_ Webmin
[detailed explanation of Huawei machine test] check whether there is a digital combination that meets the conditions
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Huawei equipment is configured with OSPF and BFD linkage
C# AES对字符串进行加密
Zoom and pan image in Photoshop 2022
无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...