当前位置:网站首页>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

边栏推荐
- Electronic tube: Literature Research on basic characteristics of 6j1
- Wisdom tooth technology announced that it had completed the round D financing of US $100million and had not obtained a valid patent yet
- Comparable interface and comparator interface
- The reason why the computer runs slowly and how to solve it
- [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)
- 320. Energy Necklace (ring, interval DP)
- [flax high frequency question] leetcode 426 Convert binary search tree to sorted double linked list
- Text replacement demo
- QGIS grid processing DEM data reclassification
- AST (Abstract Syntax Tree)
猜你喜欢

Electronic tube: Literature Research on basic characteristics of 6j1

Hcip day 15 notes

Es6~es12 knowledge sorting and summary

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

IDENTITY

Hcip 13th day notes

How to connect a laptop to a projector

Pan Yueming helps Germany's Rochester Zodiac custom wristwatch

Summary of fluent systemchrome

C deep anatomy - the concept of keywords and variables # dry inventory #
随机推荐
Go Technology Daily (2022-02-13) - Summary of experience in database storage selection
On my first day at work, this API timeout optimization put me down!
Summary of basic knowledge of exception handling
IO flow review
Ppt image processing
Unique in China! Alibaba cloud container service enters the Forrester leader quadrant
Buuctf, web:[geek challenge 2019] buyflag
How to solve the problem of computer networking but showing no Internet connection
To rotate 90 degrees clockwise and modify the video format
Pooling idea: string constant pool, thread pool, database connection pool
Yyds dry goods inventory Spring Festival "make" your own fireworks
在恒泰证券开户怎么样?安全吗?
Team collaborative combat penetration tool CS artifact cobalt strike
Leetcode week 4: maximum sum of arrays (shape pressing DP bit operation)
[automation operation and maintenance novice village] flask-2 certification
STM32 multi serial port implementation of printf -- Based on cubemx
[issue 16] golang's one-year experience in developing Purdue Technology
Subset enumeration method
Wisdom tooth technology announced that it had completed the round D financing of US $100million and had not obtained a valid patent yet
Niuke winter vacation training camp 4 g (enumeration optimization, Euler power reduction)