Help us understand the problem. What is going on with this article?

「第18回オフラインリアルタイムどう書くの参考問題」をPHPで解く

More than 5 years have passed since last update.

http://nabetani.sakura.ne.jp/hena/ord18mafovafo/
http://qiita.com/Nabetani/items/373105e7fafd12f5e9fd

山折り谷折りした後、折り目がどうなっているかを求める問題。
この手の図形問題って苦手なんですよね。
裏返しになったところを折ったらどうなるってあたりで頭がこんがらがる。

とか思ってたけど、実際に作り始めてみたらわりと一瞬で終わった。
かかった時間は20分程度。

<?php

    class MAFOVAFO{
        /**
        * 山折り谷折り
        * @param String 「JZ」みたいな文字列
        * @return String 「mVVmV」みたいな文字列
        **/
        public function get($input){
            $str = '';
            for($i=strlen($input)-1; $i>=0; $i--){
                switch($input[$i]){
                case 'J':
                    $str = $str . 'V' . $this->reverse($str);
                    break;
                case 'L':
                    $str = $this->reverse($str) . 'V' . $str;
                    break;
                case 'Z':
                    $str = $str . 'm' . $this->reverse($str) . 'V' . $str;
                    break;
                case 'S':
                    $str = $str . 'V' . $this->reverse($str) . 'm' . $str;
                    break;
                case 'U':
                    $str = $this->reverse($str) . 'V' . $str . 'V' . $this->reverse($str);
                    break;
                }
            }
            return $str;
        }

        // 反転する
        private function reverse($input){
            // 文字列を逆に
            $input = strrev($input);
            // mVを逆に 一括でm←→Vができない
            $input = str_replace(['m', 'V'],['t', 'm'], $input);
            $input = str_replace('t','V', $input);
            return $input;
        }

    }

    // 以下はテスト
    $test = [
        ['JZ', 'mVVmV'],
        ['J', 'V'],
        ['L', 'V'],
        ['Z', 'mV'],
        ['U', 'VV'],
        ['S', 'Vm'],
        ['JL', 'VVm'],
        ['JS', 'VmVVm'],
        ['JU', 'VVVmm'],
        ['LU', 'mmVVV'],
        ['SL', 'VVmmV'],
        ['SS', 'VmVVmmVm'],
        ['SU', 'VVVmmmVV'],
        ['SZ', 'mVVmVmmV'],
        ['UL', 'mVVVm'],
        ['UU', 'mmVVVVmm'],
        ['UZ', 'mVVmVVmV'],
        ['ZJ', 'VmmVV'],
        ['ZS', 'VmmVmVVm'],
        ['ZZ', 'mVmmVVmV'],
        ['JJJ', 'VVmVVmm'],
        ['JJZ', 'mVVmVVmVmmV'],
        ['JSJ', 'VVmmVVmVVmm'],
        ['JSS', 'VmVVmmVmVVmVVmmVm'],
        ['JUS', 'VmVVmVVmVVmmVmmVm'],
        ['JUU', 'mmVVVVmmVVVmmmmVV'],
        ['JUZ', 'mVVmVVmVVmVmmVmmV'],
        ['LJJ', 'VmmVVVm'],
        ['LLS', 'VmmVmVVmVVm'],
        ['LLU', 'mmmVVVmmVVV'],
        ['LLZ', 'mVmmVVmVVmV'],
        ['LSU', 'mmVVVmmmVVVVmmmVV'],
        ['LSZ', 'mVVmVmmVVmVVmVmmV'],
        ['LZL', 'mmVVmVVmmVV'],
        ['LZS', 'VmmVmVVmVVmmVmVVm'],
        ['LZU', 'mmmVVVmmVVVmmmVVV'],
        ['SJL', 'VVmVVmmmVVm'],
        ['SLU', 'mmVVVVmmmVVmmmVVV'],
        ['SLZ', 'mVVmVVmVmmVmmVVmV'],
        ['SSU', 'VVVmmmVVVmmVVVmmmmVVVmmmVV'],
        ['SUJ', 'mVVVmVVmmmVmmVVVm'],
        ['SUS', 'VmVVmVVmVVmmVmmVmmVmVVmVVm'],
        ['SZZ', 'mVmmVVmVVmVmmVVmVmmVmmVVmV'],
        ['UJJ', 'VmmVVVmVVmm'],
        ['ULU', 'mmmVVVmmVVVVmmmVV'],
        ['ULZ', 'mVmmVVmVVmVVmVmmV'],
        ['UUU', 'VVmmmmVVVmmVVVVmmVVVmmmmVV'],
        ['ZJU', 'VVVmmmVVmmmVVVVmm'],
        ['ZLS', 'VmVVmmVmmVmVVmVVm'],
        ['ZSJ', 'VVmmVmmVVmmVVVmmV'],
        ['ZUJ', 'mVVVmmVmmmVVmVVVm'],
        ['JJLJ', 'mVVVmmVVmVVmmmV'],
        ['JLJJ', 'VmmVVVmVVmmmVVm'],
        ['JLJL', 'VmmVVVmVVmmmVVm'],
        ['LJJL', 'VVmmVmmVVVmVVmm'],
        ['LLJJ', 'VmmmVVmVVmmVVVm'],
        ['SZUS', 'VmVVmVVmmVmmVmmVmVVmVVmVVmVVmmVmmVmmVmVVmVVmVVmmVmmVmmVmVVmVVmmVmmVmmVmVVmVVmVVm'],
        ['ULLS', 'VmmVmmVmVVmVVmmVmVVmVVmVVmmVmmVmVVm'],
        ['JJJJZJ', 'VmmVVVmmVVmVVmmVVmmmVVmVVmmVVVmmVVmmVmmVVmmmVVmVVmmVVVmmVVmVVmmVVmmmVVmmVmmVVVmmVVmmVmmVVmmmVVm'],
        ['JULLLJ', 'mmVmmVVmmmVVmVVVmmVmmVVVmmVVmVVVmmVmmVVmmmVVmVVVmmVmmVVVmmVVmVVmmmVmmVVmmmVVmVVmmmVmmVVVmmVVmVV'],
        ['LJJJUL', 'mVVVmVVmmmVVmVVVmmVmmmVmmVVVmVVmmmVmmVVVmmVmmmVVmVVVmVVmmmVVmVVVmmVmmmVVmVVVmVVmmmVmmVVVmmVmmmV'],
        ['LJSJJL', 'VVmVVmmVVVmmVmmmVVmVVmmmVVmmVmmVVVmVVmmmVVmmVmmVVVmVVmmVVVmmVmmmVVmVVmmVVVmmVmmVVVmVVmmmVVmmVmm'],
        ['LZLLLJ', 'mmVmmVVmmmVVmVVmmmVmmVVVmmVVmVVVmmVmmVVmmmVVmVVVmmVmmVVVmmVVmVVmmmVmmVVmmmVVmVVVmmVmmVVVmmVVmVV'],
        ['SJJJJL', 'VVmVVmmVVVmmVmmVVVmVVmmmVVmmVmmVVVmVVmmVVVmmVmmmVVmVVmmmVVmmVmmmVVmVVmmVVVmmVmmVVVmVVmmmVVmmVmm'],
        ['ZLJLJL', 'VmmVVVmmVmmmVVmVVmmVVVmVVmmmVVmmVmmVVVmmVmmmVVmmVmmVVVmVVmmmVVmVVmmVVVmmVmmmVVmVVmmVVVmVVmmmVVm'],
    ];

    $mafovafo = new MAFOVAFO();
    foreach($test as $key=>$data){
        $answer = $mafovafo->get($data[0]);
        if($answer !== $data[1]){
            print('えらー');
        }
    }

折るのではなく、後ろから開いていく方向で考えた。
たとえばJを開くと、左側はそのまま、中央に谷折りのVが入り、右側は左側のちょうど裏返しになる。
よーしこれは斬新な解き方だぜー、とか思って他の解答を見てみたら、当然みんな普通に使ってた。

rana_kualu
不労所得で生きたい。
https://twitter.com/rana_kualu
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした