poj2488-A Knight's Journey
来源:互联网 发布:单片机hex文件下载 编辑:程序博客网 时间:2024/05/19 23:13
题目链接:点击打开链接
dfs水题,给出棋盘尺寸,求出马走遍棋盘的路径,或者输出impossible,路径要按字典序输出。
首先国际象棋的马走L路线,横向为字母A—Z,纵向为1—26,要做到字典序最小,第一步首先要最小,要从A1开始,搜索时也要按字典序最小搜索,从左到右,
从上到下,若马的坐标为C3,则搜索顺序为:A2,A4,B1,B5,D1,D5,E2,E4。
//172K16MS//C++1016B#include <cstdio>#include <cstring>int d[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};int vis[27][27];struct step{ int x,y;}s[26*26];int n,m,step,flag;void dfs(int x,int y,int dp){ if(n*m==dp) { flag=1; return; } for(int i=0;i<8;i++) { int sx=x+d[i][0]; int sy=y+d[i][1]; if(sx>=0&&sx<n&&sy>=0&&sy<m&&!vis[sx][sy]) { s[dp].x=sx; s[dp].y=sy; vis[sx][sy]=1; dfs(sx,sy,dp+1); vis[sx][sy]=0; if(flag) return; } }}int main(){ int k,t,i,l=0; scanf("%d",&t); for(k=1;k<=t;k++) { flag=0; memset(vis,0,sizeof(vis)); scanf("%d%d",&n,&m); printf("Scenario #%d:\n",++l); vis[0][0]=1; dfs(0,0,1); if(flag) for(i=0;i<n*m;i++) printf("%c%d",s[i].y+'A',s[i].x+1); else printf("impossible"); printf("\n\n"); } return 0;}
0 0
- poj2488 A Knight's Journey
- poj2488 - A Knight's Journey
- poj2488 A Knight's Journey
- POJ2488--A Knight's Journey
- POJ2488:A Knight's Journey
- poj2488 A Knight's Journey
- poj2488 A Knight's Journey
- poj2488---A Knight's Journey
- POJ2488---A Knight’s Journey
- poj2488 A Knight's Journey
- Poj2488 A Knight's Journey
- POJ2488 A Knight's Journey
- poj2488 A Knight's Journey
- poj2488 A Knight's Journey
- poj2488-A Knight's Journey
- POJ2488-A Knight's Journey
- poj2488 A Knight's Journey
- poj2488——A Knight's Journey
- iPhone6 & 6 Plus 视觉设计适配说明
- Entity Framework7 入门之全功能.NET(Console, WinForms, WPF等)使用EF7
- c# SQl server数据库与下拉框的绑定
- Pascal's Triangle II
- android 实现textview部分文字点击效果,类似于微博的话题丶用户
- poj2488-A Knight's Journey
- 首部检验和计算
- 简单cowboy http请求回应
- Redis3.0.7 服务端集群搭建
- 第五课、Scala类
- ListView的Item显示动画的简单实现
- HDU 2064 汉诺塔III 和 HDU 2077 汉诺塔IV
- 学习Object-C的碰到的一些问题或者说面试题
- CentOS下jetty安装与配置