忍者ブログ
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。



2017/07/21 17:51 |
PHP5.6.0 「第18回オフラインリアルタイムどう書くの参考問題」をPHPで解く
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が入り、右側は左側のちょうど裏返しになる。
よーしこれは斬新な解き方だぜー、とか思って他の解答を見てみたら、当然みんな普通に使ってた。
PR


2014/03/24 23:11 | Comments(0) | PHP

コメント

コメントを投稿する






Vodafone絵文字 i-mode絵文字 Ezweb絵文字 (絵文字)



<<買ったものリスト 2014/03/30 | HOME | 買ったものリスト 2014/03/23>>
忍者ブログ[PR]