迷路で壁をノックダウンする


10

ルール:

このゲームでは、壁とオープンスペースで構成されるサイズN x Mの長方形グリッドの上部から開始します。入力はM文字のN行で、a .はオープンスペースをx指定し、aは壁を指定します。プログラムは、Kの壁を横切る左上隅から右下隅(対角線なし)へのパスが存在するように、最小数Kを出力する必要があります。

たとえば、次の入力があるとします。

..x..
..x..
xxxxx
..x..
..x..

あなたのプログラムは出力する必要があります2

その他の例:

出力4

xxxxx
x.x.x
x.x.x
x..xx

出力0

.xxxxxxxx
.x...x...
.x.x.x.x.
.x.x...x.
...xxxxx.

出力6

xx
xx
xx
xx
xx

追加のヒント:

作業が楽になる場合は、コマンドラインパラメータとしてNとMを指定できます。

プログラムにパスを何らかの形で印刷させることができる場合は、追加のクレジット。


4
:yawn:ダイクストラ、V [2] []であるヒープとカウンター。
Peter Taylor

4
@ピーター・テイラーしかし、そのコードをどれだけ短くできますか?
migimaru 2011

回答:


3

Ruby 1.9 (235) (225) (222)(214)

これがダイクストラに基づくプログラムよりも短いかどうかはわかりませんが、別のアプローチを試してみると思いました。これは、ループ内で正規表現を使用して、各スペースに到達するために必要な最小数の壁で各スペースをマークします。

w=".{#{/\s/=~s=$<.read}}?"
j="([.x])"
s[0]=v=s[0]<?x??0:?1
(d=v[-1];n=v.succ![-1]
0while(s.sub!(/#{j}(#{w+d})/m){($1<?x?d: n)+$2}||s.sub!(/(#{d+w})#{j}/m){$1+($2<?x?d: n)}))while/#{j}/=~q=s[-2]
p v.to_i-(q==n ?0:1)

入力は、コマンドラインでファイルとして指定されます。

> ruby1.9.1 golf.rb maze.txt

非ゴルフ:

# read in the file
maze = $<.read

# find the first newline (the width of the maze)
width = /\s/ =~ maze

# construct part of the regex (the part between the current cell and the target cell)
spaces = ".{#{width}}?"

# construct another part of the regex (the target cell)
target = "([.x])"

# set the value of the first cell, and store that in the current wall count
maze[0] = walls = (maze[0] == "x" ? "1" : "0")

# loop until the goal cell is not "." or "x"
while /#{target}/ =~ (goal = s[-2])

  # store the current wall count digit and the next wall count digit, while incrementing the wall count
  current = walls[-1]; next = walls.succ![-1]

  # loop to set all the reachable cells for the current wall count
  begin

    # first regex handles all cells above or to the left of cells with the current wall count
    result = s.sub!(/#{target}(#{spaces + current})/m) {
      ($1 == 'x' ? next : current) + $2
    }

    # second regex handles all cells below or to the right of cells with the current wall count
    result = result || s.sub!(/(#{current + spaces})#{target}/m) {
      $1 + ($2 == 'x' ? next : current)
    }
  end while result != nil
end

# we reached the goal, so output the wall count if the goal was a wall, or subtract 1 if it wasn't
puts walls.to_i - (goal == next ? 0 : 1)

2

Perl 5.10(164)

undef$/;$_=<>;/\n/;$s="(.{$-[0]})?";substr$_,0,1,($n=/^x/||0);
until(/\d$/){1while s/([.x])($s$n)/$n+($1eq x).$2/se
+s/$n$s\K[.x]/$n+($&eq x)/se;$n++}
/.$/;print"$&\n"

migimaruのソリューションとほとんど同じように、Perlを追加しただけです。5.10のために必要とされる\Kの中でs///


これは9つ以上の壁を通過する必要がある迷路を適切に処理しますか?
migimaru 2011

@migimaruいいえ。文字数をわずかに増やすだけで最大45程度まで取得でき、さらに数を少し増やすだけでほぼ無制限の数まで取得できましたが、それほどきれいではありません。
ホブ、2011

2

Python 406 378 360 348 418文字

import sys
d={}
n=0
for l in open(sys.argv[1]):
 i=0
 for c in l.strip():m=n,i;d[m]=c;i+=1
 n+=1
v=d[0,0]=int(d[0,0]=='x')
X=lambda *x:type(d.get(x,'.'))!=str and x
N=lambda x,y:X(x+1,y)or X(x-1,y)or X(x,y+1)or X(x,y-1)
def T(f):s=[(x,(v,N(*x))) for x in d if d[x]==f and N(*x)];d.update(s);return s
while 1:
 while T('.'):pass
 v+=1
 if not T('x'):break
P=[m]
s,p=d[m]
while p!=(0,0):P.insert(0,p);x,p=d[p]
print s, P

ウェイト付きの動きがxフィールド上にあるため、ダイクストラが簡略化されました。これは「ウェーブ」で行われます。最初にループして、.前に触れているフィールドを見つけて同じ重みにx設定します+1。一度前に触れているフィールドを見つけて重みを設定するのではありません。未訪問のフィールドがなくなるまで繰り返します。

最後に、すべてのフィールドの重みがわかります。

入力はコマンドラインでファイルとして指定されます。

python m.py m1.txt

更新:パスを出力します。


1

C ++バージョン(610 607 606 584)

#include<queue>
#include<set>
#include<string>
#include<iostream>
#include<memory>
#define S second
#define X s.S.first
#define Y s.S.S
#define A(x,y) f.push(make_pair(s.first-c,make_pair(X+x,Y+y)));
#define T typedef pair<int
using namespace std;T,int>P;T,P>Q;string l;vector<string>b;priority_queue<Q>f;set<P>g;Q s;int m,n,c=0;int main(){cin>>m>>n;getline(cin,l);while(getline(cin,l))b.push_back(l);A(0,0)while(!f.empty()){s=f.top();f.pop();if(X>=0&&X<=m&&Y>=0&&Y<=n&&g.find(s.S)==g.end()){g.insert(s.S);c=b[X][Y]=='x';if(X==m&&Y==n)cout<<-(s.first-c);A(1,0)A(-1,0)A(0,1)A(0,-1)}}}

ダイクストラのアルゴリズムを実装します。

ゴルフなし:

#include<queue>
#include<set>
#include<string>
#include<iostream>
#include<memory>

using namespace std;
typedef pair<int,int>P;
typedef pair<int,P>Q;

int main()
{
    int             m,n;
    string          line;
    vector<string>  board;

    cin >> m >> n;getline(cin,l);
    while(getline(cin,line))
    {
        board.push_back(line);
    }

    priority_queue<Q>   frontList;
    set<P>              found;
    frontList.push(make_pair(0,make_pair(0,0)));
    while(!frontList.empty())
    {
        Q s=frontList.top();
        frontList.pop();
        if(   s.second.first>=0
           && s.second.first<=m
           && s.second.second>=0
           && s.second.second<=n
           && found.find(s.second)==found.end()
        )
        {
            found.insert(s.second);
            int c=board[s.second.first][s.second.second]=='x';
            if(  s.second.first==m
              && s.second.second==n
            )
            {   cout<<-(s.first-c);
            }
            frontList.push(make_pair(s.first-c,make_pair(s.second.first+1,s.second.second)));
            frontList.push(make_pair(s.first-c,make_pair(s.second.first-1,s.second.second)));
            frontList.push(make_pair(s.first-c,make_pair(s.second.first,s.second.second+1)));
            frontList.push(make_pair(s.first-c,make_pair(s.second.first,s.second.second-1)));
        }
    }
}
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.