当前位置:网站首页>AcWing_188. 武士风度的牛_bfs
AcWing_188. 武士风度的牛_bfs
2022-07-02 22:53:00 【这题AC再睡.】
农民 John 有很多牛,他想交易其中一头被 Don 称为 The Knight 的牛。
这头牛有一个独一无二的超能力,在农场里像 Knight 一样地跳(就是我们熟悉的象棋中马的走法)。
虽然这头神奇的牛不能跳到树上和石头上,但是它可以在牧场上随意跳,我们把牧场用一个 x,y 的坐标图来表示。
这头神奇的牛像其它牛一样喜欢吃草,给你一张地图,上面标注了 The Knight 的开始位置,树、灌木、石头以及其它障碍的位置,除此之外还有一捆草。
现在你的任务是,确定 The Knight 要想吃到草,至少需要跳多少次。
The Knight 的位置用 K 来标记,障碍的位置用 * 来标记,草的位置用 H 来标记。
这里有一个地图的例子:
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 可以按照下图中的 A,B,C,D…A,B,C,D… 这条路径用 5 次跳到草的地方(有可能其它路线的长度也是 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
注意: 数据保证一定有解。
输入格式
第 1 行: 两个数,表示农场的列数 C 和行数 R。
第 2..R+1 行: 每行一个由 C 个字符组成的字符串,共同描绘出牧场地图。
输出格式
一个整数,表示跳跃的最小次数。
数据范围
1≤R,C≤150
输入样例:
10 11
..........
....*.....
..........
...*.*....
.......*..
..*..*...H
*.........
...*...*..
.K........
...*.....*
..*....*..
输出样例:
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]; // 标记
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() 构造函数
}
}
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;
}边栏推荐
猜你喜欢

Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open

Maybe you read a fake Tianlong eight

Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity

Interface switching based on pyqt5 toolbar button -2

I've been interviewed. The starting salary is 16K

Where is the win11 microphone test? Win11 method of testing microphone

Hit the industry directly! The propeller launched the industry's first model selection tool

Use redis to realize self increment serial number

Difference between NVIDIA n card and amda card

Master the development of facial expression recognition based on deep learning (based on paddlepaddle)
随机推荐
Additional: token; (don't read until you finish writing...)
CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
How much do you know about synchronized?
2022 latest and complete interview questions for software testing
Fudian bank completes the digital upgrade | oceanbase database helps to layout the distributed architecture of the middle office
[OJ] intersection of two arrays (set, hash mapping...)
PHP get real IP
Convolution和Batch normalization的融合
Brief introduction to common sense of Zhongtai
[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)
Chinatelecom has maintained a strong momentum in the mobile phone user market, but China Mobile has opened a new track
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
[live broadcast appointment] database obcp certification comprehensive upgrade open class
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
I've been interviewed. The starting salary is 16K
富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
直击产业落地!飞桨重磅推出业界首个模型选型工具
Bean加载控制
MFC file operation
開源了 | 文心大模型ERNIE-Tiny輕量化技術,又准又快,效果全開