当前位置:网站首页>2022.02.13
2022.02.13
2022-07-03 22:55:00 【Walking code Jun】
Review today bfs
Problem description
The warriors inadvertently entered the enemy's minefield ( use n That's ok n Matrix representation of columns ,'*' It means that a mine is buried at a certain location ,'-' Indicates that a location is safe ), They each need to be in the specified number of steps ( One step represents walking to the position adjacent to the current position ) Bypass the mines inside to reach the exit ( The first row, the first space , That is, the coordinates are (0,0) The location of ) To complete the task , Tell you the position of each warrior (x,y) And the specified number of steps s, Please judge whether each warrior can successfully complete the task (1 representative “ can ”,-1 representative “ You can't ”).
Input format
The first line of input data is an integer n; Line two to line one n+1 Line is n That's ok n Matrix representation of the column mine array ( See input example ); The first n+2 Go to the last line, and each line is the position of a warrior x、y And the maximum number of steps required to reach the exit s, Three integers are separated by spaces .
Output format
Output the judgment of whether each warrior can successfully complete the task in order (1 representative “ can ”,-1 representative “ You can't ”), The judgment of each warrior is in one line .
The sample input
5
-----
--*--
-**--
-**--
*-*--
0 1 1
0 4 3
1 1 3
1 4 2
2 0 3
3 0 4
3 3 2
4 1 3Sample output
1
-1
1
-1
1
1
-1
-1Data size and engagement
1≤n≤500,0≤x≤n-1,0≤y≤n-1,1≤s≤500
#include<iostream>
#include<queue>
using namespace std;
const int maxn=501;
typedef struct p
{
int x,y;
}node;
int n;
int X,Y,Z;
char s[maxn][maxn];
int v[maxn][maxn];
int dx[]={-1,0,1,0};// Top left, bottom right
int dy[]={0,1,0,-1};// Top left, bottom right
queue<node>q;
void bfs(int a,int b)
{
node temp;
v[a][b]=0;
q.push({a,b});
while(q.size())
{
temp=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int nx,ny;
nx=temp.x+dx[i];
ny=temp.y+dy[i];
if(nx<0||nx>n-1||ny<0||ny>n-1||v[nx][ny]!=0||s[nx][ny]=='*')
continue;
v[nx][ny]=v[temp.x][temp.y]+1;
q.push({nx,ny});
}
}
} // The key part
int main()
{
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>s[i][j];
bfs(0,0);
while(scanf("%d %d %d",&X,&Y,&Z)!=EOF)
{
if(X==0&&Y==0)
cout<<1<<endl;
else
{
if(v[X][Y]!=0&&v[X][Y]<=Z)
cout<<1<<endl;
else
cout<<-1<<endl;
}
}
}
Then there is the mindless problem

边栏推荐
- Qtoolbutton - menu and popup mode
- Fashion cloud interview questions series - JS high-frequency handwritten code questions
- Is it safe and reliable to open an account and register for stock speculation? Is there any risk?
- The 2022 global software R & D technology conference was released, and world-class masters such as Turing prize winners attended
- Plug - in Oil Monkey
- Text replacement demo
- Data consistency between redis and database
- Overview of Yunxi database executor
- Get current JVM data
- Recursion and recursion
猜你喜欢

3 environment construction -standalone

How to solve the problem of computer networking but showing no Internet connection
![Buuctf, web:[geek challenge 2019] buyflag](/img/02/d3add04f8145621bff35d46b82ba53.jpg)
Buuctf, web:[geek challenge 2019] buyflag

1068. Consolidation of ring stones (ring, interval DP)

How to restore the factory settings of HP computer

4 environment construction -standalone ha
![[automation operation and maintenance novice village] flask-2 certification](/img/9a/a9b45e1f41b9b75695dcb06c212a69.jpg)
[automation operation and maintenance novice village] flask-2 certification

Sort merge sort
![[Android reverse] use the DB browser to view and modify the SQLite database (copy the database file from the Android application data directory | use the DB browser tool to view the data block file)](/img/6e/3ffa91154a718b6ace6c8ca87c5995.jpg)
[Android reverse] use the DB browser to view and modify the SQLite database (copy the database file from the Android application data directory | use the DB browser tool to view the data block file)

Gorilla/mux framework (RK boot): add tracing Middleware
随机推荐
LeetCode 540. A single element in an ordered array
Gorilla/mux framework (RK boot): add tracing Middleware
Exness: the Central Bank of England will raise interest rates again in March, and inflation is coming
Buuctf, misc: sniffed traffic
C3p0 connection MySQL 8.0.11 configuration problem
string
Some 5000+ likes, the development notes of a director of cosmic factory, leaked
What are the common computer problems and solutions
Hcip 13th day notes
Creation of the template of the password management software keepassdx
Hcip day 12 notes
[dynamic programming] Jisuan Ke: Jumping stake (variant of the longest increasing subsequence)
Oil monkey plug-in
[sg function]split game (2020 Jiangxi university student programming competition)
Niuke winter vacation training camp 4 g (enumeration optimization, Euler power reduction)
User login function: simple but difficult
The reason why the computer runs slowly and how to solve it
[sg function] lightoj Partitioning Game
IDENTITY
Yyds dry goods inventory [practical] simply encapsulate JS cycle with FP idea~