同样是一个bfs水题。。。

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> St;
St start;
queue<St> sts;
map<St, int> dist;
int n, m, x, y;
const int dx[] = {-1, 1, 2, 2, 1, -1, -2, -2};
const int dy[] = {2, 2, 1, -1, -2, -2, -1, 1};
//-----------------
void bfs() {
sts.push(start);
while(!sts.empty()) {
St now = sts.front(); sts.pop();
int nowx = now.first;
int nowy = now.second;
for(int i = 0; i < 8; i++) {
int newx = nowx + dx[i];
int newy = nowy + dy[i];
if(newx > 0 && newx <= n && newy > 0 && newy <= m) {
St neww(newx, newy);
if(dist.count(neww)) continue;
dist[neww] = dist[now] + 1;
sts.push(neww);
}
}
}
}
void output() {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(i==x&&j==y) printf("%-5d", 0);
else {
St now(i, j);
int d = dist[now];
if(d) printf("%-5d", d);
else printf("%-5d", -1);
}
}
cout << endl;
}
}
//-----------------
int main() {
cin >> n >> m >> x >> y;
start.first = x;
start.second = y;
bfs();
output();
return 0;
}

不过莫名其妙不知道为什么最后一个点没有过

最新文章

  1. POJ-3061
  2. DragRow-GYF
  3. 《Linux内核设计与实现》CHAPTER5阅读梳理
  4. 阿牛OCX编程助手
  5. Java中 int和Integer的区别+包装类
  6. 【转】国外程序员收集整理的PHP资源大全
  7. 在Cocos2d-x中实现较为真实的云彩效果
  8. java IO流整理
  9. TCP长连接和短连接的区别
  10. P1052 过河
  11. VS2017做为Unity3D的脚本编辑器需要的最精简组件
  12. run `npm audit fix` to fix them, or `npm audit` for details
  13. Docker镜像加速设置
  14. MongoDB的账户与权限管理及在Python与Java中的登录
  15. Python MySQLdb模块连接操作mysql数据库实例_python
  16. Linq使用技巧及查询示例(一)
  17. Flink学习笔记:异步I/O访问外部数据
  18. cassandra 集群并发测试脚本
  19. Python常用模块(三)
  20. check_mk 之 Check Parameters

热门文章

  1. phpcms V9 改造:输出sql语句
  2. hdu 4004 二分 2011大连赛区网络赛D
  3. 用尽洪荒之力解决Apple Store ipv6审核通关---linux服务器支持ipv6
  4. AsyncTask下载JSON
  5. Linux常用命令_(进程管理)
  6. sublime快捷键-for mac
  7. Ubuntu14.04 Server amd64 配置 Apache+MySQL+Django
  8. BZOJ3356 : [Usaco2004 Jan]禁闭围栏
  9. 推荐一个非常COOL的开源相册程序!
  10. JAVA生成验证码