• CodeForces 370A Rook, Bishop and King


    此题看似很简单,但实际上有不少细节,WA点不少。分情况处理即可。

    #include<cmath>
    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    bool inMap(int x, int y){
        return x > 0 && x < 9 && y > 0 && y < 9;
    }
    int main(){
        int r1, c1, r2, c2, ans1, ans2, ans3;
        //freopen("in.cpp", "r", stdin);
        while(~scanf("%d%d%d%d", &r1, &c1, &r2, &c2)){
            if(r1 == r2 && c1 == c2) printf("0 0 0
    ");
            else{
                if(r1 == r2 || c1 == c2) ans1 = 1;
                else ans1 = 2;
                if(abs(r1 - r2) == abs(c1 - c2)) ans2 = 1;
                else if(r1 == r2 || c1 == c2){ 
                    int tmp1 = abs(r1 - r2);
                    int tmp2 = abs(c1 - c2);
                    if((tmp1 % 2 == 0 && tmp1) || (tmp2 % 2 == 0 && tmp2)) ans2 = 2;
                    else ans2 = 0;
                }
                else{
                    int flag = 0, x[4], y[4];
                    for(int i = 1;i < 8;i ++){
                        x[0] = r1 + i, y[0] = c1 + i;
                        x[1] = r1 - i, y[1] = c1 + i;
                        x[2] = r1 + i, y[2] = c1 - i;
                        x[3] = r1 - i, y[3] = c1 - i;
                        for(int j = 0;j < 4;j ++){
                            if(inMap(x[j], y[j]) && abs(x[j] - r2) == abs(y[j] - c2)){
                                flag = 1;
                                break;
                            }
                        }
                        if(flag) break;
                    }
                    if(flag) ans2 = 2;
                    else ans2 = 0;
                }
                if(abs(r1 - r2) == abs(c1 - c2)) ans3 = abs(r1 - r2);
                else{
                    int tmp1 = abs(r1 - r2) + abs(abs(c1 - c2) - abs(r1 - r2));
                    int tmp2 = abs(c1 - c2) + abs(abs(r1 - r2) - abs(c1 - c2));
                    ans3 = min(tmp1, tmp2);
                }
                printf("%d %d %d
    ", ans1, ans2, ans3);
            }
        }
        return 0;
    }


  • 相关阅读:
    C语言之分支语句
    C语言之运算符与表达式
    C语言之数据类型④——中文字符
    独特的对象引用:this
    理解赋值“=”的含义
    编写类的 “模板”
    类的定义
    Java语言规范
    第一周总结
    定义常量
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3933136.html
Copyright © 2020-2023  润新知