sicily 1153. 马的周游问题
来源:互联网 发布:c语言程序苏小红实验题 编辑:程序博客网 时间:2024/05/29 02:29
1153. 马的周游问题
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB , Special Judge
Description
和题目C同样的任务,这里只是把棋盘扩大到标准的国际象棋。对这样一个8 * 8的棋盘用同样的方法编号如下:
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64
Input
输入有若干行。每行一个整数N(1<=N<=64),表示马的起点。最后一行用-1表示结束,不用处理。
Output
对输入的每一个起点,求一条周游线路。对应地输出一行,有64个整数,从起点开始按顺序给出马每次经过的棋盘方格的编号。相邻的数字用一个空格分开。
Sample Input
4-1
Sample Output
注意:如果起点和输入给定的不同,重复多次经过同一方格或者有的方格没有被经过,都会被认为是错误的。
题目分析
1152的题目,只是棋盘变大了,这时候不优先搜索可能性少的节点,会大大增加其分支的搜索回溯时间
所以将现在可达的位置按照其下一步可达位置的数量排序,
先走可能性最少的路径,从而减少回溯的时间
#include <cstdio>#include <iostream>#include <algorithm>#include <memory.h>#define ROW 8#define COL 8#define ALL 64struct Node { int r, c, next;};int path[ALL];int count;int visited[ROW][COL];int dir[8][2] = {1,2,2,1,-1,2,-2,1,-1,-2,-2,-1,1,-2,2,-1};bool com(Node a, Node b) { return a.next < b.next;}bool canGo(int r, int c) { return 0 <= r && r < ROW && 0 <= c && c < COL && !visited[r][c];}void travel(int row, int col) {//std::cout << row*COL + col + 1 << " "; path[count++] = row*COL + col + 1; visited[row][col] = true; if (count == ALL) return ; Node nodes[8]; int nextcount = 0; for (int i = 0; i < 8; ++i) { int nextr = row + dir[i][0]; int nextc = col + dir[i][1]; if (canGo(nextr, nextc)) { int nextnext = 0; for (int j = 0; j < 8; ++j) { if (canGo(nextr+dir[j][0], nextc+dir[j][1])) nextnext++; } nodes[nextcount].r = nextr; nodes[nextcount].c = nextc; nodes[nextcount].next = nextnext;//std::cout << nextr*COL + nextc + 1 << " "; nextcount++; } } std::sort(nodes, nodes+nextcount, com); for (int i = 0; i < nextcount; ++i) { travel(nodes[i].r, nodes[i].c); if (count == ALL) return; } count--; visited[row][col] = false;}int main(){ int num; while(scanf("%d", &num)) { if (num == -1) break; memset(visited, 0, sizeof(visited)); count = 0; travel((num-1)/COL, (num-1)%COL); for (int i = 0; i < ALL; ++i) if (i == ALL-1) printf("%d\n", path[i]); else printf("%d ", path[i]); }}
- sicily 1153.马的周游问题
- Sicily 1153. 马的周游问题
- Sicily 1153. 马的周游问题
- [sicily]1153. 马的周游问题
- sicily 1153. 马的周游问题
- Sicily马周游问题
- sicily 1152 简单的马周游问题
- sicily 1153 马的周游问题
- Sicily 1152 简单的马周游问题
- Sicily 1153 马的周游问题
- sicily 1153 马的周游问题
- sicily 1152 简单的马周游问题
- sicily 1152.简单的马周游问题
- Sicily 1152.简单的马周游问题
- Sicily 1152. 简单的马周游问题
- Sicily 1152 简单的马周游问题
- Sicily 1153 马的周游问题
- Sicily 1152. 简单的马周游问题
- SQL 关于LEFT JOIN关键字解析(多表查询)
- SOAPUI压力测试的参数配置
- SO_REUSEADDR和SO_REUSEPORT
- 使用PHP搭建自己的MVC框架
- TIPTOP實務--退貨折讓向廠商收款的作法(支票)
- sicily 1153. 马的周游问题
- Android字体多样式和动画TextDrawable
- Python爬虫:动态网页抓取淘宝“淘女郎”照片
- TIPTOP實務--退貨折讓向廠商收款的作法(匯款)
- Android如何防止apk程序被反编译
- 再没有任何国家政府比今天的中国政府蒙受更多的冤屈了
- Ubuntu ibus输入法选中删除
- TIPTOP實務--工單出現超額報工問題
- 基于CAShapeLayer和贝塞尔曲线的圆形进度条动画【原创】