• POJ2286 The Rotation Game


    嘟嘟嘟


    (IDA*)
    没错就是暴搜,然后加上迭代步数,再加上(A*)
    至于每一步的操作,也是暴力(我写的可能有点丑)。
    还有一个剪枝,就是别走上一步的逆操作。
    然后我因为输出(No moves needed)后没输出中间的数(Debug)了半天。
    (ZZ)

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define rg register
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    //const int maxn = ;
    inline ll read()
    {
      ll ans = 0;
      char ch = getchar(), last = ' ';
      while(!isdigit(ch)) last = ch, ch = getchar();
      while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
      if(last == '-') ans = -ans;
      return ans;
    }
    inline void write(ll x)
    {
      if(x < 0) x = -x, putchar('-');
      if(x >= 10) write(x / 10);
      putchar(x % 10 + '0');
    }
    
    int tp[30], a[10][10], dep = 0, Ans;
    char ans[20];
    
    void init()
    {
      a[1][3] = tp[1]; a[1][5] = tp[2];
      a[2][3] = tp[3]; a[2][5] = tp[4];
      for(int i = 1; i <= 7; ++i) a[3][i] = tp[i + 4];
      a[4][3] = tp[12]; a[4][5] = tp[13];
      for(int i = 1; i <= 7; ++i) a[5][i] = tp[i + 13];
      a[6][3] = tp[21]; a[6][5] = tp[22];
      a[7][3] = tp[23]; a[7][5] = tp[24];
    }
    
    int ni(int x)
    {
      if(x == 0) return 5;
      if(x == 1) return 4;
      if(x == 2) return 7;
      if(x == 3) return 6;
      if(x == 4) return 1;
      if(x == 5) return 0;
      if(x == 6) return 3;
      if(x == 7) return 2;
      return -1;
    }
    const int lin[] = {3, 5, 3, 5, 5, 3, 5, 3};
    const int b[] = {8, 1, 2, 3, 4, 5, 6, 7};
    const int c[] = {0, 7, 6, 5, 4, 3, 2, 1};
    void solve(int x)
    {
      if(x == 0 || x == 1 || x == 4 || x == 5)
        {
          if(x == 0 || x == 1)
    	for(int i = 0; i < 8; ++i) a[b[i]][lin[x]] = a[i + 1][lin[x]];
          else
    	for(int i = 0; i < 8; ++i) a[c[i]][lin[x]] = a[7 - i][lin[x]];
        }
      else
        {
          if(x == 6 || x == 7)
    	for(int i = 0; i < 8; ++i) a[lin[x]][b[i]] = a[lin[x]][i + 1];
          else
    	for(int i = 0; i < 8; ++i) a[lin[x]][c[i]] = a[lin[x]][7 - i];
        }
    }
    int tot[4];
    int h()
    {
      tot[1] = tot[2] = tot[3] = 0;
      for(int i = 3; i <= 5; ++i)
        for(int j = 3; j <= 5; ++j) tot[a[i][j]]++;
      int pos = 1;
      for(int i = 2; i <= 3; ++i) if(tot[i] > tot[pos]) pos = i;
      Ans = pos;
      return 8 - tot[pos];
    }
    
    int cnt = 0;
    bool dfs(int stp, int las)
    {
      if(stp > dep) return !h() ? 1 : 0;
      if(stp + h() > dep + 1) return 0;
      for(int i = 0; i < 8; ++i)
        {
          if(i == ni(las)) continue;
          solve(i);
          ans[stp] = 'A' + i;
          if(dfs(stp + 1, i)) return 1;
          solve(ni(i));
        }
      return 0;
    }
    
    int main()
    {  while(scanf("%d", &tp[1]) != EOF && tp[1])
        {
          for(int i = 2; i <= 24; ++i) tp[i] = read();
          init();
          for(dep = 0; dep <= 15; ++dep)
    	if(dfs(1, -1)) break;
          if(!dep) printf("No moves needed");
          else for(int i = 1; i <= dep; ++i) putchar(ans[i]);
          enter; write(Ans), enter;
        }
      return 0;
    }
    
  • 相关阅读:
    LeetCode OJ:Rotate List(旋转链表)
    LeetCode OJ:Gas Station(加油站问题)
    LeetCode OJ:Reverse Nodes in k-Group(K个K个的分割节点)
    Qt实现简单的单例模式
    LeetCode OJ:Merge k Sorted Lists(归并k个链表)
    LeetCode OJ:LRU Cache(最近使用缓存)
    LeetCode OJ:Maximal Rectangle(最大矩形)
    LeetCode OJ:Number of Islands(孤岛计数)
    LeetCode OJ:Valid Sudoku(有效数独问题)
    从hive导入到oracle(Hcatalog)
  • 原文地址:https://www.cnblogs.com/mrclr/p/10020921.html
Copyright © 2020-2023  润新知