Chinese checkers

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描述

I think almost everyone play Chinese checkers when we were young.

Recently, hft777 is fond of playing Chinese checkers. But he is puzzled with how many steps at least to move all his pieces to his opposite position.

To make the problem simple, now we take a line into account. There are n positions on a line, numbered from 0 to n-1.At fast, two same balls is on 0 and 1 position, and every time you can move a ball left or right, or you can move a ball on the position x to the position z on the other side of the ball on the position y, and |x-y|=|y-z|. And you must mark sure the ball can’t move out of the bounder, and the two balls can’t be on one same position. Now, hft777 wants to how many steps at least to move the two same to the n-2, n-1 positions.

输入

The first line of the input is one integer t, the number of testcase.
For each testcase, one integer n for a line, corresponding to the length of the line. 2≤n<1000 

输出

For each testcase output the minimum steps.

样例输入

2
3
4

样例输出

1
2

比赛时一直在找规律,但是因为推出的数据有点少,根据这些数据推出的规律是错的。虽然比赛时也想到了用广搜写,但是害怕超时就没尝试。现在想想真有点后悔,当时应该试试的。

题意:跳棋。有n个位置,从0~n-1编号,开始时两个球分别在0、1位置,目标状态是两个球在n-2、n-1位置。对于每次操作,可以向左(即向后)走一步,可以向右(即向前)走1步,也可以使一个球跳过另外一个球,跳之前的位置和调之后的位置距离另外一个球的距离相等,且每次操作都必须在0~n-1范围内。问从0、1位置到n-2、n-1位置最少需要操作多少次。

分析:用广搜把所有可能出现的情况都求出来,之后每输入一个值,直接输出答案即可。

用数组模拟队列:

//step[i][j]记录两个球分别到达i、j点的最小步数
//step[i][j]=0时表示后面的球在i位置、前面的球在j位置这个状态没有出现过
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
const int N = 1010;
int step[N][N];
int que[N*N][2];
int head, tail;void bfs(int n)
{int x, y, nx, ny;memset(step, 0, sizeof(step));step[0][1] = 0;que[0][0] = 0;que[0][1] = 1;head = 0;tail = 1;while(head < tail){x = que[head][0];y = que[head++][1];if((x == n - 1 && y  == n - 2) || (x == n - 2 && y == n - 1))return ;int mmax = max(x, y);int mmin = min(x, y);x = mmin;y = mmax;if(x - 1 > 0) //后面的球向后走1步{nx = x - 1;ny = y;if(!step[nx][ny]){step[nx][ny] = step[x][y] + 1;que[tail][0] = nx;que[tail++][1] = ny;}}if(x + 1 < n && x + 1 != y) //后面的球向前走1步{nx = x + 1;ny = y;if(!step[nx][ny]){step[nx][ny] = step[x][y] + 1;que[tail][0] = nx;que[tail++][1] = ny;}}if(y - 1 > 0 && y - 1 != x) //前面的球向后走1步{nx = x;ny = y - 1;if(!step[nx][ny]){step[nx][ny] = step[x][y] + 1;que[tail][0] = nx;que[tail++][1] = ny;}}if(y + 1 < n) //前面的球向前走一步{nx = x;ny = y + 1;if(!step[nx][ny]){step[nx][ny] = step[x][y] + 1;que[tail][0] = nx;que[tail++][1] = ny;}}if(y * 2 - x < n) //后面的球跨过前面的球向前跳{nx = y;ny = y * 2 - x;if(!step[nx][ny]){step[nx][ny] = step[x][y] + 1;que[tail][0] = nx;que[tail++][1] = ny;}}if(x * 2 - y >= 0) //前面的球跨过后面的球向后跳{nx = x * 2 - y;ny = x;if(!step[nx][ny]){step[nx][ny] = step[x][y] + 1;que[tail][0] = nx;que[tail++][1] = ny;}}}
}int main()
{int T, n;bfs(1005);scanf("%d",&T);while(T--){scanf("%d",&n);if(n == 2)printf("0\n");elseprintf("%d\n", step[n-2][n-1]);}return 0;
}

用STL里面的队列:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;const int N = 1010;
struct node
{int x;int y;
};
int step[N][N];void bfs(int n)
{queue <node> que;node a, b, c;memset(step, 0, sizeof(step));a.x = 0;a.y = 1;que.push(a);while(!que.empty()){b = que.front();que.pop();int x = min(b.x, b.y);int y = max(b.x, b.y);if((x == n - 1 && y == n - 2) || (x == n - 2 && y == n - 1))return ;if(x - 1 > 0)  //后面的球向后走1步{int nx = x - 1;int ny = y;if(!step[nx][ny]){c.x = nx;c.y = ny;que.push(c);step[nx][ny] = step[x][y] + 1;}}if(x + 1 < n && x + 1 != y)   //后面的球向前走1步{int nx = x + 1;int ny = y;if(!step[nx][ny]){c.x = nx;c.y = ny;que.push(c);step[nx][ny] = step[x][y] + 1;}}if(y - 1 != x && y - 1 > 0)  //前面的球向后走1步 {int nx = x;int ny = y - 1;if(!step[nx][ny]){c.x = nx;c.y = ny;que.push(c);step[nx][ny] = step[x][y] + 1;}}if(y + 1 < n)   //前面的球向前走一步{int nx = x;int ny = y + 1;if(!step[nx][ny]){c.x = nx;c.y = ny;que.push(c);step[nx][ny] = step[x][y] + 1;}}if(y * 2 - x < n)   //后面的球跨过前面的球向前跳{int nx = y;int ny = y * 2 - x;if(!step[nx][ny]){c.x = nx;c.y = ny;que.push(c);step[nx][ny] = step[x][y] + 1;}}if(x * 2 - y > 0)   //前面的球跨过后面的球向后跳 {int nx = x * 2 - y;int ny = x;if(!step[nx][ny]){c.x = nx;c.y = ny;que.push(c);step[nx][ny] = step[x][y] + 1;}}}
}int main()
{bfs(1005);int T, n;scanf("%d",&T);while(T--){scanf("%d",&n);if(n == 2)printf("0\n");elseprintf("%d\n",step[n-2][n-1]);}return 0;
}

NYOJ 692 Chinese checkers(广搜)相关推荐

  1. nyoj 999——师傅又被妖怪抓走了——————【双广搜】

    师傅又被妖怪抓走了 时间限制: 1000 ms  |  内存限制:65535 KB 难度: 3 描述 话说唐僧复得了孙行者,师徒们一心同体,共诣西方.自宝象国救了公主,承君臣送出城西,沿路饥餐渴饮,悟 ...

  2. Go 分布式学习利器(15) -- Go 实现 深搜和广搜

    强化语法,回顾算法. 通过Go语言实现 深度优先搜索 和 广度优先搜索,来查找社交网络中的三度好友关系(三度指的是一个节点到 其相邻节点 到 其相邻节点的节点 ,图递增三层好友关系). 涉及到的Go语 ...

  3. 搜索:广搜 词语阶梯

    问题描述以及解决过程如下导图 广搜实现如下 #include <iostream> #include <algorithm> #include <vector> # ...

  4. [NC23486]小A与小B 双向广搜

    题解:这个题属于走迷宫类型问题的一个升级版吧,不同之处在于一秒钟小A走一步,小B可以走两步,这两种事件是同时发生的,所以我们每秒钟让A扩散一步,让B扩散两步. 两个人走过的路径分别用 visited[ ...

  5. 【图论专题】BFS中的双向广搜 和 A-star

    双向广搜 AcWing 190. 字串变换 #include <cstring> #include <iostream> #include <algorithm> ...

  6. [数据结构] 迷宫问题(栈和队列,深搜和广搜)

    代码: #include <iostream> #include <string.h> #include <stack> #include <queue> ...

  7. UVA 122 Trees on the level 二叉树 广搜

    题目链接: https://vjudge.net/problem/UVA-122 题目描述: 给你一种二叉树的构造方法, 让你逐层输出二叉树的节点值, 如果不能够则输出"not comple ...

  8. hrbust 1616 密码锁(广搜)

    分析:广搜,每个四位数作为一个状态,从每个状态扩展出其他的几种状态并累加步数之后加入队列. 1 #include <stdio.h> 2 #include <string.h> ...

  9. [kuangbin] M - Find a way(简单广搜)

    题目链接:https://vjudge.net/contest/215603#problem/M 其中三维数组dis将两个广搜合并到了一起 #include<iostream> #incl ...

最新文章

  1. [Asp.net 5] Options-配置文件(2)
  2. 0.基于C++的图像处理算法实现、INTEL CPU上SSE加速、ARM CPU上NEON加速
  3. html判断对错,Html翻转校园试题
  4. [C/CPP系列知识] Type difference of character literals 和 bool in C and C++
  5. 人和计算机在时间管理方面的相似性
  6. Burpsuite工具的使用
  7. 大数据泄露你的行踪?隐私不再是隐私
  8. 福建信息技术学院计算机系男生宿舍怎么样,广西职业技术学院宿舍怎么样
  9. Linux之环境变量
  10. 绿茶2003服务器系统 新浪,我在用WIN server2003
  11. 82-Spark的StandLone模式调试
  12. Python 文件编码问题解决
  13. sql server 数据库创建链接服务器访问另外一个sql server 数据库
  14. 汉仪字体安装后PPT找不到_字体不知道去哪下载?我教您
  15. 聚类分析-K-means clustering 在生物信息学中的应用
  16. java魔法门1_魔法门之英雄无敌5
  17. Python模糊匹配 | 刷英语六级段落匹配只需要3秒?
  18. 浅谈网页设计中的构图
  19. JDK8中String的intern()方法详细解读【内存图解+多种例子+1.1w字长文】
  20. memcached的基础

热门文章

  1. [LeetCode][JavaScript]Invert Binary Tree 反转二叉树
  2. 微信公众平台----带参数二维码生成和扫描事件
  3. 004-Spring boot 快速入门-项目搭建与启动、SpringBootApplication、启动图标
  4. JMS学习(六)--提高非持久订阅者的可靠性 以及 订阅恢复策略
  5. DC的网络连接端口与防火墙设置[为企业部署Windows Server 2008系列十]
  6. Alphabet以3.8亿美元收购董事格林创业公司Bebop
  7. [React Native] 解析JSON文件
  8. returnFunc.js
  9. 正则表达式(中文表达:检查表达式符)
  10. Unity AOP 处理异常的方法