当前位置:网站首页>P2802 go home
P2802 go home
2022-07-06 05:42:00 【A program ape who beats the keyboard violently】
Forever Zici original
CSDN Force list Ranking the first 12
# get home
## Title Description
[](https://paste.ubuntu.com/p/DSg5bzrrjs/)
Small H It's divided into $n \times m$ A rectangular cordon with two squares . Every time he can move one space up, down, left and right ( Of course small H Don't stand still ), But you can't leave the blockade , Or you'll be killed . At first he was full of blood $6$ spot , Each move consumes $1$ Point blood volume . Once small H Your blood volume is reduced to $0$, He will die . He can pick up the mouse along the road ( What the hell? ...) To fill up the blood volume . As long as he walks to the grid with a mouse , He doesn't need any time to pick up . The mouse on the grid can fill up in an instant , So every time I pass through this grid, there is a mouse . Even if you die in a grid with a mouse , He can't fill it up by picking up the mouse HP. Even if you die at home , He can't finish the task and come home .
There are five grids on the map :
`0`: obstacle .
`1`: clearing , Small H Can walk freely .
`2`: Small H starting point , It's also an open space .
`3`: Small H s home .
`4`: There is a mouse in the open space above .
Small H Can you go home safely ? If you can , How long is the shortest time ?
## Input format
The first line has two integers $n,m$, Indicates that the size of the map is $n \times m$.
below $n$ That's ok , Each row $m$ A number to describe the map .
## Output format
a line , If small H Can't go home , Output `-1`, Otherwise, the shortest time he needs to go home .
## Examples #1
### The sample input #1
```
3 3
2 1 1
1 1 0
1 1 3
```
### Sample output #1
```
4
```
## Tips
For all the data ,$1 \le n,m \le 9$.
2021.9.2 Add a group hack data by @ Embarrassing Fairy
【 a pile 64 Code division 】
#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;// Record the starting position
}
q.push((node){x,y,0,6}),a[x][y]=6;// The initial position sets its a value
while(!q.empty() and flag)// Until complete failure or success
{
z=q.front()/* Take the node at the front of the queue */,q.pop();
if(z.y==1)continue;// If the blood volume is 1, Continued to
for(int i=0;i<4 and flag;i++)// Four directions
{
if(map[z.n+dx[i][0]][z.m+dx[i][1]])// If you can get there
if(a[z.n+dx[i][0]][z.m+dx[i][1]]<z.y-1)// If you have more blood
{
p.n=z.n+dx[i][0],p.m=z.m+dx[i][1],p.x=z.x+1/* Update new nodes */,p.y=map[z.n][z.m]==4?6:z.y-1/* If the next node has a mouse , Then it has become 6*/,a[p.n][p.m]=z.y-1;// to update a
if(map[p.n][p.m]==3)flag=false;// If the task is completed , that flag to update
q.push(p);
}
}
}
if(flag)printf("-1");
else fwrite(p.x);
return 0;
}
It is found that not adding an equal sign will not CE, Just brave a wave .
#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;// Record the starting position
}
q.push((node){x,y,0,6}),a[x][y]=6;// The initial position sets its a value
while(!q.empty() and flag)// Until complete failure or success
{
z=q.front()/* Take the node at the front of the queue */,q.pop();
if(z.y==1)continue;// If the blood volume is 1, Continued to
for(int i=0;i<4 and flag;i++)// Four directions
{
if(map[z.n+dx[i][0]][z.m+dx[i][1]])// If you can get there
if(a[z.n+dx[i][0]][z.m+dx[i][1]]<z.y-1)// If you have more blood
{
p.n=z.n+dx[i][0],p.m=z.m+dx[i][1],p.x=z.x+1/* Update new nodes */,p.y=map[z.n][z.m]==4?6:z.y-1/* If the next node has a mouse , Then it has become 6*/,a[p.n][p.m]=z.y-1;// to update a
if(map[p.n][p.m]==3)flag=false;// If the task is completed , that flag to update
q.push(p);
}
}
}
if(flag)printf("-1");
else fwrite(p.x);
return 0;
}
Think it's an equal sign problem , It didn't turn out to be .
【CE Code 】
#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;
}
Forget to type
using namespace std;
了
【80 Code division 】
#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;
}
The array is down .
【AC Code 】
#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;
}
Another locomotive is added to optimize , Want to rush for an optimal solution , As a result, the locomotive was not allowed .
边栏推荐
- Vulhub vulnerability recurrence 68_ ThinkPHP
- 应用安全系列之三十七:日志注入
- 02. Develop data storage of blog project
- 备忘一下jvxetable的各种数据集获取方法
- Yygh-11-timing statistics
- B站刘二大人-线性回归 Pytorch
- B站刘二大人-线性回归及梯度下降
- The digital economy has broken through the waves. Is Ltd a Web3.0 website with independent rights and interests?
- 59. Spiral matrix
- 移植InfoNES到STM32
猜你喜欢
B站刘二大人-数据集及数据加载 Lecture 8
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
移植InfoNES到STM32
Installation de la Bibliothèque de processus PDK - csmc
(column 22) typical column questions of C language: delete the specified letters in the string.
04. 项目博客之日志
01. Project introduction of blog development project
数字经济破浪而来 ,LTD是权益独立的Web3.0网站?
什么是独立IP,独立IP主机怎么样?
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
随机推荐
Node 之 nvm 下载、安装、使用,以及node 、nrm 的相关使用
A master in the field of software architecture -- Reading Notes of the beauty of Architecture
JDBC calls the stored procedure with call and reports an error
实践分享:如何安全快速地从 Centos迁移到openEuler
大型网站如何选择比较好的云主机服务商?
Rustdesk builds its own remote desktop relay server
Web Security (VI) the use of session and the difference between session and cookie
How to get list length
How to download GB files from Google cloud hard disk
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
YYGH-11-定时统计
[experience] when ultralso makes a startup disk, there is an error: the disk / image capacity is too small
05. Security of blog project
[SQL Server fast track] - authentication and establishment and management of user accounts
Deep learning -yolov5 introduction to actual combat click data set training
Installation de la Bibliothèque de processus PDK - csmc
SQLite queries the maximum value and returns the whole row of data
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
Application Security Series 37: log injection
Huawei od computer test question 2