L . Moving Pegs

Description

Venture MFG Company, Inc. has made a game board. This game board has 15 holes and these holes are filled with pegs except one hole. A peg can jump over one or more consecutive peg s to the nearest empty hole along the straight line. As a peg jump over the pegs you remove them from the board. In the following figure, the peg at the hole number 12 or the peg at the hole number 14 can jump to the empty hole number 5. If the peg at the hole number 12 is moved then the peg at the hole number 8 is removed. Instead, if the peg at the hole number 14 is moved then the peg at the hole number 9 is removed.

Write a program which find a shortest sequence of moving pegs to leave the last peg in the hole that was initially empty. If such a sequence does not exist the program should write a message ‘IMPOSSIBLE’.

Input

The input consists of TT test cases. The number of test cases (TT) is given in the first line of the input file. Each test case is a single integer which means an empty hole number.

Output

For each test case, the first line of the output file contains an integer which is the number of jumps in a shortest sequence of moving pegs. In the second line of the output file, print a sequence of peg movements. A peg movement consists of a pair o f integers separated by a space. The first integer of the pair denotes the hole number of the peg that is moving, and the second integer denotes a destination (empty) hole number.

Samples

Input 复制

1
5

Output

10
12 5 3 8 15 12 6 13 7 9 1 7 10 8 7 9 11 14 14 5

题目大意:
Venture MFG Company,Inc.制造了一个游戏板。这个游戏板有15个洞,除了一个洞以外,这些洞都是用钉子填满的。一个木桩可以越过一个或多个连续的木桩沿直线跳到最近的空孔。当钉子跳过钉子时,你就把它们从板上取下来。在下图中,12号孔处的销钉或14号孔处的销钉可以跳到5号空孔。如果移动12号孔处的销钉,则移除8号孔处的销钉。相反,如果移动孔14处的销钉,则移除孔9处的销钉。编写一个程序,找出一个最短的移动桩序列,使孔中的最后一个桩最初是空的。如果这样的序列不存在,程序应该写一个消息“不可能”。
输入
输入由T个测试用例组成。测试用例的数量(T)在输入文件的第一行中给出。每个测试用例都是一个整数,表示一个空孔数。
输出
对于每个测试用例,输出文件的第一行包含一个整数,该整数是移动桩的最短序列中的跳转次数。在输出文件的第二行中,打印一系列销钉移动。销钉运动由一对被空格分隔的整数组成。该对的第一个整数表示正在移动的销钉的孔编号,第二个整数表示目的地(空)孔编号。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
#define bit(n) (1<<(n))
typedef long long LL;
typedef unsigned long long ULL;
void work();
int main()
{
#ifdef ACM
    freopen("in.txt", "r", stdin);
#endif // ACM
    work();
}
 
/***************************************************/
int tot = 0;
int line[111];
int Sta[111];
int End[111];
int lineEnd[111111];
 
void init()
{
    int no = 0;
    int row[15];
    int col[15];
    int maze[10][10];
    fff(i, 1, 5) fff(j, 1, i)
    {
        maze[i][j] = no;
        row[no] = i;
        col[no] = j;
        no++;
    }
 
    ff(i, 15) ff(j, 15)
    {
        if(row[i] == row[j] && abs(col[i]-col[j]) >= 2)
        {
            int st = 0;
 
            if(col[j] > col[i])
                fff(k, col[i], col[j])
                    st ^= bit(maze[row[i]][k]);
            else
                dff(k, col[i], col[j])
                    st ^= bit(maze[row[i]][k]);
 
            line[tot] = st;
            Sta[tot] = maze[row[i]][col[i]];
            End[tot] = maze[row[j]][col[j]];
            tot++;
        }
 
        if(col[i] == col[j] && abs(row[i]-row[j]) >= 2)
        {
            int st = 0;
 
            if(row[j] > row[i])
                fff(k, row[i], row[j])
                    st ^= bit(maze[k][col[i]]);
            else
                dff(k, row[i], row[j])
                    st ^= bit(maze[k][col[i]]);
 
            line[tot] = st;
            Sta[tot] = maze[row[i]][col[i]];
            End[tot] = maze[row[j]][col[j]];
            tot++;
        }
 
        if(abs(row[i]-row[j]) >= 2 && row[i]-row[j] == col[i]-col[j])
        {
            int st = 0;
 
            int dis = row[j]-row[i];
            if(dis > 0)
                fff(k, 0, dis)
                    st ^= bit(maze[row[i]+k][col[i]+k]);
            else
                dff(k, 0, dis)
                    st ^= bit(maze[row[i]+k][col[i]+k]);
 
            line[tot] = st;
            Sta[tot] = maze[row[i]][col[i]];
            End[tot] = maze[row[j]][col[j]];
            tot++;
        }
    }
}
 
struct Node
{
    int st;
    vector<int> path;
} x, y;
 
int id;
int vis[bit(15)];
 
void work()
{
    init();
 
    int T;
    scanf("%d", &T);
    fff(cas, 1, T)
    {
        int n;
        scanf("%d", &n);
        n--;
 
        ++id;
        x.st = (bit(15)-1)^bit(n);
        x.path.clear();
        vis[x.st] = id;
 
        queue<Node> que;
        que.push(x);
 
        while(que.size())
        {
            x = que.front();
            que.pop();
 
            if(x.st == bit(n)) break;
 
            ff(i, tot)
            {
                int state = line[i];
 
                if((x.st & state) == (state ^ bit(End[i])))
                {
                    y.st = (x.st ^ state);
 
                    if(vis[y.st] == id) continue;
                    vis[y.st] = id;
 
                    y.path = x.path;
                    y.path.push_back(Sta[i]);
                    y.path.push_back(End[i]);
                    que.push(y);
                }
            }
        }
 
        if(x.st == bit(n))
        {
            int s = x.path.size();
            printf("%d\n", s/2);
            printf("%d", x.path[0]+1);
            fff(i, 1, s-1) printf(" %d", x.path[i]+1);
            puts("");
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值