1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-03-24

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が入り、右側は左側のちょうど裏返しになる。
よーしこれは斬新な解き方だぜー、とか思って他の解答を見てみたら、当然みんな普通に使ってた。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?