骑士周游详解
- 格式:docx
- 大小:48.64 KB
- 文档页数:18
A Knight's JourneyTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14944 Accepted: 4991DescriptionBackgroundThe knight is getting bored of seeing the same black and white squares again and again and has decided to make a journeyaround the world. Whenever a knight moves, it is two squares in one direction and one square perpendicular to this. The world of a knight is the chessboard he is living on. Our knight lives on a chessboard that has a smaller area than a regular 8 * 8 board, but it is still rectangular. Can you help this adventurous knight to make travel plans?ProblemFind a path such that the knight visits every square once. The knight can start and end on any square of the board.InputThe input begins with a positive integer n in the first line. The following lines contain n test cases. Each test case consists of a single line with two positive integers p and q, such that 1 <= p * q <= 26. This represents a p * q chessboard, where p describes how many different square numbers 1, . . . , p exist, q describes how many different square letters exist. These are the first q letters of the Latin alphabet: A, . . .OutputThe output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the lexicographically first path that visits all squares of the chessboard with knight moves followed by an empty line. The path should be given on a single line by concatenating the names of the visited squares. Each square name consists of a capital letter followed by a number.If no such path exist, you should output impossible on a single line.Sample Input31 12 34 3Sample OutputScenario #1:A1Scenario #2:impossibleScenario #3:A1B3C1A2B4C2A3B1C3A4B2C4这道简单的题目竟然花了我一个晚上,本来相练习一下用权值来解决这道经典的骑士周游问题,没想到这道题用骑士周游的优化算法竟然超时(用自己的数据跑但是oj上是AC的),oj上跑了813ms 下面是我的code:#include<stdio.h>#include<string.h>#include<stdlib.h>#define lne 27int p,q;int x[lne],y[lne],ans_x[lne],ans_y[lne];int der[8][2]={{-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}, {-1, 2}, {-2, 1}}; bool vis[lne][lne],flag;typedefstruct node{int p,v;}cor;bool canjump(int r,int c){if(r>=0&&r<p&&c>=0&&c<q&&!vis[r][c])returntrue;returnfalse;}int weight(int r,int c){int i,count;for(i=count=0;i<8;i++)if(canjump(r+der[i][0],c+der[i][1]))count++;return count;}int cmp(constvoid *a,constvoid *b){cor *c=(cor *)a;cor *d=(cor *)b;return c->v>d->v?1:-1;}void backtrace(int cur,int r,int c){int i,k,nx,ny;if(cur==p*q){bool fl=false;flag=false;for(i=0;i<p*q;i++){if((ans_y[i]>y[i])||(ans_x[i]>x[i]&&ans_y[i]==y[i])){fl=true;break;}elseif((ans_y[i]<y[i])||(ans_x[i]<x[i]&&ans_y[i]==y[i])) break;}if(fl)for(;i<p*q;i++){ans_x[i]=x[i];ans_y[i]=y[i];}return ;}else{cor ds[8];for(i=k=0;i<8;i++){nx=r+der[i][0];ny=c+der[i][1];if(canjump(nx,ny)){ds[k].v=weight(nx,ny);ds[k++].p=i;}}qsort(ds,k,sizeof(ds[0]),cmp);for(i=0;i<k;i++){nx=r+der[ds[i].p][0];ny=c+der[ds[i].p][1];vis[nx][ny]=true;x[cur]=nx; y[cur]=ny;backtrace(cur+1,nx,ny);vis[nx][ny]=false;}}}void deal(){int i,j;for(i=0;i<p*q;i++)ans_x[i]=ans_y[i]=50;vis[0][0]=true;backtrace(1,0,0);}void output(){int i;if(flag) puts("impossible\n");else{for(i=0;i<p*q;i++)printf("%c%d",ans_y[i]+'A',ans_x[i]+1);puts("\n");}}int main(void){int ncase,i;scanf("%d",&ncase);for(i=1;i<=ncase;i++){memset(vis,false,sizeof(vis));scanf("%d%d",&p,&q);printf("Scenario #%d:\n",i);flag=true;deal();output();}return 0;}然而上面的代码对于8*8的棋盘还是有可行性的,纯粹是数据问题,呵呵后来没办法只能用bfs或者dfs水过,但是由于这道题目只要求求最小字典序的那个结果所以用dfs就可以了,不出意外的话,这道题用dfs做,反而比bfs快,下面是AC的code 16ms :#include<stdio.h>#include<string.h>#include<stdlib.h>#define lne 27int p,q;int x[lne];char y[lne];int der[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}}; bool vis[lne][lne];bool backtrace(int cur,int r,int c){int i,nx,ny;if(cur==p*q-1){x[cur]=r; y[cur]=c+'A';returntrue;}else{for(i=0;i<8;i++){nx=r+der[i][0];ny=c+der[i][1];if(nx>=0&&nx<p&&ny>=0&&ny<q&&!vis[nx][ny]){vis[nx][ny]=true;x[cur]=r; y[cur]=c+'A';if(backtrace(cur+1,nx,ny))returntrue;vis[nx][ny]=false;}}}returnfalse;}int main(void){int ncase,i;scanf("%d",&ncase);for(i=1;i<=ncase;i++){memset(vis,false,sizeof(vis));scanf("%d%d",&p,&q);printf("Scenario #%d:\n",i);vis[0][0]=true;if(backtrace(0,0,0)){for(int j=0;j<p*q;j++)printf("%c%d",y[j],x[j]+1);putchar('\n');}elseputs("impossible");if(i!=ncase) putchar('\n');}return 0;}这是网上用bfs做的code:#include<iostream>usingnamespace std;bool visit[10][10],f;int t,m,i,j,ans[100][2],x,y,p,q;int move[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};void dg(int p1,int q1,int ans1){visit[p1][q1]=true;ans[ans1][0]=p1,ans[ans1][1]=q1;if (ans1==p*q){f=true;for (i=1;i<=ans1;i++)printf("%c%d",(char)((int)'A'+ans[i][1]-1),ans[i][0]);printf("\n");return;}for (int i1=0;i1<8;i1++){x=p1+move[i1][0],y=q1+move[i1][1];if (x>0&&x<=p&&y>0&&y<=q){if (visit[x][y]==false)dg(x,y,ans1+1);if (f) return;}}visit[p1][q1]=false;return;}int main(){//freopen("2488.in","r",stdin); //freopen("2488.out","w",stdout); scanf("%d",&m);t=0;while(m--){t++;scanf("%d %d",&p,&q);printf("Scenario #%d:\n",t);f=false;memset(visit,0,sizeof(visit));dg(1,1,1);if (!f) printf("impossible\n"); if (m) printf("\n");}return 0;}下面是大牛写的骑士周游的三种方法但是最后的一种贪心是错的/** File: KnightTravel1.cpp* Author: eshow* Date: 2007-09-10* Question:考虑国际象棋棋盘上某个位置的一只马,它是否可能只走63步,正好走过除起点外的其他63个位置各一次?如果有一种这样的走法,则称所走的这条路线为一条马的周游路线。