当前位置:网站首页>AcWing_ 188. Warrior cattle_ bfs
AcWing_ 188. Warrior cattle_ bfs
2022-07-03 00:13:00 【This question AC sleep again】
AcWing_188. A warrior cow _bfs
a farmer John There are many cows , He wants to trade one of them Don be called The Knight Of cattle .
This cow has a unique superpower , On the farm like Knight Jump like ( It's the familiar way of horse walking in chess ).
Although this magical cow can't jump on trees and stones , But it can jump freely on the pasture , We use a ranch x,y To represent .
This magical cow likes to eat grass like other cows , Here's a map for you , It says The Knight The beginning of , Trees 、 shrub 、 The location of stones and other obstacles , In addition, there is a bundle of grass .
Now your task is , determine The Knight Want to eat grass , At least how many times .
The Knight The location of is K To mark , The location of the obstacle is * To mark , The position of the grass is H To mark .
Here is an example of a map :
11 | . . . . . . . . . .
10 | . . . . * . . . . .
9 | . . . . . . . . . .
8 | . . . * . * . . . .
7 | . . . . . . . * . .
6 | . . * . . * . . . H
5 | * . . . . . . . . .
4 | . . . * . . . * . .
3 | . K . . . . . . . .
2 | . . . * . . . . . *
1 | . . * . . . . * . .
0 ----------------------
1
0 1 2 3 4 5 6 7 8 9 0
The Knight You can follow the... In the figure below A,B,C,D…A,B,C,D… This path uses 5 Jump to the grass for the first time ( It is possible that the length of other routes is also 5):
11 | . . . . . . . . . .
10 | . . . . * . . . . .
9 | . . . . . . . . . .
8 | . . . * . * . . . .
7 | . . . . . . . * . .
6 | . . * . . * . . . F<
5 | * . B . . . . . . .
4 | . . . * C . . * E .
3 | .>A . . . . D . . .
2 | . . . * . . . . . *
1 | . . * . . . . * . .
0 ----------------------
1
0 1 2 3 4 5 6 7 8 9 0
Be careful : Data guarantee there must be solutions .
Input format
The first 1 That's ok : Two Numbers , Number of columns representing the farm C And the number of lines R.
The first 2..R+1 That's ok : Each line is made up of C A string of characters , Together to map the ranch .
Output format
An integer , Represents the minimum number of jumps .
Data range
1≤R,C≤150
sample input :
10 11
..........
....*.....
..........
...*.*....
.......*..
..*..*...H
*.........
...*...*..
.K........
...*.....*
..*....*..
sample output :
5//
#include<bits/stdc++.h>
using namespace std;
const int dx[]={ 1,1,-1,-1,2,2,-2,-2 };
const int dy[]={ 2,-2,2,-2,1,-1,1,-1 };
const int INF=0x3f3f3f3f;
const int N=222;
bool used[N][N]; // Mark
char s[N][N];
int n,m,ans;
struct A
{
int x,y,cnt;
A( int xx=0,int yy=0,int in=0 ):x(xx),y(yy),cnt(in) {}
}tt;
void bfs( int x,int y,int cnt )
{
int i,tx,ty;
queue<A> q;
q.push( A( x,y,cnt ) ); used[x][y]=1;
while( !q.empty() )
{ // front()
tt=q.front(); q.pop();
if( s[tt.x][tt.y]=='H' ) { ans=min( ans,tt.cnt ); continue; }
for( i=0;i<8;i++ )
{
tx=tt.x+dx[i]; ty=tt.y+dy[i];
if( tx>=0 && tx<n && ty>=0 && ty<m &&
used[tx][ty]==0 && s[tx][ty]!='*' ) // tt.cnt
{ used[tx][ty]=1; q.push( A( tx,ty,tt.cnt+1 ) ); }
} // A() Constructors
}
}
int main()
{
int i,j;
while( cin>>m>>n )
{
memset( used,0,sizeof( used ) );
memset( s,0,sizeof( s ) );
for( i=0;i<n;i++ ) cin>>s[i];
for( i=0;i<n;i++ )
for( j=0;j<m;j++ )
if( s[i][j]=='K' ) goto out;
out:
ans=INF; bfs( i,j,0 );
cout<<ans<<endl;
}
return 0;
}边栏推荐
- Use of cocospods
- The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
- Master the development of facial expression recognition based on deep learning (based on paddlepaddle)
- Unique line of "Gelu"
- TypeError: Cannot read properties of undefined (reading ***)
- TypeError: Cannot read properties of undefined (reading ***)
- QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)
- leetcode 650. 2 keys keyboard with only two keys (medium)
- Leetcode relaxation question - day of the week
- 67 page overall planning and construction plan for a new smart city (download attached)
猜你喜欢

接口差异测试——Diffy工具

程序分析与优化 - 9 附录 XLA的缓冲区指派

Integration of revolution and batch normalization

How much do you know about synchronized?

Additional: token; (don't read until you finish writing...)

Why can't the start method be called repeatedly? But the run method can?

Interface difference test - diffy tool

PR FAQ, what about PR preview video card?

Load balancing cluster (LBC)

实用系列丨免费可商用视频素材库
随机推荐
Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open
Xcode real machine debugging
Interface difference test - diffy tool
MySQL advanced learning notes (III)
Custom throttling function six steps to deal with complex requirements
Bigder:32/100 测试发现的bug开发认为不是bug怎么处理
Request and response
Hit the industry directly! The propeller launched the industry's first model selection tool
Load balancing cluster (LBC)
Architecture: database architecture design
A single element in an ordered array -- Valentine's Day mental problems
Where can I find foreign papers?
Additional: token; (don't read until you finish writing...)
Optimization of streaming media technology
[reading notes] phased summary of writing reading notes
接口差异测试——Diffy工具
流媒体技术优化
JVM foundation review
Leetcode skimming - game 280
Unique line of "Gelu"