马的遍历问题—贪心跑图
来源:互联网 发布:淘宝c店铺运营计划表 编辑:程序博客网 时间:2024/05/29 16:46
Description
在n*n棋盘上,对任一位置上放置的一个马,均能选择一个合适的路线,使得该棋子能按象棋的规则不重复地走过棋盘上的每一位置。
Input
输入第一行为测试数据组数。从第二行开始每行3个整数n(3<n<10)、x、y,代表棋盘的大小,和初始坐标。
Output
输出字典序最小的可行解,无解输出“No solution.”。格式见样例。
Sample Input
16 6 6
Sample Output
Case #1:
7 4 9 12 15 36
10 21 6 3 30 13
5 8 11 14 35 16
22 25 20 31 2 29
19 32 27 24 17 34
26 23 18 33 28 1
分析:比较水的一个题,dfs搜图,配合贪心最小字典序,适当剪下枝基本就能跑出来,注意贪心的时候,马要尽量先往上跑,往上跑的时候先往左跑,这样跑到的第一个数据就是所求字典序最小的那个。
7 4 9 12 15 36
10 21 6 3 30 13
5 8 11 14 35 16
22 25 20 31 2 29
19 32 27 24 17 34
26 23 18 33 28 1
分析:比较水的一个题,dfs搜图,配合贪心最小字典序,适当剪下枝基本就能跑出来,注意贪心的时候,马要尽量先往上跑,往上跑的时候先往左跑,这样跑到的第一个数据就是所求字典序最小的那个。
#include <cstdio>#include <algorithm>#include <iostream>#include <cstring>#include <vector>using namespace std;const int maxn = 33;const int inf = 0x3f3f3f3f;const int dr[] = { -2, -2, -1, -1, 1, 1, 2, 2 };const int dc[] = { -1, 1, -2, 2, -2, 2, -1, 1 };int n, ok, vis[maxn*maxn];void dfs(int pos, int step) { if (step == n*n) { ok = 1; for (int r = 1; r <= n; r ++) { for (int c = 1; c <= n; c ++) printf ("%3d", vis[(r-1)*n+c]); printf ("\n"); } return; } int r = (pos-1)/n + 1; int c = pos % n; if (!c) c = n; for (int i = 0; i < 8; i++) { int rr = r + dr[i], cc = c + dc[i], pos1 = (rr-1)*n+cc; if (rr < 1 || cc < 1 || rr > n || cc > n || vis[pos1]) continue; vis[pos1] = vis[pos] + 1; dfs(pos1, step + 1); if (ok) return; vis[pos1] = 0; }}int main() { int T, r, c; cin >> T; for (int tt = 1; tt <= T; tt ++) { cin >> n >> r >> c; memset(vis, 0, sizeof vis); vis[(r-1)*n+c] = 1; ok = 0; printf("Case #%d:\n", tt); dfs((r-1)*n+c, 1); if (!ok) printf ("No solution.\n"); }return 0;}
1 0
- 马的遍历问题—贪心跑图
- gym100206E (n*n)的马遍历问题 贪心
- 马的遍历(贪心)
- 马的遍历问题
- 马的遍历问题
- 马的遍历问题
- 马的遍历问题
- 马的遍历问题
- 马的遍历问题
- 中国象棋马的遍历问题
- 马的遍历问题-问题分析
- [算法]图算法之骑士遍历问题(象棋中马的遍历问题)分析,C语言实现
- 图的遍历算法-马遍历棋盘
- 图的遍历算法-马遍历棋盘
- 【数据结构】马踏棋盘的遍历问题
- 【经典算法问题】马的遍历【回溯】
- java一个图结构的遍历问题
- cursor遍历的问题
- 第十三届小波智能媒体技术与信息处理国际学术会议征文通知
- Ubuntu开机 网络配置等待很久 Waiting up to 60 more seconds for network configuration
- android在引用资源时使用@还是?
- android环境变量配置
- 九种浏览器端缓存方法知多少
- 马的遍历问题—贪心跑图
- js数组的一些方法join,split,sort
- 环境变量无效
- php 图像裁剪(自定义裁剪图片大小)
- Struts2中关于"There is no Action mapped for namespace / and action name"的错误解决
- Linux中一些常用命令
- 我花50元赚来190元的贴吧推广经验
- oracle用expdp导出数据库用户
- selenium:chromedriver与chrome版本的对应关系