5
5

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.

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

Posted at

http://qiita.com/Nabetani/items/6a9f5593d0f3d7e0568c
http://nabetani.sakura.ne.jp/hena/ord16boseg/

方眼紙を白と黒で塗り分けたとき、境界の線分の長さを求める。
極めて順当に実装。

<?php
	
	class BOSEG{
		/**
		* 境界線分
		* @param String 「060276724276」みたいな文字列
		* @return String 「6,2,1,1,0,1」みたいな文字列
		*/
		public function get($input){
			$boseg = array_fill(1, 6, 0);
			// 入力値をパース
			$input = array_map(function($input){
				return sprintf('%03d%03d', base_convert($input[0], 8, 2),base_convert($input[1], 8, 2));
			}, str_split($input, 2));
			
			// 横向きにチェック
			for($i=0;$i<5;$i++){
				$length=0;
				for($j=0;$j<=5;$j++){
					if($input[$i][$j] === $input[$i+1][$j]){
						// 上下が同じなら
						if($length > 0){
							// 違いがあったらそこで途切れる
							$boseg[$length]++;
							$length=0;
						}
					}elseif($j===5){
						// 最後だけ少し特殊
						if($input[$i][$j] !== $input[$i+1][$j]){
							$boseg[$length+1]++;
						}
					}else{
						// 違ってるのが続く
						$length++;
					}
				}
			}
			// 縦向きにチェック
			for($i=0;$i<5;$i++){
				$length=0;
				for($j=0;$j<=5;$j++){
					if($input[$j][$i] === $input[$j][$i+1]){
						if($length > 0){
							$boseg[$length]++;
							$length=0;
						}
					}elseif($j===5){
						if($input[$j][$i] !== $input[$j][$i+1]){
							$boseg[$length+1]++;
						}
					}else{
						$length++;
					}
				}
			}
			// 終了
			return implode(',',$boseg);
		}
		
	}
	
	// 以下はテスト
	$test = [
		['060276724276','6,2,1,1,0,1'],
		['770175454177','2,3,0,3,1,0'],
		['743733377170','9,3,1,0,0,0'],
		['724212121273','5,2,1,1,1,1'],
		['100000000000','3,0,0,0,0,0'],
		['000002000000','4,0,0,0,0,0'],
		['003622223600','0,4,0,4,0,0'],
		['520073737070','8,3,1,1,0,0'],
		['770077007700','0,0,0,0,0,5'],
		['555555555514','2,0,0,0,2,2'],
		['764252427600','4,0,4,0,2,0'],
		['774555554177','3,3,1,3,0,0'],
		['674574754557','11,5,0,1,0,0'],
		['000000000000','0,0,0,0,0,0'],
		['777777777777','0,0,0,0,0,0'],
		['774377777577','6,0,2,0,0,0'],
		['070777777777','0,1,1,0,0,0'],
		['373737373737','0,0,0,0,0,1'],
		['603260327725','30,0,0,0,0,0'],
		['466331144663','30,0,0,0,0,0'],
		['000000000242','3,2,0,0,0,0'],
		['567656043772','18,2,1,0,0,0'],
		['200763012420','15,4,1,0,0,0'],
		['400101140052','14,3,0,0,0,0'],
		['764767476476','13,2,0,1,0,0'],
		['001110140110','12,2,1,0,0,0'],
		['765405076527','16,3,0,1,0,0'],
		['377323370373','8,4,2,0,0,0'],
		['250541131216','11,5,2,0,0,0'],
		['744165741476','12,3,2,0,0,0'],
		['042101000300','10,3,0,0,0,0'],
		['002004554101','11,3,1,0,0,0'],
		['371707762706','15,1,1,0,0,0'],
		['130371310175','7,3,1,2,0,0'],
		['212537003613','13,2,1,1,1,0'],
		['157700063411','15,3,0,0,0,1'],
		['011500036007','6,7,1,0,0,0'],
		['743113313517','17,2,1,0,0,0'],
		['174105270405','13,3,1,1,0,0'],
		['427272200311','13,3,2,0,0,0'],
		['725370332237','12,5,1,1,0,0'],
		['005640420046','12,1,3,0,0,0'],
		['700350001101','14,3,1,0,0,0'],
		['577627744076','16,1,1,1,0,0'],
		['620332232007','10,4,2,1,0,0'],
		['260406401000','15,1,1,0,0,0'],
		['737272723276','5,0,0,0,3,0'],
		['000400040444','7,0,2,0,0,0'],
		['370222002177','13,2,2,0,0,0'],
		['372236024656','9,3,2,0,1,0'],
		['276131137003','11,6,2,0,0,0'],
		['742134007240','13,4,2,0,0,0'],
		['777721775571','13,1,2,0,0,0'],
		['700301232233','11,2,3,0,0,0'],
	];

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

ループ最後のところだけ少し処理を追加してますが、それ以外は普通に順番に見ていってるだけです。
横向きと縦向きで思いっきり同じことをしているので、まとめればもっと記述を減らせますがもういいや。

5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?