惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
罗磊的独立博客
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
Apple Machine Learning Research
Apple Machine Learning Research
The Register - Security
The Register - Security
J
Java Code Geeks
V2EX - 技术
V2EX - 技术
Vercel News
Vercel News
N
News and Events Feed by Topic
腾讯CDC
P
Proofpoint News Feed
N
News | PayPal Newsroom
www.infosecurity-magazine.com
www.infosecurity-magazine.com
爱范儿
爱范儿
O
OpenAI News
酷 壳 – CoolShell
酷 壳 – CoolShell
月光博客
月光博客
Martin Fowler
Martin Fowler
Engineering at Meta
Engineering at Meta
D
Docker
Y
Y Combinator Blog
博客园 - 聂微东
G
Google Developers Blog
S
Security @ Cisco Blogs
Simon Willison's Weblog
Simon Willison's Weblog
S
Schneier on Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
阮一峰的网络日志
阮一峰的网络日志
C
CXSECURITY Database RSS Feed - CXSecurity.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
CERT Recently Published Vulnerability Notes
I
Intezer
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Attack and Defense Labs
Attack and Defense Labs
V
Visual Studio Blog
博客园 - Franky
博客园 - 三生石上(FineUI控件)
W
WeLiveSecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hugging Face - Blog
Hugging Face - Blog
Scott Helme
Scott Helme
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
LINUX DO - 最新话题
C
Cybersecurity and Infrastructure Security Agency CISA

博客园 - zealsoft

洛谷 P1816 忠诚题解 洛谷 P2384 最短路题解 洛谷 P2725 邮票题解 洛谷 P2722 总分题解 洛谷 P1219 八皇后题解 洛谷 P2921 在农场万圣节Trick or Treat on the Farm题解 洛谷 P1162 填涂颜色题解 洛谷 P1330 封锁阳光大学题解 洛谷 P1032 字串变换题解 洛谷 P1280 尼克的任务题解 洛谷 P1020导弹拦截题解 洛谷 P1141 01迷宫题解 VisualSVNServer无法卸载也无法安装,报告不是有效的MOF文件(0x8004401e)错误 视频捕捉的格式问题 一个VxWorks源代码网站 找个轻量级的Log库还挺难 TAU G2中的BitString和OctetString W32.Downadup.autorun病毒的清除 如何用Visual Studio 2005编译Wireshark的插件
洛谷 P1443 马的遍历题解
zealsoft · 2019-08-10 · via 博客园 - zealsoft

题目链接:https://www.luogu.org/problem/P1443

题目描述

有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步

输入格式

一行四个数据,棋盘的大小和马的坐标

输出格式

一个n*m的矩阵,代表马到达某个点最少要走几步(左对齐,宽5格,不能到达则输出-1)

输入输出样例

输出 #1

0    3    2    
3    -1   1    
2    1    4    

题解

此题是典型的BFS问题。不过和01迷宫问题有两点不同:一是马的走法不是上下左右,所以pos数组需要修改,二是走的步数需要从队列中元素的步数加1。还有一个小问题就是要控制cout的输出格式,刚开始没有注意,10个全WA了。

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 #include <algorithm>
 5 #include <string.h>
 6 
 7 using namespace std;
 8 
 9 struct Node
10 {
11     int x, y;
12     int step;
13 };
14 Node q[100005];
15 
16 const int MAXN = 1005;
17 int n, m, a, b, c, d, step, front, rear, ans[MAXN][MAXN]; 
18 int pos[8][2] = {2, 1, 2, -1, 1, 2, 1, -2, -1, 2, -1, -2, -2, 1, -2, -1};
19 bool vis[MAXN][MAXN];
20 
21 void bfs()
22 {
23     Node now, next;
24     now.x = a;
25     now.y = b;
26     vis[a][b] = 1; 
27     now.step = 0;
28     front = rear = 0;
29     q[rear] = now;
30     rear++;
31     while(front < rear)
32     {
33         now = q[front++];
34         for(int i = 0; i < 8; i++)
35         {
36             int nx = now.x + pos[i][0]; 
37             int ny = now.y + pos[i][1]; 
38             if(nx <= n && nx > 0 && ny <= m && ny > 0 
39                 && vis[nx][ny] == false) 
40             {
41                 vis[nx][ny] = true;
42                 q[rear].x = nx;
43                 q[rear].y = ny;
44                 q[rear].step = now.step + 1;
45                 ans[nx][ny] = q[rear].step;
46                 rear++;
47             }
48         } 
49     }
50 }
51 
52 int main()
53 {
54     cin >> n >> m >> a >> b;
55     for(int i = 1; i <= n; i++)
56     {
57         for(int j = 1; j <= m; j++)
58         {
59             ans[i][j] = -1;
60         }
61     }
62     ans[a][b] = 0;
63     step = 1;
64     bfs();
65     for(int i = 1; i <= n; i++)
66     {
67         for(int j = 1; j <= m; j++)
68         {
69             cout.width(5);
70             cout.setf(ios::left);
71             cout << ans[i][j];
72         }
73         cout << endl;
74     }
75     return 0;
76 }