zoukankan      html  css  js  c++  java
  • csuoj 残缺的棋盘

    Description

    Input

    输入包含不超过10000 组数据。每组数据包含6个整数r1, c1, r2, c2, r3, c3 (1<=r1, c1, r2, c2, r3, c3<=8). 三个格子A, B, C保证各不相同。

    Output

    对于每组数据,输出测试点编号和最少步数。

    Sample Input

    1 1 8 7 5 6
    1 1 3 3 2 2

    Sample Output

    Case 1: 7
    Case 2: 3

    代码:

    #include<cstdio>
    #include<queue>
    #include<iterator>
    using namespace std;
    int dis[9][9];
    int r1,c1,r2,c2,r3,c3;
    int x[8] = {0,1,1,1,0,-1,-1,-1};
    int y[8] = {1,1,0,-1,-1,-1,0,1};
    
    struct Point{
        int x;
        int y;
    }point[9][9];
    void init(){
        for(int i = 1;i <= 8;i++){
            for(int j = 1;j <= 8;j++) {
                point[i][j].x = i;
                point[i][j].y = j;
            }
        }
    }
    int bfs(){
        bool flag = false;
        queue<struct Point> q;
        q.push(point[r1][c1]);
        while(!flag){
            struct Point temp = q.front();
            q.pop();
            for(int i = 0;i < 8;i++){
                if(temp.x + x[i] >= 1 && temp.x + x[i] <= 8 && temp.y + y[i] >= 1 && temp.y + y[i] <= 8 && dis[temp.x][temp.y] != -1 && !(temp.x + x[i] != r3 && temp.y + y[i] != c3)){
                    q.push(point[temp.x + x[i]][temp.y + y[i]]);
                    dis[temp.x + x[i]][temp.y + y[i]] = dis[temp.x][temp.y] + 1;
                    if(temp.x + x[i] == r2 && temp.y + y[i] == c2) return dis[temp.x + x[i]][temp.y + y[i]];
                }
            }
        }
        return 0;
    }
    int main(){
        init();
        int n = 1;
        while(scanf("%d %d %d %d %d %d",&r1,&c1,&r2,&c2,&r3,&c3) == 6){
            for(int i = 1;i <= 8;i++){
                for(int j = 1;j <= 8;j++) dis[i][j] = -1;
            }
            dis[r1][c1] = 0;
            printf("Case %d: %d
    ",n++,bfs());
        }
        return 0;
    }
  • 相关阅读:
    第一部分 题目要求
    完全卸载oracle
    zabbix的面试题目总结
    性能优化之MySQL调优篇
    select与epoll、apache与nginx实现原理对比
    深度优化LNMP之PHP
    深度优化LNMP之Nginx (转)
    git常用命令
    ansible 安装与卸载软件
    java8两个List集合取交集、并集、差集、去重并集
  • 原文地址:https://www.cnblogs.com/tracy520/p/6974831.html
Copyright © 2011-2022 走看看