栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1091 Knight Moves

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1091 Knight Moves

#include <stdio.h>#include <stdlib.h>#include <string.h>int state[9][9];int count[9][9];int Queue[100000];int step[8][2] = {1,2, 1,-2, -1,2, -1,-2, 2,1, 2,-1, -2,1, -2,-1};int head,tail;int push(int x){    Queue[head++] = x;}int pop(void){    return Queue[tail++];}int Qempty(void){    if( head == tail )        return 1;    return 0;}void init(void){    head = 0; tail = 0;    memset( state,0,sizeof(state) );    memset( count,0,sizeof(count) );    memset( Queue,0,sizeof(Queue) );}int main(void){    int a,b,x,y,temp,tempa,tempx,ta,tx,i;    char ch1,ch2,n;    while( scanf("%c%d %c%d%c",&ch1,&x,&ch2,&y,&n)!=EOF )    {        init();        a = ch1 - 'a' + 1;        b = ch2 - 'a' + 1;        push(a); push(x);        state[a][x] = 1;        while( !Qempty() )        { tempa = pop(); tempx = pop(); if( tempa == b && tempx == y)     break; for(i=0; i<8; i++)  {     ta = tempa + step[i][0];     tx = tempx + step[i][1];     if(state[ta][tx] == 0 && ta>=1 && ta<=8 && tx>=1 && tx<=8 )     {         push(ta); push(tx);         state[ta][tx] = 1;         count[ta][tx] = count[tempa][tempx] + 1;     } }        }        printf("To get from %c%d to %c%d takes %d knight moves.n",ch1,x,ch2,y,count[tempa][tempx]);    }return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372070.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号