zoukankan      html  css  js  c++  java
  • 水题B

    国际象棋的棋盘是黑白相间的8 * 8的方格,棋子放在格子中间。如下图所示: 


    王、后、车、象的走子规则如下: 

    • 王:横、直、斜都可以走,但每步限走一格。
    • 后:横、直、斜都可以走,每步格数不受限制。
    • 车:横、竖均可以走,不能斜走,格数不限。
    • 象:只能斜走,格数不限。



    写一个程序,给定起始位置和目标位置,计算王、后、车、象从起始位置走到目标位置所需的最少步数。 
    Input第一行是测试数据的组数t(0 <= t <= 20)。以下每行是一组测试数据,每组包括棋盘上的两个位置,第一个是起始位置,第二个是目标位置。位置用"字母-数字"的形式表示,字母从"a"到"h",数字从"1"到"8"。 
    Output对输入的每组测试数据,输出王、后、车、象所需的最少步数。如果无法到达,就输出"Inf".Sample Input

    2
    a1 c3
    f5 f8

    Sample Output

    2 1 2 1
    3 1 1 Inf
    思路:算好横纵距离,每一种棋子用它的方法来处理。细节看注释。
    #include <bits/stdc++.h>
    using namespace std;
    int cmp(int x, int y){
    	if(x>y) return x;
    	else return y;
    }
    
    int main(){
    	int t;
    	cin>>t;
    	while(t--){
    		string a,b;
    		cin>>a>>b;
    		int x = abs(a[0]-b[0]); //起始位置和终点的横向距离 
    		int y = abs(a[1]-b[1]); //起始位置和终点的纵向距离 
    		
    		if(!x && !y) {
    			cout<<"0 0 0 0"<<endl; //如果起点和终点是一个位置,输出0 
    			continue;
    		} 
    	//王
    		cout<<cmp(x,y)<<" ";//大的就是步数。 
    		
    	//后
    		 if(x == y) cout<<1<<" "; //对角,一步到位 
    		 else if((!x&&y) || (!y&&x) || x % 2 == y % 2) cout<<1<<" "; //These is a question!!!
    		 else cout<<2<<" "; 
    		
    	//车
    		if((!x&&y) || (!y&&x)) cout<<1<<" "; //同列或者同行,需要一步 
    		else cout<<2<<" ";
    	
    	//象	
    		if(x == y) cout<<1<<endl; 
    		else if(x%2 == y%2) cout<<2<<endl;
    		else cout<<"Inf"<<endl; 
    	}
    	return 0;
    }
    
  • 相关阅读:
    linux -- Ubuntu 安装搜狗输入法
    linux -- Ubuntuserver图形界面下安装、配置lampp、phpmyadmin
    linux -- ubuntu14.10安装gnome桌面环境和dock工具
    linux -- Ubuntu Server 安装图形界面
    linux -- Ubuntu 命令技巧合集
    linux -- ubuntu 何为软件源
    linux -- Ubuntu报错“unable to locate package...”
    linux -- Ubuntu图形界面终端实现注销、关机、重启
    php -- 读取文本文件内容
    NEXTDAY
  • 原文地址:https://www.cnblogs.com/stul/p/9951485.html
Copyright © 2011-2022 走看看