ZOJ-1091 Knight moves

    技术2024-09-28  59

    ZOJ Problem Set - 1091 Knight Moves
    Time Limit: 1 Second      Memory Limit: 32768 KB

    A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.

    Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.

    Input Specification

    The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.

    Output Specification

    For each test case, print one line saying "To get from xx to yy takes n knight moves.".

    Sample Input

    e2 e4 a1 b2 b2 c3 a1 h8 a1 h7 h8 a1 b1 c3 f6 f6

    Sample Output

    To get from e2 to e4 takes 2 knight moves. To get from a1 to b2 takes 4 knight moves. To get from b2 to c3 takes 2 knight moves. To get from a1 to h8 takes 6 knight moves. To get from a1 to h7 takes 5 knight moves. To get from h8 to a1 takes 6 knight moves. To get from b1 to c3 takes 1 knight moves. To get from f6 to f6 takes 0 knight moves.
    Source: University of Ulm Local Contest 1996 /******************************************************** **name:zoj problem set 1091-night moves * **coder:mike * **date:2011-02-01 * **note:nothing * **email:creativewang@163.com * **blog:blog.csdn.net/creativewang * *********************************************************/ #include<stdio.h> #include<stdlib.h> #include<assert.h> #include<string.h> #define MAP_SIZE 8 #define BUF_SIZE 8 #define PRINT(X1,Y1,X2,Y2,N) printf("To get from %c%c to %c%c takes %d knight moves./n",(X1)+'a',(Y1)+'1',(X2)+'a',(Y2)+'1',(N)) int map[MAP_SIZE][MAP_SIZE]; int xx,yy,XX,YY; /**< 之所以用全局变量是因为传参数的话参数个数太多 */ int flag=1; /*-------------------QUEUE.H-<BELOW>---------------------*/ typedef struct QueueNode { int x; int y; int depth; struct QueueNode* next; }qnode; typedef struct Queue { qnode* head; qnode* tail; }queue; int init(queue* q) { assert(q); q->head=q->tail=NULL; return 0; } qnode* cnode(int x,int y,int depth) /**< creat a new node */ { qnode* node=(qnode*)malloc(sizeof(qnode)); assert(node); node->x=x; node->y=y; node->depth=depth; node->next=NULL; return node; } int enqueue(queue* q,int x,int y,int depth) /**< 入队 */ { qnode* node=cnode(x,y,depth); if(q->head==NULL&&q->tail==NULL) q->head=q->tail=node; else { q->tail->next=node; q->tail=node; } return 0; } int dequeue(queue* q,int* x,int* y,int* depth) /**< 出队 */ { assert(q->head); qnode* tmp=q->head; *x=tmp->x; *y=tmp->y; *depth=tmp->depth; if(q->head==q->tail) q->head=q->tail=NULL; else q->head=tmp->next; free(tmp); return 0; } int destroy(queue* q) { qnode* tmp; while(q->head!=q->tail) { tmp=q->head; q->head=tmp->next; free(tmp); } if(q->head) free(q->head); free(q); return 0; } int clear(queue* q) { qnode* tmp; while(q->head!=q->tail) { tmp=q->head; q->head=tmp->next; free(tmp); } free(q->head); q->head=q->tail=NULL; return 0; } int isEmpty(queue* q) /**< 判空 */ { return q->head==NULL&&q->tail==NULL; } /*-------------------QUEUE.H <ABOVE>-------------------------*/ int getInput(int* x,int* y,int* X,int* Y) { char buf[BUF_SIZE]; if(fgets(buf,BUF_SIZE,stdin)==NULL) /**< fgets 遇到文件尾部返回NULL */ return -1; *x=buf[0]-'a'; *y=buf[1]-'1'; *X=buf[3]-'a'; *Y=buf[4]-'1'; return 0; } int jump(queue* q,int x,int y,int depth) { if(x+2<MAP_SIZE&&y+1<MAP_SIZE&&map[x+2][y+1]!=flag) /**< (1,-2) */ { if(x+2==XX&&y+1==YY) return 1; enqueue(q,x+2,y+1,depth); } if(x+1<MAP_SIZE&&y+2<MAP_SIZE&&map[x+1][y+2]!=flag) /**< (2,-1) */ { if(x+1==XX&&y+2==YY) return 1; enqueue(q,x+1,y+2,depth); } if(x>0&&y+2<MAP_SIZE&&map[x-1][y+2]!=flag) /**< (2,1) */ { if(x-1==XX&&y+2==YY) return 1; enqueue(q,x-1,y+2,depth); } if(x>1&&y+1<MAP_SIZE&&map[x-2][y+1]!=flag) /**< (1,2) */ { if(x-2==XX&&y+1==YY) return 1; enqueue(q,x-2,y+1,depth); } if(x+2<MAP_SIZE&&y>0&&map[x+2][y-1]!=flag) /**< (-1,-2) */ { if(x+2==XX&&y-1==YY) return 1; enqueue(q,x+2,y-1,depth); } if(x+1<MAP_SIZE&&y>1&&map[x+1][y-2]!=flag) /**< (-2,-1) */ { if(x+1==XX&&y-2==YY) return 1; enqueue(q,x+1,y-2,depth); } if(x>0&&y>1&&map[x-1][y-2]!=flag) /**< (-2,1) */ { if(x-1==XX&&y-2==YY) return 1; enqueue(q,x-1,y-2,depth); } if(x>1&&y>0&&map[x-2][y-1]!=flag) /**< (-1,2) */ { if(x-2==XX&&y-1==YY) return 1; enqueue(q,x-2,y-1,depth); } return 0; } int main(void) { int x,y; int depth; queue* q=(queue*)malloc(sizeof(queue)); init(q); while(getInput(&xx,&yy,&XX,&YY)!=-1) { if(xx==XX&&yy==YY) { PRINT(xx,yy,XX,YY,0); continue; } enqueue(q,xx,yy,0); while(!isEmpty(q)) { dequeue(q,&x,&y,&depth); map[x][y]=flag; if(jump(q,x,y,depth+1)) { PRINT(xx,yy,XX,YY,depth+1); break; } } flag++; clear(q); } destroy(q); return 0; }
    最新回复(0)