一.題目描述:
二.解題思路:
馬在象棋中是走日的,所以根據這個特點可以寫出八個方向,用bfs解決即可,需要注意的是,不需要每乙個點都列舉,不然t兩個點。可以跑一次就做完所有事情,然後為distance為0的就是不能到達的輸出-1即可。
三.**實現:
1 #include "bits/stdc++.h"2
using
namespace
std;
3int dt[500][500];4
int bk[500][500];5
int mv[8][2] = ,,,,,,,};
6int
n,m,x,y;
7struct
node;
12node u;
13void
bfs()
1436 head++;37}
38}39int
main()
4055 printf("\n"
);56}57
return0;
58 }
P1443 馬的遍歷
題目描述 有乙個n m的棋盤 1輸入輸出格式 輸入格式 一行四個資料,棋盤的大小和馬的座標 輸出格式 乙個n m的矩陣,代表馬到達某個點最少要走幾步 左對齊,寬5格,不能到達則輸出 1 輸入樣例 1 3 3 1 1 輸出樣例 1 0 3 2 3 1 1 2 1 4bfs題,遍歷一下所有的位置 inc...
p1443馬的遍歷
就是一道很簡單的bfs,我為了練習一下queue型別的函式,第一次沒有用陣列模擬,直接上 這道題有乙個特殊的輸出,就是輸出寬五行,學習一下。include include include include include using namespace std int n,m const int ma...
P1443 馬的遍歷
有乙個n m的棋盤 1一行四個資料,棋盤的大小和馬的座標 乙個n m的矩陣,代表馬到達某個點最少要走幾步 左對齊,寬5格,不能到達則輸出 1 331 1032 3 11 214 include include include include using namespace std int chess...