典型地图搜索 pku 2488 A Knight's Journey
2010-08-26 01:02
435 查看
题目:
http://acm.pku.edu.cn/JudgeOnline/problem?id=2488
http://acm.pku.edu.cn/JudgeOnline/problem?id=2488
#include <stdio.h> #include <memory> #define max 27 struct Step{ char x; int y; }steps[max * max]; //int dir[8][2] = {{-2,-1},{-2,1},{2,-1},{2,1},{-1,-2},{-1,2},{1,-2},{1,2}}; int dir[8][2] = {{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; int map[max][max]; int row,col; bool inside(int i,int j){ if(1 <= i && i <= row && j >= 1 && j <= col) return true; return false; } bool dfs(int i,int j,int count){ //当前在第i行,j列格子. //从该格子出发,访问下一个格子 //直到所有的格子都已经走过的时候. //或者有些格子不能访问.函数结束. steps[count].x = 'A' + i - 1; steps[count].y = j; if(count == row * col) return true; for(int k = 0;k < 8;k++){ int nr = i + dir[k][0]; int nc = j + dir[k][1]; if(inside(nr,nc) && !map[nr][nc]){ map[nr][nc] = 1; //从nr,nc位置继续深度优先访问棋盘 if(dfs(nr,nc,count + 1)) return true; //回溯 map[nr][nc] = 0; } } return false; } int main(){ int t; //FILE *fp = fopen("data.txt","r"); scanf("%d",&t); //fscanf(fp,"%d",&t); for(int i = 1;i <= t;i++){ scanf("%d%d",&col,&row); //fscanf(fp,"%d%d",&col,&row); memset(map,0,sizeof(map)); memset(steps,0,sizeof(steps)/sizeof(Step)); printf("Scenario #%d:/n",i); map[1][1] = 1; if(dfs(1,1,1)){ for(int j = 1;j <= row * col;j++) printf("%c%d",steps[j].x,steps[j].y); printf("/n/n"); } else printf("impossible/n/n"); } return 0; }
相关文章推荐
- PKU 2488 深度优先搜索
- pku 2488 A Knight's Journey (搜索 DFS)
- pku 2488 A Knight's Journey (搜索 DFS)
- 微信小程序---地图使用(定位/展示/周边信息展示/poi搜索)
- 高德js_API开发O2O项目客户端地图定位周边检索 定位选址 POI搜索选址 多边形范围指定城市 不再范围不可服务
- poj 3984 迷宫问题 —— 典型搜索--DFS递归
- 高德地图之定位篇----->定位、预测天气、围栏、搜索周边、行踪轨迹
- google map v3 地图搜索功能
- POJ 2488 A Knight's Journey(简单搜索)
- pku 3187 Backward Digit Sums搜索
- ios 地图大头针固定在地图中心,用用地图中心点左边逆地理编码,及高德云图周边搜索
- 学术搜索的地图界面优化——自动显示机构信息
- 仿百度外卖3_7_1百度地图拖动定位,显示附近地址poi提示搜索
- K.Teamwork Brings Profits! 典型的dfs搜索
- google api 地图搜索应用
- 搜索结果排序算法的研究(张天/ccl.pku.edu.cn)
- 搜索 -dfs-POJ-2488-A Knight's Journey
- 高德地图自定义搜索
- pku1085 Triangle War 完全极大极小搜索+状压DP
- pku2157 Maze (搜索)