当前位置:网站首页>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;
}
边栏推荐
- [shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)
- Develop knowledge points
- 大学生课堂作业2000~3000字的小论文,标准格式是什么?
- [array] binary search
- 论文的英文文献在哪找(除了知网)?
- MFC gets the current time
- Talk with the interviewer about the pit of MySQL sorting (including: duplicate data problem in order by limit page)
- How to apply for company email when registering in company email format?
- What is the standard format of a 2000-3000 word essay for college students' classroom homework?
- How much do you know about synchronized?
猜你喜欢
maya渔屋建模
MySQL advanced learning notes (III)
MATLAB signal processing [Q & a notes-1]
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表
Container runtime analysis
JDBC練習案例
JDBC tutorial
JS interviewer wants to know how much you understand call, apply, bind no regrets series
教育学大佬是怎么找外文参考文献的?
随机推荐
95 pages of smart education solutions 2022
MySQL Foundation
Bean加载控制
MySQL advanced learning notes (III)
Architecture: database architecture design
What are the recommended thesis translation software?
Judge whether the binary tree is full binary tree
【OJ】两个数组的交集(set、哈希映射 ...)
JSON data transfer parameters
TypeError: Cannot read properties of undefined (reading ***)
请问大家在什么网站上能查到英文文献?
Highly available cluster (HAC)
RTP 接发ps流工具改进(二)
How much do you know about synchronized?
哪些软件可以整篇翻译英文论文?
Returns the size of the largest binary search subtree in a binary tree
CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
How to set automatic reply for mailbox and enterprise mailbox?
Integration of revolution and batch normalization
JDBC練習案例