当前位置:网站首页>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;
}边栏推荐
- Flexible combination of applications is a false proposition that has existed for 40 years
- 基于OpenCV实现口罩识别
- 洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表
- Optimization of streaming media technology
- collections. What is the purpose of chainmap- What is the purpose of collections. ChainMap?
- 有哪些比较推荐的论文翻译软件?
- Digital collection trading website domestic digital collection trading platform
- JDBC Exercise case
- RTP 接发ps流工具改进(二)
- Angled detection frame | calibrated depth feature for target detection (with implementation source code)
猜你喜欢

Integration of revolution and batch normalization

Load balancing cluster (LBC)

Is the multitasking loss in pytoch added up or backward separately?

基于OpenCV实现口罩识别

How much do you know about synchronized?

Dishes launcher small green program and directory management (efficiency tool)

容器运行时分析

Digital twin smart factory develops digital twin factory solutions

Improvement of RTP receiving and sending PS stream tool (II)

TypeError: Cannot read properties of undefined (reading ***)
随机推荐
yolov5detect. Py comment
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
yolov5test. Py comment
哪些软件可以整篇翻译英文论文?
Agnosticism and practice makes perfect
带角度的检测框 | 校准的深度特征用于目标检测(附实现源码)
Leetcode DP three step problem
What are the recommended thesis translation software?
Digital twin smart factory develops digital twin factory solutions
TypeError: Cannot read properties of undefined (reading ***)
[Verilog tutorial]
国外的论文在那找?
Data set - fault diagnosis: various data and data description of bearings of Western Reserve University
CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)
Question e: merged fruit -noip2004tgt2
有哪些比较推荐的论文翻译软件?
Improvement of RTP receiving and sending PS stream tool (II)
JDBC练习案例
Dishes launcher small green program and directory management (efficiency tool)
Linux 下安装 redis