English 中文(简体)
平行游戏:优化问题
原标题:Coin flipping game: Optimization problem

co有直径网,头部代表价值1,尾料代表价值0。 你用2D英寸表(在1至10个行/栏目之间,包容性)来代表这一点。

在每次流动中,你选择电网中的任何单一电池(R、C)(R-th row,C-th栏),并在所有囚室(r, c)中标有0到R(含蓄),c在0到C(含蓄)之间。 coin折是指将电池的价值从零到1或1至零。

将电网中所有电池改为尾料所需的最低迁移次数。 这将永远成为可能。

实例:

1111  
1111  
returns: 1  

01  
01  
returns: 2   

010101011010000101010101  
returns: 20  

000  
000  
001  
011  
returns: 6  

This is what i tried: Since the order of flipping doesn t matter, and making a move on a coin twice is like not making a move at all, we can just find all distinct combinations of flipping coins, and minimizing the size of good combinations(good meaning those that give all tails).

可以通过制定由所有硬币组成的一套指标来做到这一点。 然后可以找到所有可能的分点,并看到其中哪一个分点给出答案(即如果在分块的硬币上转移给我们所有细节)。 最后,尽可能缩小良好组合的规模。

I don t know if I ve been able to express myself too clearly... I ll post a code if you want. Anyway, this method is too time consuming and wasteful, and not possible for no.of coins>20(in my code). How to go about this?

最佳回答

我认为,细微的算法就足够了,每个元件都有一个步骤。

每次移动都标出委员会的一个直截面子。 一些硬币比其他面子更小:上层(0,0)的硬币在每一分块中,右线上的硬币只有一个子,即每个硬币。

因此,选择第一种行动是显而易见的:如果右上下角必须lipp倒,就每一件事。 消除这种可能的行动。

现在,左边和外面的低右面硬币只能靠单方的移动而消失。 因此,如果必须进行这种迁移,就应当这样做。 对邻国的评价次序的确很重要,因为它们是相互之间真正的替代。 然而,ra形应当足够。

页: 1

此处为C++方案:

#include <iostream>
#include <valarray>
#include <cstdlib>
#include <ctime>
using namespace std;

void print_board( valarray<bool> const &board, size_t cols ) {
    for ( size_t i = 0; i < board.size(); ++ i ) { 
        cout << board[i] << " "; 
        if ( i % cols == cols-1 ) cout << endl;
    }   
    cout << endl;
}

int main() {
    srand( time(NULL) );
    int const rows = 5, cols = 5;

    valarray<bool> board( false, rows * cols );
    for ( size_t i = 0; i < board.size(); ++ i ) board[i] = rand() % 2;
    print_board( board, cols );

    int taken_moves = 0;
    for ( size_t i = board.size(); i > 0; ) { 
        if ( ! board[ -- i ] ) continue;

        size_t sizes[] = { i%cols +1, i/cols +1 }, strides[] = { 1, cols };

        gslice cur_move( 0, valarray<size_t>( sizes, 2 ),
                            valarray<size_t>( strides, 2 ) );
        board[ cur_move ] ^= valarray<bool>( true, sizes[0] * sizes[1] ); 

        cout << sizes[1] << ", " << sizes[0] << endl;
        print_board( board, cols );

        ++ taken_moves;
    }   

    cout << taken_moves << endl;
}
问题回答

不是C++。 同意“Potatosawaer”的说法,即最佳溶解是贪 gr,但我想知道,“线性循环系统”是否也发挥作用。 这一数学功能是:

f[ei_] := (
  xdim = Dimensions[ei][[1]];
  ydim = Dimensions[ei][[2]];

  (* Construct XOR matrixes. These are the base elements representing the
     possible moves *)

  For[i = 1, i < xdim + 1, i++,
   For[j = 1, j < ydim + 1, j++,
    b[i, j] =  Table[If[k <= i && l <= j, -1, 0], {k, 1, xdim}, {l, 1, ydim}]
   ]
  ];

  (*Construct Expected result matrix*)
  Table[rv[i, j] = -1, {i, 1, xdim}, {j, 1, ydim}];

  (*Construct Initial State matrix*)
  Table[eiv[i, j] = ei[[i, j]], {i, 1, xdim}, {j, 1, ydim}];

  (*Now Solve*)
  repl = FindInstance[
           Flatten[Table[(Sum[a[i, j] b[i, j], {i, 1, xdim}, {j, 1, ydim}][[i]][[j]])  
                   eiv[i, j] == rv[i, j], {i, 1, xdim}, {j, 1, ydim}]], 
           Flatten[Table[a[i, j], {i, 1, xdim}, {j, 1, ydim}]]][[1]];

  Table[c[i, j] = a[i, j] /. repl, {i, 1, xdim}, {j, 1, ydim}];

  Print["Result ",xdim ydim-Count[Table[c[i, j], {i, 1, xdim}, {j, 1,ydim}], 0, ydim xdim]];)

请举几个例子(-1而不是0)

ei = ({
   {1, 1, 1, 1},
   {1, 1, 1, 1}
   });
f[ei];

ei = ({
   {-1, 1},
   {-1, 1}
  });
f[ei];

ei = {{-1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 
1, -1, 1, -1, 1, -1, 1}};
f[ei];

ei = ({
    {-1, -1, -1},
    {-1, -1, -1},
    {-1, -1, 1},
    {-1, 1, 1}
   });
f[ei];

结果

Result :1
Result :2
Result :20
Result :6

或:

“alt

在90秒里,我的穷人手提电脑上充电20x20个随机问题。

从根本上说,你在正确和底层边界重新吸收N+M-1的硬币,并解决这些硬币,然后将算法重新换成一切。 这基本上是白.瓦工所说的话。 下面是很简单的复读算法。

Solver(Grid[N][M])
    if Grid[N-1][M-1] == Heads
        Flip(Grid,N-1,M-1)

    for each element i from N-2 to 0 inclusive //This is empty if N is 1
        If Grid[i][M-1] == Heads
            Flip(Grid,i,M-1)

    for each element i from M-2 to 0 inclusive //This is empty if M is 1
        If Grid[N-1][i] == Heads
            Flip(Grid,N-1,i)

    if N>1 and M > 1:
        Solver(Grid.ShallowCopy(N-1, M-1))

    return;     

注:执行Grid大概是合理的。 ShallowCopy, 仅凭Solver, 就有关于Grid的宽度和高度的论点。 我只称之为Grid.ShallowCopy,以表明你不应通过电网的复印件,尽管C++以阵列的方式取得了这种成绩。

易碎(x,y)的标准似乎被忽略:确切地说,2x2平方米的面积(x,y)是很奇怪的。

(Zaster)

def flipgame(grid):
  w, h = len(grid[0]), len(grid)
  sol = [[0]*w for y in range(h)]
  for y in range(h-1):
    for x in range(w-1):
      sol[y][x] = grid[y][x] ^ grid[y][x+1] ^ grid[y+1][x] ^ grid[y+1][x+1]
  for y in range(h-1):
    sol[y][w-1] = grid[y][w-1] ^ grid[y+1][w-1]
  for x in range(w-1):
    sol[h-1][x] = grid[h-1][x] ^ grid[h-1][x+1]
  sol[h-1][w-1] = grid[h-1][w-1]
  return sol

返回的2D阵列有1个位置(x,y),如果 rec(x,y)被击落,那么其中的数目就是对你原来的问题的答复。

EDIT: To see why it works: If we do moves (x,y), (x,y-1), (x-1,y), (x-1,y-1), only square (x,y) is inverted. This leads to the code above. The solution must be optimal, as there are 2^(hw) possible configurations of the board and 2^(hw) possible ways to transform the board (assuming every move can be done 0 or 1 times). In other words, there is only one solution, hence the above produces the optimal one.





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签