एक भूलभुलैया में दीवारों को नीचे गिराएं


10

नियम:

इस खेल में आप दीवारों और खुले स्थानों से बने आकार एन एक्स एम के एक आयताकार ग्रिड के शीर्ष पर शुरू करते हैं। इनपुट एम वर्णों की एन लाइनें है, जहां एक .खुली जगह निर्दिष्ट करता है और xएक दीवार निर्दिष्ट करता है। आपके प्रोग्राम को सबसे छोटी संख्या 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

अतिरिक्त टिडबिट्स:

यदि यह आपके जीवन को आसान बनाता है, तो आप एन और एम को कमांड लाइन मापदंडों के रूप में निर्दिष्ट कर सकते हैं।

अतिरिक्त क्रेडिट यदि आप अपने कार्यक्रम को किसी न किसी रूप में पथ प्रिंट कर सकते हैं।


4
: जंभाई: दिज्क्स्त्र, एक ढेर के साथ जो एक वी [2] [] और एक काउंटर है।
पीटर टेलर

4
@Peter टेलर लेकिन आप उस कोड को कितना छोटा बना सकते हैं?
मिगिमारू

जवाबों:


3

रूबी 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

Ungolfed:

# 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

पर्ल 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"

बहुत अधिक लाइनों के साथ ही माइग्रिमारू के समाधान के रूप में, केवल उस अतिरिक्त पर्ल स्पर्श के साथ। में 5.10 की जरूरत \Kहै s///


क्या यह ठीक से 9 से अधिक दीवारों से गुजरने वाले मेज़ों को संभालता है?
मिगिमारू

@ मिगिमारू नं। मैं इसे केवल ४५ तक या पात्रों में मामूली वृद्धि के साथ प्राप्त कर सकता हूं, और लगभग एक-असीमित संख्या में एक और छोटी वृद्धि के साथ - लेकिन यह बहुत सुंदर नहीं होगा।
हॉब्स

2

पायथन 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

सी ++ संस्करण (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)}}}

दीप्क्स्ट्रा के एल्गोरिदम को लागू करता है।

संयुक्त राष्ट्र के golfed:

#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)));
        }
    }
}
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.