当前位置:网站首页>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;
}边栏推荐
- Leetcode skimming - game 280
- How to specify const array in the global scope of rust- How to specify const array in global scope in Rust?
- 论文的设计方案咋写?
- 请求与响应
- MySQL advanced learning notes (4)
- Leetcode DP three step problem
- 经济学外文文献在哪查?
- Define MySQL function to realize multi module call
- Load balancing cluster (LBC)
- Architecture: load balancing
猜你喜欢

Difference between NVIDIA n card and amda card

maya渔屋建模

Request and response

The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north

Open Source | Wenxin Big Model Ernie Tiny Lightweight Technology, Accurate and Fast, full Open Effect

How to set automatic reply for mailbox and enterprise mailbox?

程序分析与优化 - 9 附录 XLA的缓冲区指派
![[shutter] open the third-party shutter project](/img/1a/e35d0180612d7e79b55e7818193740.jpg)
[shutter] open the third-party shutter project

How much do you know about synchronized?

Practical series - free commercial video material library
随机推荐
Returns the size of the largest binary search subtree in a binary tree
Linux 下安装 redis
Speech recognition Series 1: speech recognition overview
Top Devops tool chain inventory
Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open
Data set - fault diagnosis: various data and data description of bearings of Western Reserve University
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
How to specify const array in the global scope of rust- How to specify const array in global scope in Rust?
秒杀系统设计
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
Container runtime analysis
Mutual exclusion and synchronization of threads
容器运行时分析
MySQL advanced learning notes (III)
JDBC教程
Request and response
Chapter 3 of getting started with MySQL: database creation and operation
35页危化品安全管理平台解决方案2022版