CSUOJ-1511: 残缺的棋盘

1511: 残缺的棋盘

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 169   Solved: 56
[Submit][Status][Web Board]

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

HINT

Source

湖南省第十届大学生计算机程序设计竞赛


很显然是一道BFS的题目


#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

int m[10][10];
int x[8]={-1,0,1,1,1,0,-1,-1};
int y[8]={-1,-1,-1,0,1,1,1,0};

int r1,c1,r2,c2,r3,c3;

struct node{
    int r;
    int c;
    int s;
};

bool limit(int a,int b)
{
    if(a<1||a>8||b<1||b>8)
        return false;
    if(a==r3&&b==c3)
        return false;
    if(m[a][b]==1)
        return false;
    return true;
}

int bfs()
{
    queue <node> q;
    int tx,ty,i;
    node s1,s;
    s1.r=r1;
    s1.c=c1;
    s1.s=0;
    m[r1][c1]=1;

    q.push(s1);
    while(!q.empty())
    {
        s1=q.front();
        q.pop();
        if(s1.r==r2&&s1.c==c2)
                return s1.s;
        for(i=0;i<8;i++)
        {
            tx=s1.r+x[i];
            ty=s1.c+y[i];
            if(!limit(tx,ty))
                continue;

            m[tx][ty]=1;

            s.r=tx;
            s.c=ty;
            s.s=s1.s+1;
            q.push(s);
        }
    }
}

int main()
{
    int c=0;
    while(cin>>r1>>c1>>r2>>c2>>r3>>c3)
    {
        memset(m,0,sizeof(m));
        printf("Case %d: %d\n",++c,bfs());
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值