game 矩阵取数游戏

Posted on 2008-02-24 13:43 Humtong 阅读(854) 评论(1) 编辑 收藏

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*第三题:game 矩阵取数游戏
    一个n行m列的矩阵,每次你需要按要求取出n个数,m次正好将所有数取完。每取出一个数你都会有一个得分,请求出最终的得分最大是多少。
    每一次取数的要求:每一行中恰好取一个数,且只能取剩下的数中最左边或最右边位置上的数
    每取一个数的得分:所取数的数值乘以2^i,i表示这是第i轮取数。
    矩阵中的数为不超过100的自然数,1<=n,m<=80

样例输入:
2 3
1 2 3
3 4 2
样例输出:
82
样例说明:
1*2+2*2 + 2*4+3*4 + 3*8+4*8 = 82*/

/*题目比较简单 */
namespace ConsoleApplication1
{
    class Program
    {
        //n,m
        long Game(int pN, int pM)
        {
            int[,] chess = new int[pN, pM];
            int[,] rChess = new int[pN, pM];
            long result = 0;
            int min = 0;
            for (int i = 0; i < chess.Length; i++)
            {
                chess[i/pM,i%pM] = int.Parse(Console.ReadLine());
            }
            //get times m
            for (int i = 0; i < pM; i++)
            {
                //get nums n
                for (int j = 0; j < pN; j++)
                {
                    //
                    min = 0;

                    for(int k=1;k<pM;k++)
                    {
                        if (chess[j,k] < chess[j,min])
                            min = k;
                    }
                    result+=chess[j,min] * Squre(2, i + 1);
                    chess[j,min] = Int32.MaxValue;
                }
            }
            return result;
        }

        int Squre(int pNum, int pT)
        {
            int r = 1;
            while (pT-- > 0)
            {
                r *= pNum;
            }
            return r;
        }

        static void Main(string[] args)
        {
            Program p=new Program();
            Console.Write(p.Game(2,3));
        }
    }
}

posts - 14, comments - 11, trackbacks - 0, articles - 0

Copyright © Humtong