結果

問題 No.506 限られたジャパリまん
ユーザー 14番14番
提出日時 2017-04-22 02:30:28
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 5,559 bytes
コンパイル時間 4,661 ms
コンパイル使用メモリ 109,860 KB
実行使用メモリ 24,060 KB
最終ジャッジ日時 2023-09-10 12:53:21
合計ジャッジ時間 7,006 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
23,852 KB
testcase_01 AC 65 ms
23,808 KB
testcase_02 WA -
testcase_03 AC 69 ms
21,864 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 68 ms
21,784 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 RE -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 64 ms
21,768 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 RE -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;

        int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();

        long[,] Map = new long[inpt[0]+1, inpt[1]+1];

        this.FriendsList = new Friends[inpt[2]];

        int japariman = inpt[3];

        for (int i = 0; i < this.FriendsList.Length; i++)
        {
            this.FriendsList[i] = new Friends(i, Reader.ReadLine());
        }
        if (!this.FriendsList.Any(a => a.X == 0 && a.Y == 0))
        {
            Map[0, 0] = 1;
        }
        for (int i = 1; i <= inpt[0]; i++)
        {
            if(!FriendsList.Any(a=>a.X == i && a.Y == 0)) {
                Map[i, 0] = Map[i - 1, 0];
            }
        }
        for (int i = 1; i <= inpt[1]; i++)
        {
            if(!FriendsList.Any(a=>a.X == 0 && a.Y == i)) {
                Map[0, i] = Map[0, i - 1];
            }
        }
        for(int i=1; i<=inpt[0]; i++) {
            for(int j=1; j<=inpt[1]; j++) {
                if(!FriendsList.Any(a=>a.X == i && a.Y == j)) {
                    Map[i, j] = Map[i-1, j] + Map[i,j-1];
                }
            }
        }
        Ans ans = GetAns(Map, 0, japariman);
        Console.WriteLine(ans.WayCount);
        for (int i = 0; i < FriendsList.Length; i++)
        {
            int key = 1 << i;
            if ((ans.Flag & key) != 0)
            {
                Console.WriteLine(FriendsList[i].Name);
            }
        }
    }

    private Ans GetAns(long[,] map, int flag, int remain)
    {
        if (remain <= 0)
        {
            return new Ans(map[map.GetLength(0)-1, map.GetLength(1)-1], flag);
        }

        int w = map.GetLength(0);
        int h = map.GetLength(1);

        List<Friends> target = new List<Friends>();
        if (map[0, 0] == 0)
        {
            target.Add(FriendsList.Where(a => a.X == 0 && a.Y == 0).First());
        }
        else
        {
            long max = FriendsList.Where(a => map[a.X, a.Y] == 0).Select(a =>a.GetKaitu(map)).Max();
            target = FriendsList.Where(a => a.GetKaitu(map) == max).ToList();
        }

        Ans ans = new Ans(0, 0);

        foreach (Friends f in target)
        {
            long[,] subMap = GetMap(map, f.X, f.Y, f.GetKaitu(map));
            Ans ret = this.GetAns(subMap, flag + (1 << f.ID), remain - 1);
            if (ret.WayCount > ans.WayCount)
            {
                ans = ret;
            }
        }
        return ans;

    }

    private long[,] GetMap(long[,] basMap, int x, int y, long val)
    {
        int w = basMap.GetLength(0);
        int h = basMap.GetLength(1);
        long[,] newMap = new long[w, h];
        for(int i=0; i<w; i++) {
            for(int j=0; j<h; j++) {
                newMap[i,j] = basMap[i,j];
            }
        }
        if (x == 0 && y == 0)
        {
            for (int i = 0; i < w; i++)
            {
                newMap[i, 0] = val;
            }
            return GetMap(newMap, 0, 1, val);
        }
        else if (x == 0)
        {
            for (int i = y; i < h; i++)
            {
                newMap[0, i] = val;
            }
            return GetMap(newMap, 1, y, newMap[0, y] + newMap[1, y - 1]);
        }
        else if (y == 0)
        {
            for (int i = x; i < w; i++)
            {
                newMap[i, 0] = val;
            }
            return GetMap(newMap, x, 1, newMap[x - 1, 1] + newMap[x, 0]);
        }
        else
        {
            for (int i = x; i < w; i++)
            {
                for (int j = y; j < h; j++)
                {
                    newMap[i, j] = newMap[i - 1, j] + newMap[i, j - 1];
                }
            }
            return newMap;
        }
    }

    private struct Ans
    {
        public long WayCount;
        public int Flag;

        public Ans(long w, int f) {
            this.WayCount = w;
            this.Flag = f;
        }

    }

    private Friends[] FriendsList;

    private struct Friends
    {
        public int X;
        public int Y;
        public string Name;
        public int ID;

        public long GetKaitu(long[,] mp)
        {
            if (this.X == 0 && this.Y == 0)
            {
                return 1;
            }
            else if (this.X == 0)
            {
                return mp[0, this.Y - 1];
            }
            else if (this.Y == 0)
            {
                return mp[this.X - 1, 0];
            }
            return mp[this.X - 1, this.Y - 1];
        }

        public Friends(int id, string init)
        {
            this.ID = id;
            string[] tmp = init.Split(' ');
            this.X = int.Parse(tmp[0]);
            this.Y = int.Parse(tmp[1]);
            this.Name = tmp[2];
        }
    }




    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"


1 2 1 1
0 1 serval


";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0