結果

問題 No.643 Two Operations No.2
ユーザー moti
提出日時 2018-05-23 02:02:29
言語 Perl
(5.40.0)
結果
WA  
実行時間 -
コード長 467 bytes
コンパイル時間 281 ms
コンパイル使用メモリ 7,296 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2024-06-28 16:02:56
合計ジャッジ時間 1,092 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 11 WA * 2
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.pl syntax OK

ソースコード

diff #

#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;

my ($x, $y) = split / /, <>; chomp $y;

if ($x == $y) {
  print 0;
  exit;
}

my @q;
push @q, [$y, $x, 1];
push @q, [$x + $y, $x - $y, 1];
my %vis;
while (scalar @q) {
  my $tup = shift @q;
  my ($x, $y, $c) = @{$tup};
  if ($x == $y) {
    print $c;
    exit;
  }
  if (!exists $vis{$tup}) {
    $vis{$tup} = 1;
    push @q, [$y, $x, $c + 1];
    push @q, [$x + $y, $x - $y, $c + 1];
  }
}

print -1
0