Chinese checkers 跳棋(广搜)

台州学院oj 

题目地址 http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=2655

题目大意:

直线上有n个格子,0,1,…,n-1.现在有两个一样的棋子放在(0,1)位置.现在每步可以按跳棋的规则移动一个棋子,问至少多少步可以将两个棋子移动到(n-2,n-1)或(n-1,n-2)位置  移动规则( |x-y|=|y-z|
题目分析:
BFS:
以两个棋子的位置定义状态(x,y)
定义初始状态F(0,1)=0;
BFS扩展下一层节点:
按照跳棋规则,不妨设x<y.那么节点(x,y)的下层节点:
(x-1,y),(x+1,y),(x,y-1),(x,y+1),(x,2*x-y),(2*y-x,y)
超时代码 没有记忆化 
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
#define clr(x) memset(x,0,sizeof(x))
#define INF 0x1f1f1f1f
int map[1005][1005];
int v[1005][1005];
int n;
struct node
{
    int x,y;
}st,tt,t;
int ok(int x)
{
    if(x>=0&&x<n)return 1;
    else return 0;
}
int main()
{
    int T,res,i,j;
    queue<struct node>q;
    scanf("%d",&T);
    while(T--)
    {
            clr(v);
            while(!q.empty())q.pop();
            scanf("%d",&n);
            st.x=0;st.y=1;
            q.push(st);
            st.x=1;st.y=0;
            q.push(st);
            for(i=0;i<n;i++)
            for(j=0;j<n;j++)
            {
                map[i][j]=99999;
            }
            map[0][1]=0;
            map[1][0]=0;
            while(!q.empty())
            {
                t=q.front();
                q.pop();
               if(ok(t.x+1)&&!v[t.x+1][t.y])
               {
                   if(map[t.x][t.y]+1<map[t.x+1][t.y])map[t.x+1][t.y]=map[t.x][t.y]+1;
                   v[t.x+1][t.y]=1;
                   tt.x=t.x+1;tt.y=t.y;
                   q.push(tt);
               }

                if(ok(t.x-1)&&!v[t.x-1][t.y])
                {
                   if(map[t.x][t.y]+1<map[t.x-1][t.y])map[t.x-1][t.y]=map[t.x][t.y]+1;
                   v[t.x-1][t.y]=1;
                   tt.x=t.x-1;tt.y=t.y;
                   q.push(tt);
                }
               if(ok(t.y+1)&&!v[t.x][t.y+1])
               {
                    if(map[t.x][t.y]+1<map[t.x][t.y+1])map[t.x][t.y+1]=map[t.x][t.y]+1;
                   v[t.x][t.y+1]=1;
                   tt.x=t.x;tt.y=t.y+1;
                   q.push(tt);
               }
               if(ok(t.y-1)&&!v[t.x][t.y-1])
               {
                   if(map[t.x][t.y]+1<map[t.x][t.y-1])map[t.x][t.y-1]=map[t.x][t.y]+1;
                   v[t.x][t.y-1]=1;
                   tt.x=t.x;tt.y=t.y-1;
                   q.push(tt);
               }
              if(ok(2*t.x-t.y)&&!v[t.x][2*t.x-t.y])
               {
                   if(map[t.x][t.y]+1<map[t.x][2*t.x-t.y])map[t.x][2*t.x-t.y]=map[t.x][t.y]+1;
                   v[t.x][2*t.x-t.y]=1;
                   tt.x=t.x;tt.y=2*t.x-t.y;
                   q.push(tt);
               }
               if(ok(2*t.y-t.x)&&!v[2*t.y-t.x][t.y])
               {
                   if(map[t.x][t.y]+1<map[2*t.y-t.x][t.y])map[2*t.y-t.x][t.y]=map[t.x][t.y]+1;
                   v[2*t.y-t.x][t.y]=1;
                   tt.x=2*t.y-t.x;tt.y=t.y;
                   q.push(tt);
               }
               
            }
            res=min(map[n-1][n-2],map[n-2][n-1]);
            printf("%d\n",res);
            
    }
    return 0;
}

打表AC
#include<stdio.h>
int ans[1010]={0,0,0,1,2,3,4,4,5,5,6,6,7,7,8,8,8,9,9,9,10,10,10,11,11,11,12,12,12,12,13,13,13,13,14,14,14,14,15,15,15,15,16,16,16,16,16,17,17,17,17,17,18,18,18,18,18,19,19,19,19,19,20,20,20,20,20,20,21,21,21,21,21,21,22,22,22,22,22,22,23,23,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,26,26,26,26,26,27,27,27,27,27,27,27,28,28,28,28,28,28,28,28,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,30,31,31,31,31,31,31,31,31,32,32,32,32,32,32,32,32,32,33,33,33,33,33,33,33,33,33,34,34,34,34,34,34,34,34,34,35,35,35,35,35,35,35,35,35,36,36,36,36,36,36,36,36,36,36,37,37,37,37,37,37,37,37,37,37,38,38,38,38,38,38,38,38,38,38,39,39,39,39,39,39,39,39,39,39,40,40,40,40,40,40,40,40,40,40,40,41,41,41,41,41,41,41,41,41,41,41,42,42,42,42,42,42,42,42,42,42,42,43,43,43,43,43,43,43,43,43,43,43,44,44,44,44,44,44,44,44,44,44,44,44,45,45,45,45,45,45,45,45,45,45,45,45,46,46,46,46,46,46,46,46,46,46,46,46,47,47,47,47,47,47,47,47,47,47,47,47,48,48,48,48,48,48,48,48,48,48,48,48,48,49,49,49,49,49,49,49,49,49,49,49,49,49,50,50,50,50,50,50,50,50,50,50,50,50,50,51,51,51,51,51,51,51,51,51,51,51,51,51,52,52,52,52,52,52,52,52,52,52,52,52,52,52,53,53,53,53,53,53,53,53,53,53,53,53,53,53,54,54,54,54,54,54,54,54,54,54,54,54,54,54,55,55,55,55,55,55,55,55,55,55,55,55,55,55,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,67,67,67,67,67,67,67,67,67,67,67,67,67,67,67,67,67,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,69,69,69,69,69,69,69,69,69,69,69,69,69,69,69,69,69,69,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,71,71,71,71,71,71,71,71,71,71,71,71,71,71,71,71,71,71,72,72,72,72,72,72,72,72,72,72,72,72,72,72,72,72,72,72,72,73,73,73,73,73,73,73,73,73,73,73,73,73,73,73,73,73,73,73,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,82,82,82,82,82,82,82,82,82,82,82,82,82,82,82,82,82,82,82,82,82,83,83,83,83,83,83,83,83,83,83,83,83,83,83,83,83,83,83,83,83,83,84,84,84,84,84,84,84,84,84,84,84,84,84,84,84,84,84,84,84,84,84,84,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,86,86,86,86,86,86,86,86,86,86,86,86,86,86,86,86,86,86,86,86,86,86,87,87,87,87,87,87,87,87,87,87};
int main(){
    int T,n;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        printf("%d\n",ans[n]);
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值