sicily 1150,1151

// Problem#: 1151
// Submission#: 2982372
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<queue>
#include<stdio.h>
using namespace std;
struct node {
  int a;
  int b;
  string path;
  int sum;

  node() {
    sum = 0;
  }
};

struct hash {
    int n;
    hash *next;
};

node end;
queue<node> q;
int n;
bool sign;
hash *ju[10000];
bool cmp(node temp) {
  if(temp.a == end.a && temp.b == end.b){
    sign = 1;
    return true;
  } else {
    return false;
  }
}

bool judge(node temp) {
    if(ju[temp.a] == NULL) {
        ju[temp.a] = new hash();
        (ju[temp.a])->n = temp.b;
        (ju[temp.a])->next = NULL;
        return true;
    } else {
        hash *p;
        p = ju[temp.a];
        if(p->n == temp.b) {
            return false;
        } else {
            while(p->next != NULL) {
                p = p->next;
                if(p->n == temp.b) {
                    return false;
                }
            }
            p->next= new hash();
            p = p->next;
            p->n = temp.b;
            p->next = NULL;
            return true;
        }
    }
}

void Achange(node temp) {
  if(sign == 0 && temp.sum < n) {
        int a1;
        a1 = temp.a;
        temp.a = temp.b;
        temp.b = a1;
        (temp.path).push_back(‘A‘);
        temp.sum++;
        if(cmp(temp)) {
          end.path = temp.path;
          end.sum = temp.sum;  
        } else {
          if(judge(temp)) {
            q.push(temp);
          }
        }
      }
}

void Bchange(node temp) {
  if(sign == 0 && temp.sum < n) {    
        int a1;
        int b1;
        a1 = temp.a % 10;
        b1 = temp.b % 10;
        temp.a = temp.a/10 + a1*1000;
        temp.b = temp.b/10 + b1*1000;
        (temp.path).push_back(‘B‘);
        temp.sum++;
        if(cmp(temp)) {
          end.path = temp.path;
          end.sum = temp.sum;  
        } else {
            if(judge(temp)) {
            q.push(temp);
          }
        }

   }
}

void Cchange(node temp) {
  if(sign == 0 && temp.sum < n) {
        int g1,s1,b1,q1,g2,s2,b2,q2;
        g1 = temp.a%10;
        s1 = (temp.a%100)/10;
        b1 = (temp.a%1000)/100;
        q1 = temp.a/1000;
        g2 = temp.b%10;
        s2 = (temp.b%100)/10;
        b2 = (temp.b%1000)/100;
        q2 = temp.b/1000;
        temp.a = q1*1000 + b2*100 + b1*10 +g1;
        temp.b = q2*1000 + s2*100 + s1*10 + g2;
        (temp.path).push_back(‘C‘);
        temp.sum++;
        if(cmp(temp)) {
          end.path = temp.path;
          end.sum = temp.sum;  
        } else {
        if(judge(temp)) {
            q.push(temp);
          }
        }
    }
}

int main() {
  while(cin>>n && n != -1) {
    for(int i = 0; i < 10000; i++) {
        ju[i] = NULL;
    }
    int a = 0;
    int b = 0;
    char c;
    c=getchar();
    c=getchar();
    while(c != ‘\n‘) {
      if(c != ‘ ‘) {
        a = a*10 + (int)(c - ‘0‘);
      }
      c = getchar();
    }
    c = getchar();
    while(c != ‘\n‘) {
      if(c != ‘ ‘) {
        b = b*10 + (int)(c - ‘0‘);
      }
      c = getchar();
    }
    node start;
    start.sum = 0;
    start.a = 1234;
    start.b = 8765;
    end.a = a;
    end.b = b;
    end.sum = 0;
    end.path = "";
    q.push(start);
    sign = 0;
    ju[1234] = new hash();
    (ju[1234])->n = 8765;
    (ju[1234])->next = NULL;
    if(cmp(start)) {
    }
    while(!q.empty() && sign == 0) {
      Achange(q.front());
      Bchange(q.front());
      Cchange(q.front());
      q.pop();
    }

    if(sign == 1) {
      cout << end.sum << "  " << end.path << endl;
    } else {
      cout << "-1" <<endl;
    }
  }
}
时间: 2024-08-25 06:51:56

sicily 1150,1151的相关文章

sicily 1150 简单魔方 队列解题

1150. 简单魔板 Constraints Time Limit: 1 secs, Memory Limit: 32 MB , Special Judge Description 魔板由8个大小相同方块组成,分别用涂上不同颜色,用1到8的数字表示. 其初始状态是 1 2 3 4 8 7 6 5 对魔板可进行三种基本操作: A操作(上下行互换): 8 7 6 5 1 2 3 4 B操作(每次以行循环右移一个): 4 1 2 3 5 8 7 6 C操作(中间四小块顺时针转一格): 1 7 2 4

Sicily 1150. 简单魔板

BFS.军训晚上下雨无聊写的水题. 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <queue> 6 #include <vector> 7 #define rep(i,l,r) for(int i = l; i <= r; i++) 8 #define clr(x,y) memse

广度/宽度优先搜索(BFS)详解

广度/宽度优先搜索(BFS) [算法入门] 1.前言 广度优先搜索(也称宽度优先搜索,缩写BFS,以下采用广度来描述)是连通图的一种遍历策略.因为它的思想是从一个顶点V0开始,辐射状地优先遍历其周围较广的区域,故得名. 一般可以用它做什么呢?一个 广度/宽度优先搜索(BFS) 算法导论里边会给出不少严格的证明,我想尽量写得通俗一点,因此采用一些直观的讲法来伪装成证明,关键的point能够帮你get到就好. 2.图的概念 刚刚说的广度优先搜索是连通图的一种遍历策略,那就有必要将图先简单解释一下.

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

【2016-11-2】【坚持学习】【Day17】【微软 推出的SQLHelper】

从网络上找到 微软原版本的SQLHelper,很多行代码.认真看了,学习了. 代码: 1 using System; 2 using System.Data; 3 using System.Xml; 4 using System.Data.SqlClient; 5 using System.Collections; 6 7 namespace Helper 8 { 9 /// <summary> 10 /// The SqlHelper class is intended to encapsu

使用mysql5.7.16头文件库文件编译安装atlas

最近mysql数据库升级到5.7.16,而中间件atlas之前是基于mysql5.6编译的,打算重新编译下atlas并使用mysql5.7.16的库文件头文件.这里主要介绍下编译过程遇到问题及解决方法. 由于需要glib版本要大于2.32以上,centos6.6系统自带的glib版本为2.28,因此需要重新编译安装2.32以上的glib,这里安装2.34版本的glib需要依赖pcre大于8.31 一.编译安装pcre8.35 # ./configure --prefix=/usr/local/g

微软原版SQLHelper类

C# Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

HDU 题目分类

基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029.1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093.1094.1095.1096.1097.1098.1106.1108.1157.1163.1164.1170.1194.1196.1197.1201.1202.1205.1219.1234.1235.1236.1248.1

模板化的七种排序算法,适用于T* vector&lt;T&gt;以及list&lt;T&gt;

最近在写一些数据结构以及算法相关的代码,比如常用排序算法以及具有启发能力的智能算法.为了能够让写下的代码下次还能够被复用,直接将代码编写成类模板成员函数的方式,之所以没有将这种方式改成更方便的函数模板纯属于偷懒,更方便于测试代码的有效性,等代码写完也懒得去改了.下面开始介绍这段代码,有什么不对的地方欢迎前来指正. 一共写了七种排序,插入排序InsertSort.堆排序HeapSort.快速排序QuickSort.合并排序MergeSort,计数排序CountingSort,基数排序RadixSo