LoginSignup
0
0

More than 5 years have passed since last update.

灯りと鏡 (Crystal/C++)

Last updated at Posted at 2018-09-10

多言語でやるべしという圧を感じましてorz ( https://twitter.com/Nabetani/status/1038998668248895488 )

tyama_henae27pre.cr
#!/usr/bin/env crystal
#http://nabetani.sakura.ne.jp/hena/orde27ligmir/
#https://qiita.com/Nabetani/items/0b2f459ec128c89948f4

#x,y
D={
    '.'=>{
        [0,-1]=>[0,-1],  #N
        [1,0]=>[1,0],   #E
        [0,1]=>[0,1],   #S
        [-1,0]=>[-1,0], #W
    },
    '1'=>{
        [0,-1]=>[1,0],  
        [1,0]=>[0,-1],
        [0,1]=>[-1,0],
        [-1,0]=>[0,1],
    },
    '0'=>{
        [0,-1]=>[-1,0],  
        [1,0]=>[0,1],
        [0,1]=>[1,0],
        [-1,0]=>[0,-1],
    },
}
H=5;W=5

while line=gets
    m=line.chomp.split('/').map(&.chars)
    x,y=[0,0]
    H.times{|h|W.times{|w|
        if m[h][w]=='Y'
            x,y=w,h
            m[h][w]='.'
        end
    }}
    r=Array(Tuple(Int32,Int32)).new
    dx,dy=[0,-1]
    visited={[x,y,dx,dy]=>1}
    while 0<=x&&x<W && 0<=y&&y<H && m[y][x]!='x'
        r<<{x,y}
        dx,dy=D[m[y][x]][[dx,dy]]
        x+=dx
        y+=dy
        k=[x,y,dx,dy]
        break if visited.has_key?(k)
        visited[k]=1
    end
    puts r.map{|x,y|(x+y*W+97).chr}.uniq.sort.join("")
    STDOUT.flush
end
tyama_henae27pre.cpp
//http://nabetani.sakura.ne.jp/hena/orde27ligmir/
//https://qiita.com/Nabetani/items/0b2f459ec128c89948f4

#include <iostream>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <cstdio>
using namespace std;

//x,y
map<
    char,map<
        pair<int,int>,pair<int,int>
    >
>D={
    {'.',{
        {{0,-1},{0,-1}},  //N
        {{1,0},{1,0}},   //E
        {{0,1},{0,1}},   //S
        {{-1,0},{-1,0}}, //W
    }},
    {'1',{
        {{0,-1},{1,0}},  
        {{1,0},{0,-1}},
        {{0,1},{-1,0}},
        {{-1,0},{0,1}},
    }},
    {'0',{
        {{0,-1},{-1,0}},  
        {{1,0},{0,1}},
        {{0,1},{1,0}},
        {{-1,0},{0,-1}},
    }},
};
const int H=5,W=5;

vector<string> split(string &str, const char *delim){
    vector<string> result;
    int cutAt;
    while( (cutAt = str.find_first_of(delim)) != str.npos ){
        if(cutAt > 0){
            result.push_back(str.substr(0, cutAt));
        }
        str = str.substr(cutAt + 1);
    }
    if(str.length() > 0){
        result.push_back(str);
    }
    return result;
}

int main(){
    string line;
    for(;getline(cin,line);){
        vector<string>m=split(line,"/");
        int x=0,y=0;
        for(int h=0;h<H;h++)for(int w=0;w<W;w++){
            if(m[h][w]=='Y'){
                x=w,y=h;
                m[h][w]='.';
            }
        }
        set<char>r;
        int dx=0,dy=-1;
        set<vector<int>>visited={{x,y,dx,dy}};
        for(;0<=x&&x<W && 0<=y&&y<H && m[y][x]!='x';){
            r.insert(x+y*W+97);
            int ndx,ndy;
            tie(ndx,ndy)=D[m[y][x]][{dx,dy}];
            dx=ndx,dy=ndy;
            x+=dx;
            y+=dy;
            vector<int>k={x,y,dx,dy};
            if(visited.find(k)!=visited.end())break;
            visited.insert(k);
        }
        for(auto &e:r)putchar(e);
        puts("");
        fflush(stdout);
    }
}
0
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
0
0