P1443 马的遍历
2024-08-29 18:04:02
同样是一个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;
}
不过莫名其妙不知道为什么最后一个点没有过
最新文章
- POJ-3061
- DragRow-GYF
- 《Linux内核设计与实现》CHAPTER5阅读梳理
- 阿牛OCX编程助手
- Java中 int和Integer的区别+包装类
- 【转】国外程序员收集整理的PHP资源大全
- 在Cocos2d-x中实现较为真实的云彩效果
- java IO流整理
- TCP长连接和短连接的区别
- P1052 过河
- VS2017做为Unity3D的脚本编辑器需要的最精简组件
- run `npm audit fix` to fix them, or `npm audit` for details
- Docker镜像加速设置
- MongoDB的账户与权限管理及在Python与Java中的登录
- Python MySQLdb模块连接操作mysql数据库实例_python
- Linq使用技巧及查询示例(一)
- Flink学习笔记:异步I/O访问外部数据
- cassandra 集群并发测试脚本
- Python常用模块(三)
- check_mk 之 Check Parameters