zoukankan      html  css  js  c++  java
  • uva 439

    /*************************************************************************
         File Name: uva439.cpp
         Author: yubo
         Mail: yuzibode@126.com 
         Created Time: 2014/5/18 23:15:55
    reference:http://blog.sina.com.cn/s/blog_6730a3aa0100vkzd.html
    相似于中国象棋中的马的走法,可是这道题改来改去最终自己做出来了
    input:
    e2 e4
    a1 b2
    b2 c3
    a1 h8
    a1 h7
    h8 a1
    b1 c3
    f6 f6
     ************************************************************************/
    
    #include<iostream>
    #include<cstring>
    #include<queue>
    #include<cstdio>
    using namespace std;
    int dir[8][2]={
    	{2,1},{2,-1},{1,2},{1,-2},{-1,2},{-1,-2},{-2,1},{-2,-1},
    };
    typedef struct{
    	int x,y;
    	int times;
    
    }SE;
    int x1,y1,x2,y2;//
    int judge1(int i,int j)
    {
    	if(i>=0&&i<8&&j>=0&&j<8)
    		return 1;
    	else
    		return 0;
    
    }
    int vis[8][8];
    char command[2][5];//记录命令
    int ans;
    void bfs(int x,int y)
    {
    	queue<SE> q;
    	SE q0,q1,q2;
    	q0.x=x;
    	q0.y=y;
    	q0.times=0;
    	vis[x][y]=1;
    	q.push(q0);
    	while(!q.empty()){
    		q1=q.front();
    		q.pop();
    		for(int i=0;i<8;i++){
    			int temp1=q1.x+dir[i][0];
    			int temp2=q1.y+dir[i][1];
    			if(judge1(temp1,temp2)&&!vis[temp1][temp2]){
    				if(temp1==x2&&temp2==y2){
    					ans=q1.times+1;
    					return ;
    				}
    				q2.x=temp1;
    				q2.y=temp2;
    				q2.times=q1.times+1;
    				vis[temp1][temp2]=1;
    				q.push(q2);
    			}
    		}
    
    
    	}
    
    
    }
    int main()
    {
    //	freopen("in.txt","r",stdin);
    	while(cin>>command[0]>>command[1]){
    		memset(vis,0,sizeof(vis));
    		y1=command[0][0]-'a';
    		x1=command[0][1]-'1';
    		y2=command[1][0]-'a';
    		x2=command[1][1]-'1';
    		ans=0;
    //		printf("%d %d %d %d
    ",x1,y1,x2,y2);
    //		printf("%s
    ",command[0]);
    		bfs(x1,y1);
    		cout<<"To get from "<<command[0]<<" to "<<command[1]<<" takes "<<ans<<" knight moves."<<endl;
    	}
    }
    
    

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    DNS正向解析实现
    基于bind软件部署DNS服务器
    DNS解析工具使用案例
    DNS服务工作原理
    3
    .deb/.whl/.tgz/.tar/gz软件包安装方式(Ubuntu)
    博客系列目录
    Databricks 第3篇:pyspark.sql 通过JDBC连接数据库
    Databricks 第2篇:pyspark.sql 简介
    数据预处理 第4篇:数据预处理(sklearn 插补缺失值)
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4793774.html
Copyright © 2011-2022 走看看