2
0

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 1 year has passed since last update.

迷路 あるいは カリグラム的プログラム

Last updated at Posted at 2023-04-29

迷路を出力するプログラムを Rust で書いてみました。

use rand::seq::SliceRandom;struct M{w:usize,h:usize,
r:Vec<R>}impl M{fn new(w:usize,h:usize)->Self{let r:
Vec<            R>=             vec!            [];M
{w,h            ,r}}            fn b(           &mut
self    ){if    self.   r.len   ()>0    {self.r=vec!
[];}    let     mut     ws:     Vec<    (usize,usize,
bool    )>=             vec!            [];     for
rid     in 0            ..self          .w*     self
.h{     self.r.push(R::new(     rid));if rid%   self
.w<     self.w-1{ws.push((      rid,rid+1,true  ));}
if      rid/    self            .w<     self    .h-1
{ws.    push(   (rid,           rid+    self    .w,
false));}}      let     mut     rng=    rand      ::
thread_rng()    ;ws.  shuffle   (&mut   rng);   for(
rid1,           rid2,   v)in    ws{let          aid1
=self           .r[     rid1]   .aid            ;let
aid2=self.r[    rid2].aid;if   aid1==  aid2{continue
;}if aid1<      aid2{self.u(    aid2,   aid1);}else{
self                            .u(             aid1,
aid2                            );}            if v{
self.   r[rid1].e=false;}else   {self   .r[rid1].s=
false   ;}}}fn u(&mut self,     aid1:   usize,aid2:
usize   ){for                                   i in
0..     self.w                                 *self
.h{if self.r[i].aid==aid1{self.r[i].aid=aid2;}}}fn p
(&self){println!("{}+","+-".repeat(self.w));for i in


 0..   self       .h{         print!("|"  );for j in
 0..   self       .w{             print!  (" {}"
 ,if   self      .r[i*            self    .w+j
 ].e  {"|"}      else{           " "})    ;}print!
 ( "\n+" );     for j in        0..self   .w{print!
 ("{}+" ,if    self.r[i*      self        .w+j
 ].s{    "-"}  else  {" "}    );}println!  ("");}}
}struct R{aid :usize,e:bool, s:bool,}impl R{fn new(aid

  :usize)->Self{Self{aid:aid,e:true,s:true,}}}fn main(){
        let mut m=M::new(20,20);m.b();m.p()}

プログラム書くよりもバナー作る方がめんどくさかった。

AKAMAI Tシャツに対抗してこれでTシャツ作るか!

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?