POJ2488==DFS+路径输出+字典序排列
2016-03-07 12:10
411 查看
/* * POJ 2488 * DFS进行遍历就好,记录走过的路径,只要不重复地走过p*q个方格就行了(结束条件) *
/* 这里注意几个问题: 1、国际象棋,横着是字母,竖着是数字。 2、是按字典序输出的,所以搜索方向上一定要注意!这里是个坑。 3、忽略“The knight can start and end on any square of the board.”这句话,这也 算是个坑,实际上只要从A1点开始搜索就可以了,只要能从A1开始能搜到一条遍历全棋盘 的路径,那么肯定从棋盘上的任意一个点作为起点都能搜出一条遍历棋盘的路径,并且题目 要求要按字典序输出,所以只需从起点开始搜索就可以了! */ [code]#include<iostream> #include<cstring> #include<cstdio> #include<cmath> using namespace std; const int maxn = 50; int dirx[] = {-2, -2, -1, -1, 1, 1, 2, 2}; int diry[] = {-1, 1, -2, 2, -2, 2, -1, 1}; int N, R, C, vis[maxn][maxn]; bool ans; struct node{ int x, y; node(int _x=0, int _y=0):x(_x), y(_y){} }path[maxn*maxn]; void Print() { for(int i = 1; i <= N; i++) { cout << char(path[i].x+64) << path[i].y; } cout << endl; } bool dfs(int cur, int x, int y) { if(cur == N) return true; for(int i = 0; i < 8; i++) { int tx = x + dirx[i]; int ty = y + diry[i]; if(tx<=R && tx >=1 && ty <= C && ty >= 1 && !vis[tx][ty]) { vis[tx][ty] = 1; path[cur+1] = node(tx, ty); if(dfs(cur+1, tx, ty)) return true; vis[tx][ty] = 0; } } return false; } int main() { int T; cin >> T; int cas = 0; while(T--) { ans = false; cin >> C >> R; N = R*C; memset(vis, 0, sizeof(vis)); vis[1][1] = 1; path[1] = node(1, 1); ans = dfs(1, 1, 1); cout << "Scenario #" << ++cas << ":" << endl; if(ans) Print(); else cout << "impossible" << endl; if(T) cout << endl; } return 0; } //AC
[/code]
相关文章推荐
- javascript正則表達式 "\b"问题
- postgre常用系统函数
- 竞态条件
- iOS 第三方Reachability网络检测
- DrawerLayout实现侧滑菜单
- 搜索实现最近联系人 thinkPHP框架
- 隐藏css中超出TD宽度的字段的方法
- bzoj1855: [Scoi2010]股票交易
- 微信公众号开发接入指南
- iOS集成环信聊天和EaseUI之后出现警告:Warning! ivar size mismatch in PSUICollectionView_ - can't change the supercl
- iOS键盘类型UIKeyboardType(模拟器效果+真机效果)
- 【转】MyBatis学习总结(一)——MyBatis快速入门
- 地图location定位
- MFC中关于控件和变量对应方法
- sql order by错top(100)percent
- 简单总结RectF、Rect 和Matrix ,还有Paint的使用方法
- AsyncQueryHandler 异步查询数据
- 【前端】require函数实现原理
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-002-把用户数据存在memory里(AuthenticationManagerBuilder、 UserDetailsManagerConfigurer.UserDetailsBuilder)
- LVS持久连接分类