치즈 찾기


25

업데이트 : 6 개의 미로가 있습니다. 그것들은 컨트롤러에 포함되어 있습니다. 여기에 미로의 tar.gz와 .bmp 파일이 있습니다 (드롭 박스). 해당 링크에서 더 많은 미로를 만드는 유틸리티도 있습니다 (아카이브에서 maze_4.txt 파일이 올바르지 않음). 이 시점에서 자신의 항목을 실행하고 점수를 업데이트하십시오. 이를 수행하는 방법에 대한 자세한 내용은 하단에 포함되어 있습니다. 궁금한 점이나 문제가 있으면 채팅으로 핑 (Ping) 해주세요.


당신은 마우스입니다. 당신은 미로에 있습니다. 치즈를 찾으십시오.

개념

직사각형 그리드에 존재하는 미로에 있습니다. 그리드의 각 공간에는 여러 가지 중 하나가 포함됩니다.

  • ! -지나갈 수없는 벽
  • -지나갈 수있는 빈 공간
  • O -마우스
  • + -치즈, 목표

컨트롤러를 수정할 필요가 없도록 동일한 문자를 사용하십시오.

매 턴마다 현재 위치의 북쪽, 남쪽, 동쪽 및 서쪽에있는 타일이 제공됩니다. 그런 다음 이동하려는 방향을 출력해야합니다. 치즈에 도착하면 이깁니다. 단계가 적을수록 좋습니다.

입력

stdin을 통해 다음과 같은 방식으로 입력이 제공됩니다. nesw여기서 각 문자는 해당 나침반 지점의 타일을 나타냅니다. 예를 들어 현재 상태가

  !          <--- Wall
 !O          <--- You
  +          <--- Cheese

그런 다음 문자열이 제공 ! +!됩니다.

게임이 끝나면 컨트롤러는 네 개의 0으로 구성된 문자열을 보냅니다 : 0000. 이 문자열을 받으면 프로그램을 종료해야합니다. 다른 입력은 0문자 를 포함하지 않습니다 .

다른 모든 입력은 무시하십시오.

산출

당신은 출력 하나의 문자에있다 n, s, e, 또는 w, 새 라인 문자 다음에, 여행에 할 방향을 나타냅니다.

채점

각 시험에 대한 점수는 치즈를 찾는 데 걸리는 단계 수입니다.

전체 점수는 다양한 크기의 미로 배터리에서 미로 당 평균 점수의 합이며 길이는 50의 제곱 안에 맞습니다.

예를 들어, 6 개의 미로를 완료하기 위해 봇 (100)이 움직이면 점수는 600입니다.

봇이 결정적이지 않은 경우 각 미로를 10 회 시도하고 평균을 해당 미로의 점수로 사용하십시오. 최종 점수는 모든 평균의 합입니다.

규칙

  • 각 미로는 50x50 정사각형 안에 들어갑니다.
  • 각 미로는 시작부터 치즈까지 유효한 경로가 하나 이상 있습니다.
  • 치즈가 본질적으로 미로로 나가는 역할 을 하도록 치즈가 항상 외벽에있는 것을 제외하고는 각각의 미로가 완전히 벽으로 막히게됩니다.
  • 벽에 부딪 치면 제출이 실격됩니다.
  • 제출이 너무 오래 걸리면 (테스트를 시작할 때 본인이 결정한대로) 실격 처리됩니다. 이것은 무한 루프를 방지하기위한 부분입니다. 소프트 리미트는 미로마다 1 분이지만 언제든지 어느 방향 으로든 변경할 수 있습니다.
  • 출품작은 결정론적일 필요는 없지만 너무 무작위 인 경우 위의 시점에서 실격 처리 될 수 있습니다.
  • 어느 시점에서 미로의 배터리가 풀리고 향후 답변이 최적화되지 않을 수 있으며 변경 될 수 있습니다.

제출 :

제출은 stdin을 통해 입력하고 stdout을 통해 출력하는 전체 프로그램입니다. 제출이 미로 컨트롤러와 상호 작용하기 때문에 이것은 중요합니다. 자유롭게 구할 수없는 언어는 금지하지 않겠지 만, 언어에 액세스 할 수없는 경우 다른 사람이 테스트를 실행할 시간을 제공해야한다는 것을 알고 있습니다.

제출 방법에 대한 지침을 포함하십시오.

제출 한 내용이 결정적인지 여부를 명시하여 여러 번 실행해야하는지 알 수 있도록하십시오.

테스트 미로

테스트 미로에서 .캐릭터는 치즈까지 가장 짧은 경로를 나타냅니다. 그것들은 (공백) 문자와 같습니다. 제출 한 내용이 보이지 않습니다. 컨트롤러는 공백으로 대체합니다.

!!!!!!!!+!
!O..!....!
! !.!.! !!
! !.!.!  !
! !.!.!! !
!!!.!.!! !
!  .!.! !!
!!!...   !
!!!!!!!!!!

31x31 테스트 미로. 부끄러운 도난 .

 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
 ! O...!.......!   !  .....!.....!           !             !   ! 
 !!!!!.!.! !!!.! !!! !.!!!.!.!!!.!!!!!!! !!! !!!!!!!!! !!! ! ! ! 
 !   !...!   !.!     !.! !.!.! !.!       !   !         !   ! ! ! 
 ! !!!!! !!! !.!!!!!!!.! !.!.! !.! !!!!!!! !!! ! !!!!!!! !!! ! ! 
 !     !     !.........! !...!...!     !   !   ! !     !   ! ! ! 
 ! !!! !!!!!!!!!!!!!!!!! !!!!!.!!! !!! !!! ! ! !!! !!! !!! !!! ! 
 !   !         !     !   !.....!     !   ! ! ! !     !   !   ! ! 
 !!!!!!!!!!! ! ! !!! !!! !.!!!!!!!!!!!!! ! !!! ! !!!!!!! !!! ! ! 
 !           !   !       !.!             !   !     !     !     ! 
 ! !!!!!!! !!!!!!! !!!!!!!.! !!!!!!!!!!!!!!! !!!!!!! !!!!!!!!!!! 
 ! !     ! !   !     !...!.!           !   !       ! !         ! 
 ! !!! ! ! ! ! !!!!!!!.!.!.! !!!!!!!!! ! ! !!!!!!! ! ! !!!!!!! ! 
 !   ! !   ! !       !.!...! !         ! !       ! ! !   !   ! ! 
 !!! ! !!!!! !!!!!!! !.!!!!!!! !!!!!!!!! !!! !!!!! ! !!! ! !!! ! 
 !   !   ! ! !       !...!     !   !     ! !           ! !   ! ! 
 ! !!!!! ! ! ! !!!!!!!!!.! !!!!! !!! !!!!! !!!!!!!!!!! ! ! ! ! ! 
 ! !       ! !   !   !...! !       ! !       !   !     ! ! ! ! ! 
 ! !!!!!!!!! !!! ! ! !.!!! !!!!!!! ! !!!!!!! ! ! !!!!!!! !!! ! ! 
 !             !   ! !...!       ! !     !   ! !             ! ! 
 !!!!!!!!!!!!!!!!!!! !!!.!!!!!!! ! !!!!! ! !!! !!!!!!!!!!!!!!! ! 
 !               !   !...!       !         !   !     !   !     ! 
 ! !!!!!!!!!!!!! ! ! !.!!! !!!!!!! !!!!!!!!! !!! !!! !!! ! !!! ! 
 ! !   !       !   ! !.! !     ! ! ! !     !     !   !   !   ! ! 
 ! ! ! !!!!! !!!!!!! !.! ! !!! ! ! ! ! !!! !!!!!!! !!! !!!!! !!! 
 !   ! !   !       ! !.!     ! !     ! ! !     !   !       !   ! 
 !!!!! ! ! !!! !!! ! !.!!!!!!! !!!!!!! ! ! !!! ! !!!!!!!!! !!! ! 
 !     ! !   !   !   !.......!       ! ! ! !   !   !         ! ! 
 ! !!!!! !!! !!! !!!!!!!!!!!.!!!!!!! ! ! ! !!!!!!! ! !!!!!!! ! ! 
 !         ! !           !...!       ! ! !     !   ! !       ! ! 
 !!!!!!!!!!! !!!!!!!!!!! !.!!! !!!!!!! ! !!!!! ! !!! !!!!!!!!! ! 
 !         !     !     ! !.!       !   !     ! !     !         ! 
 ! !!!!!!! !!!!! ! !!! !!!.!!!!!!! ! !!!!! ! ! !!!!! ! !!!!!!!!! 
 ! !     !     !   ! !   !.......! !       ! !       !         ! 
 ! ! !!! !!!!! ! !!! !!! !!!!!!!.! !!!!!!!!! !!!!!!!!!!!!!!!!! ! 
 !     !     ! !   !   ! !     !.!       !   ! !     !         ! 
 !!!!!!!!!!! ! !!! !!! ! ! ! !!!.! ! !!!!! !!! ! !!! ! !!!!!!! ! 
 !           ! !       !   ! !...! !       !   ! ! ! !     !   ! 
 ! !!!!!!!!!!! !!!!!!!!!!!!! !.!!! !!!!!!!!!!! ! ! ! ! !!! ! !!! 
 !       !   !             ! !.! !   !         ! !   !   ! ! ! ! 
 !!!!!!! !!! !!!!!!!!!!!!! ! !.! !!! ! !!!!!!! ! !!! !!!!! ! ! ! 
 !       !         !     ! ! !.!   !   !     ! !   !       !   ! 
 ! !!!!!!! !!!!!!! ! !!!!! ! !.!!! !!!!!!! ! ! !!! !!!!!!!!!!!!! 
 !   !         ! !   !       !.!           ! !   !             ! 
 ! ! ! !!!!!!! ! ! !!! !!!!!!!.! !!!!!!!!!!! ! !!!!!!!!!!!!!!! ! 
 ! ! ! !     ! !   !   ! !.....!   !   !     ! !...............! 
 ! ! !!! !!! ! !!!!! !!! !.!!!!! ! ! ! !!!!!!! !.!!!!!!!!!!!!!.! 
 ! !   !   ! !   !       !...!   !   !         !.!         !...! 
 !!!!! !!! ! !!! ! !!!!!!!!!.!!!!!!! !!!!!!!!!!!.!!!!! !!!!!.!!! 
 !     !   !   !   !       !.......!       !...!.....!      .! ! 
 ! !!!!! !!!!! !!!!! !!!!! !!!!!!!.!!!!!!!!!.!.!!!!!.!!!!!!!.! ! 
 !           !     ! !   !   !   !...........!...!...!.....!...! 
 ! !!!!!!!!! !!!!! ! !!! ! !!! ! !!!!!!!!!!!!!!!.!.!!!.!!!.!!!.! 
 ! !     !       ! !     !     !     !         !.!.....  !...!.! 
 !!! !!! !!!!!!!!! !!!!! !!!!!!!!! ! !!!!!!! !!!.! !!!!!!!!!.!.! 
 !   !     !   !   !   ! !       ! !         !...! !.........!.! 
 ! !!!!!!! ! ! ! ! !!! ! !!!!!!! ! !!!!!!!!! !.!!!!!.!!!!!!!!!.! 
 !       !   !   ! !   !         !   ! !   ! !.!.....!       !.! 
 ! !!!!! !!!!!!!!! ! !!!!!!!!!!! !!! ! ! ! ! !.!.!!!!! !!!!! !.! 
 ! !     !           !         ! ! ! !   !   !.!...!   !     !.! 
 ! ! !!!!!!!!!!!!!!!!! !!! !!!!! ! ! !!!!!!!!!.!!!.! !!!!!!!!!.! 
 ! !                     !         !          .....!          .! 
 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!+! 

제어 장치

컨트롤러가 녹 상태 (1.11 야간)

    type Maze = Vec<Vec<char>>;

fn str_to_maze(input: &str) -> Result<Maze,i32> {
    let mut maze: Vec<Vec<char>> = vec![ vec![] ];
    let mut row: Vec<char> = vec![];

    for c in input.chars() {
        if c == '!' || c == '+' || c == 'O'  || c == ' ' {
            row.push(c);
        }
        else if c =='.' {
            row.push(' ');
        }
        else if c == '#' {
            maze.push(row);
            row = vec![];
        }
        else if c =='\0' {
            break;
        }  
        else {
            println!("Bad character in maze: {}, exiting.", c);
            return Err(1);
        }
    }
    return Ok(maze);
}

fn display_maze(maze: &Maze, position: [usize;2]) {
    for y in 0..maze.len() {
        for x in 0..maze[y].len() {
            if [x,y] == position {
                print!("O");
            }
            else if maze[y][x] == '#' {
                println!("\n");
            }
            else {
                print!("{}",maze[y][x]);
            }
        }
        println!("");
    }
    println!("\n");
}

fn get_starting_position(maze: &mut Maze) -> Result<[usize;2],&str> {
    for y in 0..maze.len() {
        for x in 0..maze[y].len() {
            if maze[y][x] == 'O' {
                maze[y][x] = ' ';
                return Ok([x,y]);
            }
        }
    }
    return Err("No mouse found");
}

enum State {
    Continue([char;4]),
    Win,
    Disqualify,
}

fn output(maze: &Maze, position: [usize;2]) -> State {
    let x = position[0];
    let y = position[1];
    if maze[y][x] == '+' {
        return State::Win;
    }
    else if maze[y][x] == '!' {
        return State::Disqualify;
    }

    let n = maze[y-1][x];

    assert!(y+1<maze.len());
    let s = maze[y+1][x];


    let w = maze[y][x-1];

    assert!(x+1<maze[y].len());
    let e = maze[y][x+1];

    return State::Continue([n,e,s,w]);
}

fn get_input() -> char {
    use std::io;
    use std::io::Read;
    let mut buffer: [u8;2] = [0;2];
    io::stdin().read_exact(&mut buffer).unwrap();
    //println!("{:?}", buffer); // to see exactly what the input is
    return buffer[0] as char;
}

fn next_position(current_position: [usize;2], direction: char) -> Result<[usize;2],char> {
    let mut x = current_position[0];
    let mut y = current_position[1];
    if direction == 'n' {
        y -= 1;
    }
    else if direction == 'e' {
        x += 1;
    }
    else if direction == 's' {
        y += 1;
    }
    else if direction == 'w' {
        x -= 1;
    }
    else {
        return Err(direction);
    }
    return Ok([x,y]);
}


fn play(maze: &mut Maze) -> (State, usize) {
    let mut position: [usize;2];
    match get_starting_position(maze) {
        Ok(pos) => position = pos,
        Err(s) => {
            println!("{}",s);
            std::process::exit(2);
        }
    }

    let mut moves = 0;

    loop {

        let state = output(maze, position);

        /* uncomment below to view the maze at each step  */
        //display_maze(&maze, position);                
        /* ----------------------------------------------*/

        match state {
            State::Win => {
                //println!("You found the cheese");
                return(State::Win, moves);
            }
            State::Disqualify => {
                //println!("You were disqualified");
                return(State::Disqualify, moves);
            }
            State::Continue(out) => {
                println!("{}{}{}{}",out[0],out[1],out[2],out[3]);
            }
        }
        // only get here with Continue
        let input = get_input();
        moves += 1;
        match next_position(position, input) {
            Err(c) => {
                println!("Invalid input: {}", c as u8);
                return (State::Disqualify, moves);
            }
            Ok(next_pos) => position = next_pos,
        }
    }    
}

fn main() {

    let mut arg_counter = 0; 
    for argument in std::env::args() {
        if arg_counter != 0 {

            let mut maze = match argument.as_str(){
                "1" => maze_1(),
                "2" => maze_2(),
                "3" => maze_3(),
                "4" => maze_4(),
                "5" => maze_5(),
                "6" => maze_6(),
                _ => {
                    println!("invalid input: {}, breaking", argument);
                    break;
                }

            };

            let game_result = play(&mut maze);

            println!("0000");
            match game_result.0 {
                State::Win => println!("WIN"),
                State::Disqualify => println!("DISQUALIFY"),
                _ => println!("Error"),
            }
            println!("moves: {}", game_result.1 );
        }
        arg_counter += 1;
    }

}

fn maze_1() -> Maze {
    let maze_str = "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#\
                    !O !                    !      !#\
                    !. ! !!!!!!!!!!!!!!!!!! ! !!!!!!#\
                    !. !                    ! !    !#\
                    !. ! !!!!!!!!!!!!!!!!!!!! ! !! !#\
                    !. !...........           ! !!.+#\
                    !. !.!!!!!!!!!.!!!!!!!!!!!!!!!.!#\
                    !.!..!        ...............!.!#\
                    !.!.!! !!!!!!!!!!!!!!!!!!!!!.!.!#\
                    !.!.!! !!!  !               .!.!#\
                    !.!.!! !!!  !!!!!!!!!!!!!!!!.!.!#\
                    !...!! !!!                  .!.!#\
                    ! ! !! !!!                  .!.!#\
                    ! ! !! !!!  !!!!!!!!! !!!!!!.!.!#\
                    ! ! !! !!!  !      !! !     .!.!#\
                    ! ! !! !!!  ! !!!!!!! !     .!.!#\
                    ! ! !! !!!  !      !! !     .!.!#\
                    ! ! !! !!!  !      !! !     .!.!#\
                    ! ! !!   !  !      !! !     .!.!#\
                    ! ! !! ! !  !!!!!! !! !     .!.!#\
                    ! ! !! ! !  !      !! !     ...!#\
                    ! ! !! ! !  !      !! !        !#\
                    ! ! !! ! !  !      !! !      ! !#\
                    ! ! !! ! !  !  !!!!!! !      ! !#\
                    ! ! !! ! !  !      !! !      ! !#\
                    ! !    !    !      !! !      ! !#\
                    ! !!!!!!  !!!!!!!! !! !      ! !#\
                    !                ! !! !      ! !#\
                    ! !!!!!!!!!!! !!!! !! !      ! !#\
                    !                     !      ! !#\
                    ! !!!!!!!! !!!!       !        !#\
                    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#\
                    ";

    match str_to_maze(&maze_str) {
        Ok(x) => return x,
        Err(i) => std::process::exit(i),
    }
}

fn maze_2() -> Maze {
    let maze_str = "!!!!!!!!!!!!!!!#\
                    !      .......!#\
                    ! !!! !.!!!! .!#\
                    !   ! !.!!O!!.!#\
                    !!!   !....! .!#\
                    !   !!!!!!!!!.!#\
                    ! !!        ..!#\
                    !  !!!!!!!!!.!!#\
                    !           ..+#\
                    !!!!!!!!!!!!!!!#\
                    ";

    match str_to_maze(&maze_str) {
        Ok(x) => return x,
        Err(i) => std::process::exit(i),
    }
}

fn maze_3() -> Maze {
    let maze_str = "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#\
                    !                            !!!#\
                    ! !  !!!  !!!!!!!!!!!!!!!!!!   !#\
                    ! !  ! !!!              !!!  ! !#\
                    ! !  ! !!!!!!!!!!!!!!!!     !! !#\
                    ! !  !                  !!!!   !#\
                    ! !! !!!!!!!!!!        !!    ! !#\
                    !  ! !        !!!! !!!!!   !!! !#\
                    !! ! ! !!!!      !         !   !#\
                    !! ! ! !!!!   !!!!!!!!!!!!!! ! !#\
                    !! ! ! !!!! ! !              ! !#\
                    !! ! ! !!!! ! ! !!!!       ! ! !#\
                    !! ! ! !!!! ! ! !   !!!    ! ! !#\
                    !  ! ! !!!! ! ! !     !!!  ! ! !#\
                    !  ! ! !!!! ! ! !!!!!      !   !#\
                    !  ! ! !!!! !!! !   !!     ! !!!#\
                    !  ! !  !!!  !! !    !!!   ! !!!#\
                    !  ! !     ! !! !!!!   !!  ! !!!#\
                    !  ! !!    ! !! !  !!      ! !!!#\
                    !  !  !   !! !!     !!!    ! !!!#\
                    !  !! !!!!     !!!    !!   !   !#\
                    !!  ! !! !       !!!   !!  !!! !#\
                    !!  !    !    !    !           !#\
                    !!  !!!!!!    !!   !!!!!!!!!!! !#\
                    !             !!!! !!!!!!!!!!! !#\
                    !  ..........O!!!! !!!!!!!!!!!.+#\
                    !! .!!!!!!    !!   !!!!!!!!!!!.!#\
                    !! .!    !    !    !          .!#\
                    !!..! !! !       !!!   !!  !!!.!#\
                    ! .!! !!!!     !!!    !!   !...!#\
                    ! .!  !   !! !!     !!!    !.!!!#\
                    ! .! !!    ! !! !  !!      !.!!!#\
                    ! .! !     ! !! !!!!   !!  !.!!!#\
                    ! .! !  !!!  !! !    !!!   !.!!!#\
                    ! .! ! !!!! !!! !   !!     !.!!!#\
                    ! .! ! !!!! ! ! !!!!!      !...!#\
                    ! .! ! !!!! ! ! !     !!!  ! !.!#\
                    !!.! ! !!!! ! ! !   !!!    ! !.!#\
                    !!.! ! !!!! ! ! !!!!       ! !.!#\
                    !!.! ! !!!! ! !              !.!#\
                    !!.! ! !!!!   !!!!!!!!!!!!!! !.!#\
                    !!.! ! !!!!      !         !  .!#\
                    !..! !        !!!! !!!!!   !!!.!#\
                    !.!! !!!!!!!!!!        !!    !.!#\
                    !.!  !                  !!!!  .!#\
                    !.!  ! !!!!!!!!!!!!!!!!     !!.!#\
                    !.!  ! !!!              !!!  !.!#\
                    !.!  !!!  !!!!!!!!!!!!!!!!!!...!#\
                    !............................!!!#\
                    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#\
                    ";

    match str_to_maze(&maze_str) {
        Ok(x) => return x,
        Err(i) => std::process::exit(i),
    }
}

fn maze_4() -> Maze {
    let maze_str = "!!!!!!!!!!!!!!!!!+!!!!!!!!!!!!!!#\
                    !.................           !!!#\
                    !.!  !!!  !!!!!!!!!!!!!!!!!!   !#\
                    !.!  ! !!!              !!!  ! !#\
                    !.!  ! !!!!!!!!!!!!!!!!     !! !#\
                    !.!  !                  !!!!   !#\
                    !.!! !!!!!!!!!!        !!    ! !#\
                    !..! !        !!!! !!!!!   !!! !#\
                    !!.! ! !!!!      !         !   !#\
                    !!.! ! !!!!   !!!!!!!!!!!!!! ! !#\
                    !!.! ! !!!! ! !              ! !#\
                    !!.! ! !!!! ! ! !!!!       ! ! !#\
                    !!.! ! !!!! ! ! !   !!!    ! ! !#\
                    ! .! ! !!!! ! ! !     !!!  ! ! !#\
                    ! .! ! !!!! ! ! !!!!!      !   !#\
                    ! .! ! !!!! !!! !   !!     ! !!!#\
                    ! .! !  !!!  !! !    !!!   ! !!!#\
                    ! .! !     ! !! !!!!   !!  ! !!!#\
                    ! .! !!    ! !! !  !!      ! !!!#\
                    ! .!  !   !! !!     !!!    ! !!!#\
                    ! .!! !!!!     !!!    !!   !   !#\
                    !!. ! !! !       !!!   !!  !!! !#\
                    !!. !    !    !    !           !#\
                    !!. !!!!!!    !!   !!!!!!!!!!! !#\
                    ! ........... !!!! !!!!!!!!!!! !#\
                    !           . !!!! !!!!!!!!!!! !#\
                    !!  !!!!!!  . !!   !!!!!!!!!!! !#\
                    !!  !    !  . !    !           !#\
                    !!  ! !! !  .    !!!   !!  !!! !#\
                    !  !! !!!!  .  !!!    !!   !   !#\
                    !  !  !   !!.!!     !!!    ! !!!#\
                    !  ! !!    !.!! !  !!      ! !!!#\
                    !  ! !     !.!! !!!!   !!  ! !!!#\
                    !  ! !  !!!..!! !    !!!   ! !!!#\
                    !  ! ! !!!!.!!! !   !!     ! !!!#\
                    !  ! ! !!!!.! ! !!!!!      !   !#\
                    !  ! ! !!!!.! ! !     !!!  ! ! !#\
                    !! ! ! !!!!.! ! !   !!!    ! ! !#\
                    !! ! ! !!!!.! ! !!!!       ! ! !#\
                    !! ! ! !!!!.! !              ! !#\
                    !! ! ! !!!!.  !!!!!!!!!!!!!! ! !#\
                    !! ! ! !!!!.....O!         !   !#\
                    !  ! !        !!!! !!!!!   !!! !#\
                    ! !! !!!!!!!!!!        !!    ! !#\
                    ! !  !                  !!!!   !#\
                    ! !  ! !!!!!!!!!!!!!!!!     !! !#\
                    ! !  ! !!!              !!!  ! !#\
                    ! !  !!!  !!!!!!!!!!!!!!!!!!   !#\
                    !                            !!!#\
                    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#\
                    ";

    match str_to_maze(&maze_str) {
        Ok(x) => return x,
        Err(i) => std::process::exit(i),
    }
}


fn maze_5() -> Maze {
    let maze_str = "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#\
                    +......!!!!       !!!        !!!       !!!!     !!#\
                    !     .!       !!                            !!!!!#\
                    !  !!!.! !!      !!!  !!!!  !!!!!!!!!      !!!   !#\
                    ! !!...!   !!!!!   !!    !          !!    !!     !#\
                    !!!..!!         !    !!  !           !    !     !!#\
                    !! .!!........        !! !!!     !    !   !  !  !!#\
                    !!!. !.  !  !.   !      !!!!!    !!   !   ! !! !!!#\
                    !!!. !.  !  !.   !       !!!!!    !   !!    !  !!!#\
                    !!.. !.  !  !..  !        !  !    !!   !    !   !!#\
                    !!.! !.!  ! ! ..  ! !!!!!!  !      !   !    !   !!#\
                    !!.! !.!  ! !! .  ! !      !        !  !    !   !!#\
                    !!.! !.!  !  ! .  ! !!   !!    !!!! !  !    !   !!#\
                    !!.! !.!! !  ! .  !  !!!   !!!!     !  !    !!  !!#\
                    !!.! !. ! !  ! .  !    !!        !  !   !    !  !!#\
                    ! .!!!. ! !  !!.   !    !        !  !   !    !   !#\
                    ! .!!!. ! !   !.   !     !       !   !  !    !   !#\
                    ! .! !. ! !   !.   !     !       !   !   !   !   !#\
                    ! .! !. ! !!  !....!!!   !      !!   !     ! !   !#\
                    ! .!  ..!  !  !   ...!!!!       !    !     ! ! ! !#\
                    ! .!   .!  !  !!!!!!.... !!!!!!!             ! ! !#\
                    ! .! !!.!  !! !  !!!!  .!                        !#\
                    ! .!!!!.!   !!!! !!!   .!   !!!!!   !!!!!!!!!!!  !#\
                    ! .. !!.!    !!!  !!  !.!!                       !#\
                    !!!.. !. !   !!!      !..!        !!!   !   !    !#\
                    !!! .... !  !!!!      ! .!        !             !!#\
                    !!!!!!!!!!!!!!!!!!!!!!! .! !!!!!!!!!!!!!!!!!!!! !!#\
                    !!!  !                  .!                     !!!#\
                    !!   !   !!!  !!        .!                      !!#\
                    !!   !  !     !!  !!!!!!.!  !!!!!!              !!#\
                    !!   !  !    !!   !!!!!!.! !!!!!!!!          !  !!#\
                    !!  !   !   !!   !!!!!!!.! !!!!!! !!   !  !     !!#\
                    !!  !   !   !   !!!!!!!!.! !!!  !  !   !        !!#\
                    !!  !   !   !  !!!!!!!!!.! !!!! !   !  !  !  !  !!#\
                    !!  !   !   !           .!  !!  !   !  !     !  !!#\
                    !! !!!  !   !   !!!!!!  .!      !    !!         !!#\
                    !! ! !   !  !   !     !!. !    !!!    !    !    !!#\
                    !! ! !   !  !   ! !     .  !   ! !!    !     !  !!#\
                    !! ! !   !  !   ! !!  !!.   !!!   !!   !   ! !  !!#\
                    !! ! !   !  !!  ! !!! ! .....     !!   !      ! !!#\
                    !! ! !   !   !  ! !!!!!!!   .     !    !        !!#\
                    !  ! !   !   !  !  !!!  !   .!!!!     !  !       !#\
                    !  ! !   !   !! !       !   .!      !!.......... !#\
                    ! !! !   !    !  !!!!!!!!!  .!    !!  .!   !!!!. !#\
                    ! !  !   !    !!       !!!  .!!!!!   ..   !    . !#\
                    ! !  !    !!   !!!     !!!  .......... !!!!    . !#\
                    ! !  !     !!     !!!!      !!!!!!!!!!!!      !. !#\
                    ! !         !         !!!!!!                  O. !#\
                    !           !                                    !#\
                    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#\
                    ";

    match str_to_maze(&maze_str) {
        Ok(x) => return x,
        Err(i) => std::process::exit(i),
    }
}

fn maze_6() -> Maze {
    let maze_str = "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#\
                    !      !!!!   ....!!!        !!!       !!!!     !!#\
                    !      !      .!!..........                  !!!!!#\
                    !  !!! ! !!   ...!!!  !!!!. !!!!!!!!!      !!!   !#\
                    ! !!   !   !!!!!.. !!    !.         !!    !!     !#\
                    !!!  !!         !.   !!  !.....      !    !     !!#\
                    !!  !!          ..    !! !!!  .  !    !   !  !  !!#\
                    !!!  !   !  !   .!      !!!!! .  !!   !   ! !! !!!#\
                    !!!  !   !  !   .!       !!!!!.   !   !!    !  !!!#\
                    !!   !   !  !   .!        !  !.   !!   !    !   !!#\
                    !! ! ! !  ! !   . ! !!!!!!  ! ..   !   !    !   !!#\
                    !! ! ! !  ! !!  . ! !      !   .....!  !    !   !!#\
                    !! ! ! !  !  !  . ! !!   !!    !!!!.!  !    !   !!#\
                    !! ! ! !! !  !  ..!  !!!   !!!!    .!  !    !!  !!#\
                    !! ! !  ! !  !   .!    !!        ! .!   !    !  !!#\
                    !  !!!  ! !  !!  . !    !        ! .!   !    !   !#\
                    !  !!!  ! !   !  ..!     !       ! . !  !    !   !#\
                    !  ! !  ! !   !   .!     !       ! . !   !   !   !#\
                    !  ! !  ! !!  !   .!!!   !      !! . !     ! !   !#\
                    !  !    !  !  !   ...!!!!       !  . !     ! ! ! !#\
                    !  !    !  !  !!!!!!.... !!!!!!!   .         ! ! !#\
                    !  ! !! !  !! !  !!!!  .!          .             !#\
                    !  !!!! !   !!!! !!!   .!   !!!!!  .!!!!!!!!!!!  !#\
                    !    !! !    !!!  !!  !.!!..........             !#\
                    !!!   !  !   !!!      !. !.       !!!   !   !    !#\
                    !!!      !  !!!!      !. !.       !             !!#\
                    !!!!!!!!!!!!!!!!!!!!!!!. !.!!!!!!!!!!!!!!!!!!!! !!#\
                    !!!  !                 . !.....................!!!#\
                    !!   !   !!!  !!  O..... !                    ..!!#\
                    !!   !  !     !!  !!!!!! !  !!!!!!             .!!#\
                    !!   !  !    !!   !!!!!! ! !!!!!!!!          ! .!!#\
                    !!  !   !   !!   !!!!!!! ! !!!!!! !!   !  !    .!!#\
                    !!  !   !   !   !!!!!!!! ! !!!  !  !   !       .!!#\
                    !!  !   !   !  !!!!!!!!! ! !!!! !   !  !  !  ! .!!#\
                    !!  !   !   !            !  !!  !   !  !     ! .!!#\
                    !! !!!  !   !   !!!!!!   !      !    !!        .!!#\
                    !! ! !   !  !   !     !!  !    !!!    !    !   .!!#\
                    !! ! !   !  !   ! !        !   ! !!    !     ! .!!#\
                    !! ! !   !  !   ! !!  !!    !!!   !!   !   ! ! .!!#\
                    !! ! !   !  !!  ! !!! !           !!   !      !.!!#\
                    !! ! !   !   !  ! !!!!!!!         !    !       .!!#\
                    !  ! !   !   !  !  !!!  !    !!!!     !  !     . !#\
                    !  ! !   !   !! !       !    !      !!         . !#\
                    ! !! !   !    !  !!!!!!!!!   !    !!   !   !!!!. !#\
                    ! !  !   !    !!       !!!   !!!!!        !    . !#\
                    ! !  !    !!   !!!     !!!   !         !!!!    . !#\
                    ! !  !     !!     !!!!      !!!!!!!!!!!!      !..+#\
                    ! !         !         !!!!!!                     !#\
                    !           !              !                     !#\
                    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#\
                    ";

    match str_to_maze(&maze_str) {
        Ok(x) => return x,
        Err(i) => std::process::exit(i),
    }
}

더 큰 미로를 테스트하려면 단순히 maze_1함수 에서 미로 문자열을 교체하십시오 . #\각 줄에 올바른 문자 를 추가하십시오 .

출품작 테스트

이 스크립트는 항목을 테스트하는 데 사용할 수 있습니다

#!/bin/bash

mkfifo /tmp/pipe1
mkfifo /tmp/pipe2

for arg in "$@"; do
    <path to controller> $arg < /tmp/pipe1 | tee /tmp/pipe2 trascript.txt &
    ( <path to entry> < /tmp/pipe2 | tee /tmp/pipe1 ) > /dev/null
done

rm /tmp/pipe1
rm /tmp/pipe2

예를 들어 내 스크립트는 다음과 같습니다.

#!/bin/bash

mkfifo /tmp/pipe1
mkfifo /tmp/pipe2

for arg in "$@"; do
    ./maze/target/release/maze $arg < /tmp/pipe1 | tee /tmp/pipe2 trascript.txt &
    ( ./maze_test/main < /tmp/pipe2 | tee /tmp/pipe1 ) > /dev/null
done

rm /tmp/pipe1
rm /tmp/pipe2

다음과 같은 방식으로 사용됩니다.

./script <mazes to test>

예를 들어

./script 1 2 3 4 5 6

모든 것을 콘솔에 인쇄하고 모든 것을 파일에 기록합니다. transcript.txt

출품작을 개발할 목적으로

display_maze(&maze, position)

play함수의 라인 . 그러면 컨트롤러가 각 단계에서 미로를 표시합니다.


8
벽은 마우스 트랩으로 만들어졌습니다. 알았다.
mbomb007

2
@JuliePelletier 마감일이 필요 없다고 생각합니다. 독립적으로 프로그램을 실행할 수 있습니다. 여기에있는 대부분의 컨테스트는 공개 종료되었습니다.
flawr

1
@Liam 테스트 케이스를 최적화하는 것은 표준 허점으로 간주되어 어쨌든 허용되지 않으므로 테스트 배터리를 보류하는 데 아무런 의미가 없습니다.
flawr

1
@JuliePelletier 그래, 나는 이것을 가능한 한 쉽게 만들려고 노력할 것이다
Liam

1
@JuliePelletier 완료되었습니다.
Liam

답변:


4

경계 찾기 봇, Java 1.5+, 124 + 37 + 206 + 324 + 248 + 223 = 1172 단계

여기에 이미지 설명을 입력하십시오

이 봇은 치즈가 항상 경계에 위치한다는 것을 알고 미로의 경계를 찾아 따르려고 시도합니다.

미로의 내부 모습을 업데이트하고 북쪽, 남쪽, 동쪽 및 서쪽 경계 벽에 대한 현재 후보를 구성하여이를 수행합니다.

이 벽에서 탐색되지 않은 셀에 대해 A * 경로 찾기가 수행되고 가장 짧은 경로가 선택됩니다. 그러나 빈 공간을 포함하지 않는 후보 경계 벽만 "진정한"경계 벽이 될 수 있으므로 빈 셀을 포함하는 벽은 경로 찾기에 고려되지 않습니다. 경로 내의 미 탐색 세포는 바람직하지 않은 연속성을 갖는 연속적인 미 탐색 세포와 함께 덜 바람직한 점수를 받는다.

이 최신 편집에서 봇은 이제 이미 방문한 셀에 대해 음의 가중치를 갖습니다. 약간 개선되었습니다.

경로를 찾을 수없는 경우에 유효한 이동이 선택되도록하는 안전한 안전 이동 선택 메커니즘이 있습니다.

이 구현은 매우 비효율적이며 최악의 경우 미로를 해결하는 데 몇 초가 걸릴 수 있습니다.

결정 론적. 로 실행java BoundryFindingBot

import java.io.IOException;
import java.io.InputStream;

public class BoundryFindingBot {
    private static final char[] DIRECTION = {'n','e','s','w'};
    private static final int MAP_SIZE = 102;
    private static final int PATH_FINDING_MAX_STEPS = 50000;
    private static final int[][] offsets = new int[][]{{0,-1},{1,0},{0,1},{-1,0}};

    public static void main(String[] args) throws Exception
    {
        char[][] map = new char[MAP_SIZE][MAP_SIZE];
        int[][] visitCount = new int[MAP_SIZE][MAP_SIZE];
        int mx=MAP_SIZE/2-1, my=MAP_SIZE/2-1;
        int direction=0;

        String line=readLine(System.in);
        out:
        while (line!=null && !"0000".equals(line))
        {
            // update map with new information
            for (int i=0;i<4;i++)
            {
                map[mx+offsets[i][0]][my+offsets[i][1]] = line.charAt(i);

                // immediately move toward cheese if found.
                if (line.charAt(i) == '+')
                {
                    System.out.println(DIRECTION[i]);
                    break out;
                }
            }

            // determine the current boundary walls information
            int currentNorthWallY=-1,currentSouthWallY=-1,currentWestWallX=-1,currentEastWallX=-1;
            boolean currentNorthWallHasBlanks=false,currentSouthWallHasBlanks=false,currentEastWallHasBlanks=false,currentWestWallHasBlanks=false;
            for (int y=0;y<MAP_SIZE;y++)
            {
                for (int x=0;x<MAP_SIZE;x++)
                {
                    if (map[x][y]!=0)
                    {
                        if (currentNorthWallY > -1)
                        {
                            if (currentSouthWallY !=y)
                            {
                                currentSouthWallHasBlanks = false;
                            }
                            currentSouthWallY=y;
                            currentSouthWallHasBlanks|=map[x][y]==' ';
                        }
                        else
                        {
                            currentNorthWallY=y;
                        }

                        if (currentNorthWallY == y)
                        {
                            currentNorthWallHasBlanks|=map[x][y]==' ';
                        }

                    }
                }
            }
            for (int x=0;x<MAP_SIZE;x++)
            {
                for (int y=0;y<MAP_SIZE;y++)
                {
                    if (map[x][y]!=0)
                    {

                        if (currentWestWallX > -1)
                        {
                            if (currentEastWallX !=x)
                            {
                                currentEastWallHasBlanks = false;
                            }
                            currentEastWallX=x;
                            currentEastWallHasBlanks|=map[x][y]==' ';
                        }
                        else
                        {
                            currentWestWallX=x;
                        }

                        if (currentWestWallX == x)
                        {
                            currentWestWallHasBlanks|=map[x][y]==' ';
                        }

                    }
                }
            }

            int closestUnvisitedWallCellResult =0xFFFFFF; 

            // attempt to find paths to undiscovered cells in the current north wall, setting the shortest path if shortest of any path
            if (!currentNorthWallHasBlanks)
            {
                for (int x=currentWestWallX;x<=currentEastWallX;x++)
                {
                    if (map[x][currentNorthWallY] == 0)
                    {
                        int result = pathFind(mx, my, x, currentNorthWallY, map,visitCount,currentWestWallX,currentNorthWallY,currentEastWallX,currentSouthWallY);
                        if ((result &0xFFFFFF) <(closestUnvisitedWallCellResult &0xFFFFFF))
                        {
                            closestUnvisitedWallCellResult = result;
                        }
                    }

                }
            }

            // attempt to find paths to undiscovered cells in the current south wall, setting the shortest path if shortest of any path
            if (!currentSouthWallHasBlanks)
            {
                for (int x=currentWestWallX;x<=currentEastWallX;x++)
                {
                    if (map[x][currentSouthWallY] == 0)
                    {
                        int result = pathFind(mx, my, x, currentSouthWallY, map,visitCount,currentWestWallX,currentNorthWallY,currentEastWallX,currentSouthWallY);
                        if ((result &0xFFFFFF) <(closestUnvisitedWallCellResult &0xFFFFFF))
                        {
                            closestUnvisitedWallCellResult = result;
                        }
                    }

                }
            }

            // attempt to find paths to undiscovered cells in the current east wall, setting the shortest path if shortest of any path
            if (!currentEastWallHasBlanks)
            {
                for (int y=currentNorthWallY;y<=currentSouthWallY;y++)
                {
                    if (map[currentEastWallX][y] == 0)
                    {
                        int result = pathFind(mx, my, currentEastWallX, y, map,visitCount,currentWestWallX,currentNorthWallY,currentEastWallX,currentSouthWallY);
                        if ((result &0xFFFFFF) <(closestUnvisitedWallCellResult &0xFFFFFF))
                        {
                            closestUnvisitedWallCellResult = result;
                        }
                    }

                }
            }

            // attempt to find paths to undiscovered cells in the current west wall, setting the shortest path if shortest of any path
            if (!currentWestWallHasBlanks)
            {
                for (int y=currentNorthWallY;y<=currentSouthWallY;y++)
                {
                    if (map[currentWestWallX][y] == 0 )
                    {
                        int result = pathFind(mx, my, currentWestWallX, y, map,visitCount,currentWestWallX,currentNorthWallY,currentEastWallX,currentSouthWallY);
                        if ((result &0xFFFFFF) <(closestUnvisitedWallCellResult &0xFFFFFF))
                        {
                            closestUnvisitedWallCellResult = result;
                        }
                    }
                }
            }

            // fail-safe if we are unable to find a path to a wall (i.e. initial game frame or all current boundary walls are known to have blanks and thus
            // not wall to head for. Simply tries to go north if possible or failing that try to head east, south, west consecutively
            if (closestUnvisitedWallCellResult == 0xFFFFFF)
            {
                for (int i=0;i<4;i++)
                {
                    if (map[mx+offsets[i][0]][my+offsets[i][1]] == ' ')
                    {
                        direction = i;
                        break;
                    }
                }
            }
            else
            {
                direction = closestUnvisitedWallCellResult >> 24;
            }

            mx +=offsets[direction][0];
            my +=offsets[direction][1];
            visitCount[mx][my]+=5;

            System.out.println(DIRECTION[direction]);

//// uncomment to bot's view of maze solving
//          System.err.println();
//          for (int y=currentNorthWallY;y<=currentSouthWallY;y++)
//          {
//              for (int x=currentWestWallX;x<=currentEastWallX;x++)
//              {
//                  if (x==mx && y==my)
//                  {
//                      System.err.print("O");
//                  }
//                  else
//                  {
//                      System.err.print(map[x][y]);
//                  }
//              }
//              System.err.println();
//          }
            line=readLine(System.in);
        }
        System.err.println("Exited");
    }

/**
 * returns a result that is the combination of movement direction and path length of a path found from the given start position to the target
 * position for cells within the given bounding box. Only empty cells and unexplored cells are traversable. Sequential cells of unexplored cells 
 * are given increasing magnitude negative score to reduce desirability.
 */
static int pathFind(int startX, int startY, int targetX,int targetY,char[][] map,int[][] visitCount,int boundMinX,int boundMinY,int boundMaxX,int boundMaxY)
{
    // A*
    if (!(startX==targetX && startY==targetY))
    {

        int[] tileX = new int[PATH_FINDING_MAX_STEPS];
        int[] tileY = new int[PATH_FINDING_MAX_STEPS];
         int[] fscore = new int[PATH_FINDING_MAX_STEPS];
         int[] gscore = new int[PATH_FINDING_MAX_STEPS];
         int[] openList = new int[PATH_FINDING_MAX_STEPS];
         int[] tileParent = new int[PATH_FINDING_MAX_STEPS];
         int[] unexploredCellRun = new int[PATH_FINDING_MAX_STEPS];
         int[][] tileIsClosed = new int[MAP_SIZE][MAP_SIZE];
         int currentIndex = -1;     

        int openListSize=1;
        int tileId=1;

        tileX[0]=targetX;
        tileY[0]=targetY;
        fscore[0]=1;
        gscore[0]=1;



        do
        {
          int currentBestIndex=-1;
          int currentBestScore=Integer.MAX_VALUE;
          //  Look for the lowest F cost square on the open list
          for (int ii=0;ii<openListSize;ii++)
          {
            if (fscore[openList[ii]]<currentBestScore)
            {
              currentBestScore=fscore[openList[ii]];
              currentBestIndex=ii;
            }
          }
          if (currentBestIndex==-1)
          {
            break;
          }
          currentIndex=openList[currentBestIndex];
          int currentTileX=tileX[currentIndex];
          int currentTileY=tileY[currentIndex];

          // found path
          if (startX==currentTileX && startY==currentTileY)
          {
            break;
          }

          // if not in closed list
          if (tileIsClosed[currentTileX][currentTileY]==0)
          {
                // Switch it to the closed list.
                tileIsClosed[currentTileX][currentTileY]=1;
                // remove from openlist
                openList[currentBestIndex]=openList[--openListSize];   

                // add neigbours to the open list if necessary
                for (int i=0;i<4;i++)
                {

                        int surroundingCurrentTileX=currentTileX+offsets[i][0];
                        int surroundingCurrentTileY=currentTileY+offsets[i][1];
                        if (surroundingCurrentTileX>=boundMinX-1 && surroundingCurrentTileX<=boundMaxX+1 &&
                            surroundingCurrentTileY>=boundMinY-1 && surroundingCurrentTileY<=boundMaxY+1 )
                        {
                          tileX[tileId]=surroundingCurrentTileX;
                          tileY[tileId]=surroundingCurrentTileY;
                          if (map[surroundingCurrentTileX][surroundingCurrentTileY]==0)
                          {
                              unexploredCellRun[tileId]=0;
                          }
                          else if (map[surroundingCurrentTileX][surroundingCurrentTileY]=='!')
                          {
                              continue;
                          }
                          else
                          {
                              unexploredCellRun[tileId]=unexploredCellRun[currentIndex]+1;
                          }
                          int surroundingCurrentGscore=gscore[currentIndex]+visitCount[surroundingCurrentTileX][surroundingCurrentTileY]+1+((int) (unexploredCellRun[tileId]*10));
                          gscore[tileId]=surroundingCurrentGscore;
                          fscore[tileId]=surroundingCurrentGscore+Math.abs( surroundingCurrentTileX-startX)+Math.abs( surroundingCurrentTileY-startY);
                          tileParent[tileId]=currentIndex;
                          openList[openListSize++]=tileId++;
                     }
                }
          }
          else
          {
          // remove from openlist
          openList[currentBestIndex]=openList[--openListSize];    
          }
        } while(true);

        if (tileX[tileParent[currentIndex]]-startX<0) return (3 <<24) + currentIndex;
        else if (tileX[tileParent[currentIndex]]-startX>0) return (1 <<24) + currentIndex;
        else if (tileY[tileParent[currentIndex]]-startY<0) return (0 <<24) + currentIndex;
        else if (tileY[tileParent[currentIndex]]-startY>0) return (2 <<24) + currentIndex;
    }
    throw new RuntimeException("Path finding failed");
 }

    /**
     * Reads a line of text from the input stream. Blocks until a new line character is read.
     * NOTE: This method should be used in favor of BufferedReader.readLine(...) as BufferedReader buffers data before performing
     * text line tokenization. This means that BufferedReader.readLine() will block until many game frames have been received. 
     * @param in a InputStream, nominally System.in
     * @return a line of text or null if end of stream.
     * @throws IOException
     */
    private static String readLine(InputStream in) throws IOException
    {
       StringBuilder sb = new StringBuilder();
       int readByte = in.read();
       while (readByte>-1 && readByte!= '\n')
       {
          sb.append((char) readByte);
          readByte = in.read();
       }
       return readByte==-1?null:sb.toString();
    }

}

3

파이썬, 132 + 23 + 228 + 218 + 764 + 213 = 1578 단계

이것은 치즈가 보일 때까지 알려진 빈 공간과 알려지지 않은 공간을 통해 알려진 세계의 경계 사각형으로가는 최단 경로를 따릅니다.

결정 론적. python SCRIPT또는 python3 SCRIPT(2.7 및 3.5에서 테스트)로 실행하십시오 .

import collections, sys

def neighbors(p):
    x, y = p
    return [("n", (x, y + 1)), ("e", (x + 1, y)), ("s", (x, y - 1)), ("w", (x - 1, y))]

ne = sw = loc = 0, 0
maze = {loc: ' '}

while True:
    cells = sys.stdin.readline()
    if cells == '0000\n':
        break
    for (dir1, loc1), cell in zip(neighbors(loc), cells):
        maze[loc1] = cell
        if cell == ' ':
            ne = tuple(map(max, loc1, ne))
            sw = tuple(map(min, loc1, sw))
    visited = {loc}
    queue = collections.deque()
    for dir1, loc1 in neighbors(loc):
        visited.add(loc1)
        queue.append((dir1, loc1, loc1))
    while True:
        dir1, loc1, loc2 = queue.popleft()
        if maze.get(loc2, ' ') == ' ':
            if loc2 not in maze and \
               (any(a >= b for a, b in zip(loc2, ne)) or
                any(a <= b for a, b in zip(loc2, sw))):
                break
            for dir3, loc3 in neighbors(loc2):
                if loc3 not in visited:
                    visited.add(loc3)
                    queue.append((dir1, loc1, loc3))
        elif maze[loc2] == '+':
            break
    sys.stdout.write(dir1 + "\n")
    sys.stdout.flush()
    loc = loc1

2

MATLAB, 210 + 23 + 394 + 270 + 1272 + 707 = 2876 단계

이 접근법은 다른 MATLAB 제출을 수정 한 것입니다 . (그러나 그들은 똑같은 컨트롤러를 사용합니다.)

이 방법에서 마우스는 막 다른 길을 찾을 때까지 가능한 경로를 따릅니다. 그런 다음 아직 탐색하지 않은 경로가 있던 이전 교차로로 돌아갑니다. 그러나 모든 단계에서 마우스는 미 탐지 영역이 있는지 여부를 확인합니다. 이것들 안에서, 치즈는 분명히 국경에 있기 때문에 분명히 될 수 없습니다. 이러한 영역이 발견되면 이후에는 무시됩니다.

결정 론적입니다. 사용 가능한 경로에서 항상 순서대로 선택합니다 NESW.

matlab 스크립트를 컴파일 할 수 없으므로 컨트롤러를 MATLAB으로 변환했습니다. "프로그램"은 이제 단계 간 저장을 위해 전역 변수에 액세스하는 기능 일뿐입니다.

function find_the_cheese_controller()
clc;clear;
    global State;
    clearvars -global State;
    %uncomment the maze you want to test
    %maze=['!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!';'!O !                    !      !';'!. ! !!!!!!!!!!!!!!!!!! ! !!!!!!';'!. !                    ! !    !';'!. ! !!!!!!!!!!!!!!!!!!!! ! !! !';'!. !...........           ! !!.+';'!. !.!!!!!!!!!.!!!!!!!!!!!!!!!.!';'!.!..!        ...............!.!';'!.!.!! !!!!!!!!!!!!!!!!!!!!!.!.!';'!.!.!! !!!  !               .!.!';'!.!.!! !!!  !!!!!!!!!!!!!!!!.!.!';'!...!! !!!                  .!.!';'! ! !! !!!                  .!.!';'! ! !! !!!  !!!!!!!!! !!!!!!.!.!';'! ! !! !!!  !      !! !     .!.!';'! ! !! !!!  ! !!!!!!! !     .!.!';'! ! !! !!!  !      !! !     .!.!';'! ! !! !!!  !      !! !     .!.!';'! ! !!   !  !      !! !     .!.!';'! ! !! ! !  !!!!!! !! !     .!.!';'! ! !! ! !  !      !! !     ...!';'! ! !! ! !  !      !! !        !';'! ! !! ! !  !      !! !      ! !';'! ! !! ! !  !  !!!!!! !      ! !';'! ! !! ! !  !      !! !      ! !';'! !    !    !      !! !      ! !';'! !!!!!!  !!!!!!!! !! !      ! !';'!                ! !! !      ! !';'! !!!!!!!!!!! !!!! !! !      ! !';'!                     !      ! !';'! !!!!!!!! !!!!       !        !';'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'];
    %maze=['!!!!!!!!!!!!!!!';'!      .......!';'! !!! !.!!!! .!';'!   ! !.!!O!!.!';'!!!   !....! .!';'!   !!!!!!!!!.!';'! !!        ..!';'!  !!!!!!!!!.!!';'!           ..+';'!!!!!!!!!!!!!!!'];
    %maze=['!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!';'!                            !!!';'! !  !!!  !!!!!!!!!!!!!!!!!!   !';'! !  ! !!!              !!!  ! !';'! !  ! !!!!!!!!!!!!!!!!     !! !';'! !  !                  !!!!   !';'! !! !!!!!!!!!!        !!    ! !';'!  ! !        !!!! !!!!!   !!! !';'!! ! ! !!!!      !         !   !';'!! ! ! !!!!   !!!!!!!!!!!!!! ! !';'!! ! ! !!!! ! !              ! !';'!! ! ! !!!! ! ! !!!!       ! ! !';'!! ! ! !!!! ! ! !   !!!    ! ! !';'!  ! ! !!!! ! ! !     !!!  ! ! !';'!  ! ! !!!! ! ! !!!!!      !   !';'!  ! ! !!!! !!! !   !!     ! !!!';'!  ! !  !!!  !! !    !!!   ! !!!';'!  ! !     ! !! !!!!   !!  ! !!!';'!  ! !!    ! !! !  !!      ! !!!';'!  !  !   !! !!     !!!    ! !!!';'!  !! !!!!     !!!    !!   !   !';'!!  ! !! !       !!!   !!  !!! !';'!!  !    !    !    !           !';'!!  !!!!!!    !!   !!!!!!!!!!! !';'!             !!!! !!!!!!!!!!! !';'!  ..........O!!!! !!!!!!!!!!!.+';'!! .!!!!!!    !!   !!!!!!!!!!!.!';'!! .!    !    !    !          .!';'!!..! !! !       !!!   !!  !!!.!';'! .!! !!!!     !!!    !!   !...!';'! .!  !   !! !!     !!!    !.!!!';'! .! !!    ! !! !  !!      !.!!!';'! .! !     ! !! !!!!   !!  !.!!!';'! .! !  !!!  !! !    !!!   !.!!!';'! .! ! !!!! !!! !   !!     !.!!!';'! .! ! !!!! ! ! !!!!!      !...!';'! .! ! !!!! ! ! !     !!!  ! !.!';'!!.! ! !!!! ! ! !   !!!    ! !.!';'!!.! ! !!!! ! ! !!!!       ! !.!';'!!.! ! !!!! ! !              !.!';'!!.! ! !!!!   !!!!!!!!!!!!!! !.!';'!!.! ! !!!!      !         !  .!';'!..! !        !!!! !!!!!   !!!.!';'!.!! !!!!!!!!!!        !!    !.!';'!.!  !                  !!!!  .!';'!.!  ! !!!!!!!!!!!!!!!!     !!.!';'!.!  ! !!!              !!!  !.!';'!.!  !!!  !!!!!!!!!!!!!!!!!!...!';'!............................!!!';'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'];
    %maze=['!!!!!!!!!!!!!!!!!+!!!!!!!!!!!!!!';'!.................           !!!';'!.!  !!!  !!!!!!!!!!!!!!!!!!   !';'!.!  ! !!!              !!!  ! !';'!.!  ! !!!!!!!!!!!!!!!!     !! !';'!.!  !                  !!!!   !';'!.!! !!!!!!!!!!        !!    ! !';'!..! !        !!!! !!!!!   !!! !';'!!.! ! !!!!      !         !   !';'!!.! ! !!!!   !!!!!!!!!!!!!! ! !';'!!.! ! !!!! ! !              ! !';'!!.! ! !!!! ! ! !!!!       ! ! !';'!!.! ! !!!! ! ! !   !!!    ! ! !';'! .! ! !!!! ! ! !     !!!  ! ! !';'! .! ! !!!! ! ! !!!!!      !   !';'! .! ! !!!! !!! !   !!     ! !!!';'! .! !  !!!  !! !    !!!   ! !!!';'! .! !     ! !! !!!!   !!  ! !!!';'! .! !!    ! !! !  !!      ! !!!';'! .!  !   !! !!     !!!    ! !!!';'! .!! !!!!     !!!    !!   !   !';'!!. ! !! !       !!!   !!  !!! !';'!!. !    !    !    !           !';'!!. !!!!!!    !!   !!!!!!!!!!! !';'! ........... !!!! !!!!!!!!!!! !';'!           . !!!! !!!!!!!!!!! !';'!!  !!!!!!  . !!   !!!!!!!!!!! !';'!!  !    !  . !    !           !';'!!  ! !! !  .    !!!   !!  !!! !';'!  !! !!!!  .  !!!    !!   !   !';'!  !  !   !!.!!     !!!    ! !!!';'!  ! !!    !.!! !  !!      ! !!!';'!  ! !     !.!! !!!!   !!  ! !!!';'!  ! !  !!!..!! !    !!!   ! !!!';'!  ! ! !!!!.!!! !   !!     ! !!!';'!  ! ! !!!!.! ! !!!!!      !   !';'!  ! ! !!!!.! ! !     !!!  ! ! !';'!! ! ! !!!!.! ! !   !!!    ! ! !';'!! ! ! !!!!.! ! !!!!       ! ! !';'!! ! ! !!!!.! !              ! !';'!! ! ! !!!!.  !!!!!!!!!!!!!! ! !';'!! ! ! !!!!.....O!         !   !';'!  ! !        !!!! !!!!!   !!! !';'! !! !!!!!!!!!!        !!    ! !';'! !  !                  !!!!   !';'! !  ! !!!!!!!!!!!!!!!!     !! !';'! !  ! !!!              !!!  ! !';'! !  !!!  !!!!!!!!!!!!!!!!!!   !';'!                            !!!';'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'];
    %maze=['!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!';'+......!!!!       !!!        !!!       !!!!     !!';'!     .!       !!                            !!!!!';'!  !!!.! !!      !!!  !!!!  !!!!!!!!!      !!!   !';'! !!...!   !!!!!   !!    !          !!    !!     !';'!!!..!!         !    !!  !           !    !     !!';'!! .!!........        !! !!!     !    !   !  !  !!';'!!!. !.  !  !.   !      !!!!!    !!   !   ! !! !!!';'!!!. !.  !  !.   !       !!!!!    !   !!    !  !!!';'!!.. !.  !  !..  !        !  !    !!   !    !   !!';'!!.! !.!  ! ! ..  ! !!!!!!  !      !   !    !   !!';'!!.! !.!  ! !! .  ! !      !        !  !    !   !!';'!!.! !.!  !  ! .  ! !!   !!    !!!! !  !    !   !!';'!!.! !.!! !  ! .  !  !!!   !!!!     !  !    !!  !!';'!!.! !. ! !  ! .  !    !!        !  !   !    !  !!';'! .!!!. ! !  !!.   !    !        !  !   !    !   !';'! .!!!. ! !   !.   !     !       !   !  !    !   !';'! .! !. ! !   !.   !     !       !   !   !   !   !';'! .! !. ! !!  !....!!!   !      !!   !     ! !   !';'! .!  ..!  !  !   ...!!!!       !    !     ! ! ! !';'! .!   .!  !  !!!!!!.... !!!!!!!             ! ! !';'! .! !!.!  !! !  !!!!  .!                        !';'! .!!!!.!   !!!! !!!   .!   !!!!!   !!!!!!!!!!!  !';'! .. !!.!    !!!  !!  !.!!                       !';'!!!.. !. !   !!!      !..!        !!!   !   !    !';'!!! .... !  !!!!      ! .!        !             !!';'!!!!!!!!!!!!!!!!!!!!!!! .! !!!!!!!!!!!!!!!!!!!! !!';'!!!  !                  .!                     !!!';'!!   !   !!!  !!        .!                      !!';'!!   !  !     !!  !!!!!!.!  !!!!!!              !!';'!!   !  !    !!   !!!!!!.! !!!!!!!!          !  !!';'!!  !   !   !!   !!!!!!!.! !!!!!! !!   !  !     !!';'!!  !   !   !   !!!!!!!!.! !!!  !  !   !        !!';'!!  !   !   !  !!!!!!!!!.! !!!! !   !  !  !  !  !!';'!!  !   !   !           .!  !!  !   !  !     !  !!';'!! !!!  !   !   !!!!!!  .!      !    !!         !!';'!! ! !   !  !   !     !!. !    !!!    !    !    !!';'!! ! !   !  !   ! !     .  !   ! !!    !     !  !!';'!! ! !   !  !   ! !!  !!.   !!!   !!   !   ! !  !!';'!! ! !   !  !!  ! !!! ! .....     !!   !      ! !!';'!! ! !   !   !  ! !!!!!!!   .     !    !        !!';'!  ! !   !   !  !  !!!  !   .!!!!     !  !       !';'!  ! !   !   !! !       !   .!      !!.......... !';'! !! !   !    !  !!!!!!!!!  .!    !!  .!   !!!!. !';'! !  !   !    !!       !!!  .!!!!!   ..   !    . !';'! !  !    !!   !!!     !!!  .......... !!!!    . !';'! !  !     !!     !!!!      !!!!!!!!!!!!      !. !';'! !         !         !!!!!!                  O. !';'!           !                                    !';'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'];
    %maze=['!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!';'!      !!!!   ....!!!        !!!       !!!!     !!';'!      !      .!!..........                  !!!!!';'!  !!! ! !!   ...!!!  !!!!. !!!!!!!!!      !!!   !';'! !!   !   !!!!!.. !!    !.         !!    !!     !';'!!!  !!         !.   !!  !.....      !    !     !!';'!!  !!          ..    !! !!!  .  !    !   !  !  !!';'!!!  !   !  !   .!      !!!!! .  !!   !   ! !! !!!';'!!!  !   !  !   .!       !!!!!.   !   !!    !  !!!';'!!   !   !  !   .!        !  !.   !!   !    !   !!';'!! ! ! !  ! !   . ! !!!!!!  ! ..   !   !    !   !!';'!! ! ! !  ! !!  . ! !      !   .....!  !    !   !!';'!! ! ! !  !  !  . ! !!   !!    !!!!.!  !    !   !!';'!! ! ! !! !  !  ..!  !!!   !!!!    .!  !    !!  !!';'!! ! !  ! !  !   .!    !!        ! .!   !    !  !!';'!  !!!  ! !  !!  . !    !        ! .!   !    !   !';'!  !!!  ! !   !  ..!     !       ! . !  !    !   !';'!  ! !  ! !   !   .!     !       ! . !   !   !   !';'!  ! !  ! !!  !   .!!!   !      !! . !     ! !   !';'!  !    !  !  !   ...!!!!       !  . !     ! ! ! !';'!  !    !  !  !!!!!!.... !!!!!!!   .         ! ! !';'!  ! !! !  !! !  !!!!  .!          .             !';'!  !!!! !   !!!! !!!   .!   !!!!!  .!!!!!!!!!!!  !';'!    !! !    !!!  !!  !.!!..........             !';'!!!   !  !   !!!      !. !.       !!!   !   !    !';'!!!      !  !!!!      !. !.       !             !!';'!!!!!!!!!!!!!!!!!!!!!!!. !.!!!!!!!!!!!!!!!!!!!! !!';'!!!  !                 . !.....................!!!';'!!   !   !!!  !!  O..... !                    ..!!';'!!   !  !     !!  !!!!!! !  !!!!!!             .!!';'!!   !  !    !!   !!!!!! ! !!!!!!!!          ! .!!';'!!  !   !   !!   !!!!!!! ! !!!!!! !!   !  !    .!!';'!!  !   !   !   !!!!!!!! ! !!!  !  !   !       .!!';'!!  !   !   !  !!!!!!!!! ! !!!! !   !  !  !  ! .!!';'!!  !   !   !            !  !!  !   !  !     ! .!!';'!! !!!  !   !   !!!!!!   !      !    !!        .!!';'!! ! !   !  !   !     !!  !    !!!    !    !   .!!';'!! ! !   !  !   ! !        !   ! !!    !     ! .!!';'!! ! !   !  !   ! !!  !!    !!!   !!   !   ! ! .!!';'!! ! !   !  !!  ! !!! !           !!   !      !.!!';'!! ! !   !   !  ! !!!!!!!         !    !       .!!';'!  ! !   !   !  !  !!!  !    !!!!     !  !     . !';'!  ! !   !   !! !       !    !      !!         . !';'! !! !   !    !  !!!!!!!!!   !    !!   !   !!!!. !';'! !  !   !    !!       !!!   !!!!!        !    . !';'! !  !    !!   !!!     !!!   !         !!!!    . !';'! !  !     !!     !!!!      !!!!!!!!!!!!      !..+';'! !         !         !!!!!!                     !';'!           !              !                     !';'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!']



    %replace dot with space
    maze(maze=='.')=' ';
    %position of mouse
    [u,v]=find(maze=='O'); 
    maze(maze=='O') = ' ';
    step_counter = 0;
    while true; %game loop
        %test if mouse found cheese
        if maze(u,v) == '+';
            disp(['mouse found cheese after ', num2str(step_counter), ' steps']);
            break;
        end

        %extract NESW tiles
        nesw = [maze(u-1,v),maze(u,v+1),maze(u+1,v),maze(u,v-1)];

        %get result and move accordingly
        answer = find_the_cheese(nesw);
        switch answer;
            case 'n';
                u = u-1;
            case 'e';
                v = v+1;
            case 's';
                u = u+1;
            otherwise;
                v = v-1;
        end

        %make sure, mouse did not run into wall
        assert(maze(u,v) ~= '!','mouse ran into wall!');
        step_counter = step_counter + 1;
    end
end


function step = find_the_cheese(nesw)
    global State;
    NESW = 'nesw';
    NESW_REVERSE = 'swne';

    if all(nesw == '0000');
        return;
    elseif ~isfield(State,'maze');
        State = struct('maze', zeros(140)+' ','u',75,'v',75,'state','E');
        State.maze(State.u,State.v) = 'S';
    end    
    if State.maze(State.u-1,State.v) == ' '
        State.maze(State.u-1,State.v) = nesw(1);
    end
    if State.maze(State.u,State.v+1) == ' '
        State.maze(State.u,State.v+1) = nesw(2);
    end
    if State.maze(State.u+1,State.v) == ' '
        State.maze(State.u+1,State.v) = nesw(3);
    end
    if State.maze(State.u,State.v-1) == ' '
        State.maze(State.u,State.v-1) = nesw(4);
    end

    current_nesw = [State.maze(State.u-1,State.v),State.maze(State.u,State.v+1),State.maze(State.u+1,State.v),State.maze(State.u,State.v-1)];

    if any(current_nesw == '+'); % if there is cheese, go there
        nesw_index = find(current_nesw == '+',1);

    elseif any(current_nesw == ' '); % if there is a path that we did not walk, go there
        nesw_index = find(current_nesw == ' ',1);
        State.state = 'E';

    else % return to previous crossing
        nesw_index = find(NESW == State.maze(State.u,State.v),1);
        assert(numel(nesw_index)>0,'not enough indices')

        State.state = 'R';
    end

    %execute the step
    if State.state == 'E';
        step = NESW(nesw_index);
    else %State.state = 'R'
        step = State.maze(State.u, State.v);
    end

    switch step;
        case 'n';
            State.u = State.u-1;
        case 'e';
            State.v = State.v+1;
        case 's';
            State.u = State.u+1;
        otherwise;
            State.v = State.v-1;
    end

    if State.maze(State.u,State.v) == ' '; %if we do not have a reverse poniter yet
        State.maze(State.u,State.v) = NESW_REVERSE(nesw_index); %reverse pointer
    end

    %check whether we have any enclosed areas, where the cheese obviously cannot be
    M = imfill(State.maze ~= ' ','holes'); 
    %fill those areas with walls
    State.maze(M & State.maze == ' ') = '!';


    %disp_important(State.maze); %uncomment for display
end


function disp_important(m) %just show the important stuff of the maze
    if any(m(:) ~= ' ');
        for k=1:4
            m = rot90(m);
            while all(m(:,1) == ' ');
                m = m(:,2:end);
            end
        end
    end
    if numel(m) == 0;
        m = 'X';
    end
    m = padarray(m,[1,1],35,'both');
    disp([m,'']);
end

참고로,이 미로는 점수가 매겨진 미로가 아닙니다. "총 10 개의 미로가있을 가능성이 높으며, 각 제출물은 여러 다른 시작 위치와 치즈 위치에서 각 미로를 여러 번 시도 할 것입니다."
mbomb007

나는 알고 있지만 제목에 쓸 수있는 다른 것은 없습니다 : D 아니요 실제로, 나는이 두 가지 테스트 사례가 적어도 제출이 어떻게 수행되는지에 대해 약간의 인상을 줄 것이라고 생각했습니다 .
flawr

배터리가
Liam

감사합니다! work =)로 완료하자마자 업데이트하겠습니다.
flawr

@ 리암 나는 그들을 업데이 트했습니다!
flawr

1

파이썬 3, 156 바이트, 37692 + 715 + 50626 + 27806 + 148596 + 172675 = 438110 단계

이것은 골프는 아니지만 어쨌든 골프는 재미 있습니다. 이것은 mbomb007의 (완전히 구현되지 않은) 아이디어 와 유사 하지만 알파벳순으로 최신 방향 이름으로 이동하여 연결이 끊긴 치즈로 하거나 가장 적게 나가는 나가는 경로를 취합니다 .

결정 론적. 로 실행하십시오 python3 SCRIPT(3.5에서 테스트).

x=y=0
f={}
for l in iter(input,'0000'):c,d,x,y=p=max((f.setdefault(p,0),p)for p in zip(l,'nesw',[x,x+1,x,x-1],[y+1,y,y-1,y])if'!'!=p[0])[1];print(d);f[p]-=1

@ mbomb007 f은 공백 사이의 단계와 사용 빈도 를 추적합니다. 예를 들어, -f[' ', 'n', 3, 6]우리가 (3, 5)에서 (3, 6)으로 북쪽으로 이동 한 횟수입니다.
Anders Kaseorg

방향도 추적합니까?
mbomb007

@ mbomb007 그렇습니다.하지만 몇 바이트를 절약 할 수 있기 때문에 – 방향을 기억하면 실제로 더 많은 단계를 거치게됩니다.
Anders Kaseorg 2016 년

1

PHP 362 + 37 + 1638 + 1508 + 6696 + 1613 = 11854 단계

버그 수정 코드에 대한 벤치 마크 실행 :

<?php

class Maze {
    public $map, $pos;
    public $prevPos = FALSE;
    public $intersections = [];

    public $n = 75, $e = 75, $w = 75, $s = 75;

    public function __construct() {
        // since we don't know where we start, build a 150x150 map and position ourselves at the middle
        $this->map = array_pad([], 150, array_pad([], 150, 0)); // 0 is unknown
        $this->pos = ['x'=> 75, 'y'=> 75];
    }

    public function play($input){
        $this->updateMap($input);
        $this->move();
    }

    private function updateField($x, $y, $inData) {
        if ($inData == '!' || $this->map[$x][$y] >= 1000) {                 // avoid overwriting our observations
            $this->map[$x][$y] = 1000;
            return;
        }

        // update our known borders
        if ($x <= $this->w) {
            $this->w = $x - 1;
        }
        elseif ($x >= $this->e) {
            $this->e = $x + 1;
        }
        elseif ($y <= $this->n) {
            $this->n = $y - 1;
        }
        elseif ($y >= $this->s) {
            $this->s = $y + 1;
        }

        if (!$this->map[$x][$y]) {
            $this->map[$x][$y] = $inData == '+' ? -1 : 1;
        }
    }

    private function checkForIntersection($input) {
        if (array_key_exists('x' . $this->pos['x'] . 'y' . $this->pos['y'], $this->intersections)) {
            if ($this->intersections['x' . $this->pos['x'] . 'y' . $this->pos['y']] > 0) {
                $this->intersections['x' . $this->pos['x'] . 'y' . $this->pos['y']] --;
                return TRUE;                                        // intersection already crossed
            }
        }
        elseif ($c = substr_count($input, ' ') > 2) {
            $this->intersections['x' . $this->pos['x'] . 'y' . $this->pos['y']] = $c - 1;
        }

        return FALSE;
    }

    private function updateMap($input) {
        if ($this->checkForIntersection($input)) {
            // if we're at a known intersection, we know that the path we come from lead nowhere
            $this->updateField($this->prevPos['x'], $this->prevPos['y'], '!');
        }

        // update discovered information
        $this->updateField($this->pos['x'], $this->pos['y'] - 1, $input[0]);
        $this->updateField($this->pos['x'] + 1, $this->pos['y'], $input[1]);
        $this->updateField($this->pos['x'], $this->pos['y'] + 1, $input[2]);
        $this->updateField($this->pos['x'] - 1, $this->pos['y'], $input[3]);
    }

    private function move() {
        if ($this->prevPos) {

            $this->map[$this->prevPos['x']][$this->prevPos['y']] ++;    // count times stepped on   
        }

        $best = ['w' => 1000];

        foreach ([
            ['x' => $this->pos['x'], 'y' => $this->pos['y'] - 1, 'w' => $this->map[$this->pos['x']][$this->pos['y'] - 1], 'd' => 'n'],
            ['x' => $this->pos['x'] + 1, 'y' => $this->pos['y'], 'w' => $this->map[$this->pos['x'] + 1][$this->pos['y']], 'd' => 'e'],
            ['x' => $this->pos['x'], 'y' => $this->pos['y'] + 1, 'w' => $this->map[$this->pos['x']][$this->pos['y'] + 1], 'd' => 's'],
            ['x' => $this->pos['x'] - 1, 'y' => $this->pos['y'], 'w' => $this->map[$this->pos['x'] - 1][$this->pos['y']], 'd' => 'w']]
        as $direction) {
            if ($direction['w'] < $best['w'] || ($direction['w'] == $best['w'] && $best['w'] < 1000 && max($this->e - $direction['x'],  $direction['x'] - $this->w, $direction['y'] - $this->n, $this->s - $direction['y']) > max($best['x'] - $this->e, $this->w - $best['x'], $best['y'] - $this->n, $this->s - $best['y']))) { 
            // encourage searching for borders which will later allow to block certain dead end paths without walking them
            // testing with middle search instead
                $best = $direction;
            }
        }

        echo $best['d'] . "\n";

        $this->prevPos = $this->pos;
        $this->pos = $best;
    }
}

$maze = new Maze();

while (strncmp(($input = fgets(STDIN)), '0000', 4)) {
    $len = strlen($input);
    if (($len > 6) || $len < 3) { continue; }

    $maze->play($input);
}

1

MATLAB, 212 + 23 + 416 + 300 + 1806 + 757 = 3514 단계

이 방법에서 마우스는 막 다른 길을 찾을 때까지 가능한 경로를 따릅니다. 그런 다음 아직 탐색하지 않은 경로가 있던 이전 교차로로 돌아갑니다. 결정 론적입니다. 사용 가능한 경로에서 항상 순서대로 선택합니다 NESW( NSFW항상 =를 쓰려고 유혹했기 때문에 아닙니다 )

matlab 스크립트를 컴파일 할 수 없으므로 컨트롤러를 MATLAB으로 변환했습니다. "프로그램"은 이제 단계 간 저장을 위해 전역 변수에 액세스하는 기능 일뿐입니다.

function find_the_cheese_controller()
clc;clear;
    global State;
    clearvars -global State;
    %uncomment the maze you want to test
    %maze=['!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!';'!O !                    !      !';'!. ! !!!!!!!!!!!!!!!!!! ! !!!!!!';'!. !                    ! !    !';'!. ! !!!!!!!!!!!!!!!!!!!! ! !! !';'!. !...........           ! !!.+';'!. !.!!!!!!!!!.!!!!!!!!!!!!!!!.!';'!.!..!        ...............!.!';'!.!.!! !!!!!!!!!!!!!!!!!!!!!.!.!';'!.!.!! !!!  !               .!.!';'!.!.!! !!!  !!!!!!!!!!!!!!!!.!.!';'!...!! !!!                  .!.!';'! ! !! !!!                  .!.!';'! ! !! !!!  !!!!!!!!! !!!!!!.!.!';'! ! !! !!!  !      !! !     .!.!';'! ! !! !!!  ! !!!!!!! !     .!.!';'! ! !! !!!  !      !! !     .!.!';'! ! !! !!!  !      !! !     .!.!';'! ! !!   !  !      !! !     .!.!';'! ! !! ! !  !!!!!! !! !     .!.!';'! ! !! ! !  !      !! !     ...!';'! ! !! ! !  !      !! !        !';'! ! !! ! !  !      !! !      ! !';'! ! !! ! !  !  !!!!!! !      ! !';'! ! !! ! !  !      !! !      ! !';'! !    !    !      !! !      ! !';'! !!!!!!  !!!!!!!! !! !      ! !';'!                ! !! !      ! !';'! !!!!!!!!!!! !!!! !! !      ! !';'!                     !      ! !';'! !!!!!!!! !!!!       !        !';'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'];
    %maze=['!!!!!!!!!!!!!!!';'!      .......!';'! !!! !.!!!! .!';'!   ! !.!!O!!.!';'!!!   !....! .!';'!   !!!!!!!!!.!';'! !!        ..!';'!  !!!!!!!!!.!!';'!           ..+';'!!!!!!!!!!!!!!!'];
    %maze=['!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!';'!                            !!!';'! !  !!!  !!!!!!!!!!!!!!!!!!   !';'! !  ! !!!              !!!  ! !';'! !  ! !!!!!!!!!!!!!!!!     !! !';'! !  !                  !!!!   !';'! !! !!!!!!!!!!        !!    ! !';'!  ! !        !!!! !!!!!   !!! !';'!! ! ! !!!!      !         !   !';'!! ! ! !!!!   !!!!!!!!!!!!!! ! !';'!! ! ! !!!! ! !              ! !';'!! ! ! !!!! ! ! !!!!       ! ! !';'!! ! ! !!!! ! ! !   !!!    ! ! !';'!  ! ! !!!! ! ! !     !!!  ! ! !';'!  ! ! !!!! ! ! !!!!!      !   !';'!  ! ! !!!! !!! !   !!     ! !!!';'!  ! !  !!!  !! !    !!!   ! !!!';'!  ! !     ! !! !!!!   !!  ! !!!';'!  ! !!    ! !! !  !!      ! !!!';'!  !  !   !! !!     !!!    ! !!!';'!  !! !!!!     !!!    !!   !   !';'!!  ! !! !       !!!   !!  !!! !';'!!  !    !    !    !           !';'!!  !!!!!!    !!   !!!!!!!!!!! !';'!             !!!! !!!!!!!!!!! !';'!  ..........O!!!! !!!!!!!!!!!.+';'!! .!!!!!!    !!   !!!!!!!!!!!.!';'!! .!    !    !    !          .!';'!!..! !! !       !!!   !!  !!!.!';'! .!! !!!!     !!!    !!   !...!';'! .!  !   !! !!     !!!    !.!!!';'! .! !!    ! !! !  !!      !.!!!';'! .! !     ! !! !!!!   !!  !.!!!';'! .! !  !!!  !! !    !!!   !.!!!';'! .! ! !!!! !!! !   !!     !.!!!';'! .! ! !!!! ! ! !!!!!      !...!';'! .! ! !!!! ! ! !     !!!  ! !.!';'!!.! ! !!!! ! ! !   !!!    ! !.!';'!!.! ! !!!! ! ! !!!!       ! !.!';'!!.! ! !!!! ! !              !.!';'!!.! ! !!!!   !!!!!!!!!!!!!! !.!';'!!.! ! !!!!      !         !  .!';'!..! !        !!!! !!!!!   !!!.!';'!.!! !!!!!!!!!!        !!    !.!';'!.!  !                  !!!!  .!';'!.!  ! !!!!!!!!!!!!!!!!     !!.!';'!.!  ! !!!              !!!  !.!';'!.!  !!!  !!!!!!!!!!!!!!!!!!...!';'!............................!!!';'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'];
    %maze=['!!!!!!!!!!!!!!!!!+!!!!!!!!!!!!!!';'!.................           !!!';'!.!  !!!  !!!!!!!!!!!!!!!!!!   !';'!.!  ! !!!              !!!  ! !';'!.!  ! !!!!!!!!!!!!!!!!     !! !';'!.!  !                  !!!!   !';'!.!! !!!!!!!!!!        !!    ! !';'!..! !        !!!! !!!!!   !!! !';'!!.! ! !!!!      !         !   !';'!!.! ! !!!!   !!!!!!!!!!!!!! ! !';'!!.! ! !!!! ! !              ! !';'!!.! ! !!!! ! ! !!!!       ! ! !';'!!.! ! !!!! ! ! !   !!!    ! ! !';'! .! ! !!!! ! ! !     !!!  ! ! !';'! .! ! !!!! ! ! !!!!!      !   !';'! .! ! !!!! !!! !   !!     ! !!!';'! .! !  !!!  !! !    !!!   ! !!!';'! .! !     ! !! !!!!   !!  ! !!!';'! .! !!    ! !! !  !!      ! !!!';'! .!  !   !! !!     !!!    ! !!!';'! .!! !!!!     !!!    !!   !   !';'!!. ! !! !       !!!   !!  !!! !';'!!. !    !    !    !           !';'!!. !!!!!!    !!   !!!!!!!!!!! !';'! ........... !!!! !!!!!!!!!!! !';'!           . !!!! !!!!!!!!!!! !';'!!  !!!!!!  . !!   !!!!!!!!!!! !';'!!  !    !  . !    !           !';'!!  ! !! !  .    !!!   !!  !!! !';'!  !! !!!!  .  !!!    !!   !   !';'!  !  !   !!.!!     !!!    ! !!!';'!  ! !!    !.!! !  !!      ! !!!';'!  ! !     !.!! !!!!   !!  ! !!!';'!  ! !  !!!..!! !    !!!   ! !!!';'!  ! ! !!!!.!!! !   !!     ! !!!';'!  ! ! !!!!.! ! !!!!!      !   !';'!  ! ! !!!!.! ! !     !!!  ! ! !';'!! ! ! !!!!.! ! !   !!!    ! ! !';'!! ! ! !!!!.! ! !!!!       ! ! !';'!! ! ! !!!!.! !              ! !';'!! ! ! !!!!.  !!!!!!!!!!!!!! ! !';'!! ! ! !!!!.....O!         !   !';'!  ! !        !!!! !!!!!   !!! !';'! !! !!!!!!!!!!        !!    ! !';'! !  !                  !!!!   !';'! !  ! !!!!!!!!!!!!!!!!     !! !';'! !  ! !!!              !!!  ! !';'! !  !!!  !!!!!!!!!!!!!!!!!!   !';'!                            !!!';'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'];
    %maze=['!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!';'+......!!!!       !!!        !!!       !!!!     !!';'!     .!       !!                            !!!!!';'!  !!!.! !!      !!!  !!!!  !!!!!!!!!      !!!   !';'! !!...!   !!!!!   !!    !          !!    !!     !';'!!!..!!         !    !!  !           !    !     !!';'!! .!!........        !! !!!     !    !   !  !  !!';'!!!. !.  !  !.   !      !!!!!    !!   !   ! !! !!!';'!!!. !.  !  !.   !       !!!!!    !   !!    !  !!!';'!!.. !.  !  !..  !        !  !    !!   !    !   !!';'!!.! !.!  ! ! ..  ! !!!!!!  !      !   !    !   !!';'!!.! !.!  ! !! .  ! !      !        !  !    !   !!';'!!.! !.!  !  ! .  ! !!   !!    !!!! !  !    !   !!';'!!.! !.!! !  ! .  !  !!!   !!!!     !  !    !!  !!';'!!.! !. ! !  ! .  !    !!        !  !   !    !  !!';'! .!!!. ! !  !!.   !    !        !  !   !    !   !';'! .!!!. ! !   !.   !     !       !   !  !    !   !';'! .! !. ! !   !.   !     !       !   !   !   !   !';'! .! !. ! !!  !....!!!   !      !!   !     ! !   !';'! .!  ..!  !  !   ...!!!!       !    !     ! ! ! !';'! .!   .!  !  !!!!!!.... !!!!!!!             ! ! !';'! .! !!.!  !! !  !!!!  .!                        !';'! .!!!!.!   !!!! !!!   .!   !!!!!   !!!!!!!!!!!  !';'! .. !!.!    !!!  !!  !.!!                       !';'!!!.. !. !   !!!      !..!        !!!   !   !    !';'!!! .... !  !!!!      ! .!        !             !!';'!!!!!!!!!!!!!!!!!!!!!!! .! !!!!!!!!!!!!!!!!!!!! !!';'!!!  !                  .!                     !!!';'!!   !   !!!  !!        .!                      !!';'!!   !  !     !!  !!!!!!.!  !!!!!!              !!';'!!   !  !    !!   !!!!!!.! !!!!!!!!          !  !!';'!!  !   !   !!   !!!!!!!.! !!!!!! !!   !  !     !!';'!!  !   !   !   !!!!!!!!.! !!!  !  !   !        !!';'!!  !   !   !  !!!!!!!!!.! !!!! !   !  !  !  !  !!';'!!  !   !   !           .!  !!  !   !  !     !  !!';'!! !!!  !   !   !!!!!!  .!      !    !!         !!';'!! ! !   !  !   !     !!. !    !!!    !    !    !!';'!! ! !   !  !   ! !     .  !   ! !!    !     !  !!';'!! ! !   !  !   ! !!  !!.   !!!   !!   !   ! !  !!';'!! ! !   !  !!  ! !!! ! .....     !!   !      ! !!';'!! ! !   !   !  ! !!!!!!!   .     !    !        !!';'!  ! !   !   !  !  !!!  !   .!!!!     !  !       !';'!  ! !   !   !! !       !   .!      !!.......... !';'! !! !   !    !  !!!!!!!!!  .!    !!  .!   !!!!. !';'! !  !   !    !!       !!!  .!!!!!   ..   !    . !';'! !  !    !!   !!!     !!!  .......... !!!!    . !';'! !  !     !!     !!!!      !!!!!!!!!!!!      !. !';'! !         !         !!!!!!                  O. !';'!           !                                    !';'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'];
    %maze=['!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!';'!      !!!!   ....!!!        !!!       !!!!     !!';'!      !      .!!..........                  !!!!!';'!  !!! ! !!   ...!!!  !!!!. !!!!!!!!!      !!!   !';'! !!   !   !!!!!.. !!    !.         !!    !!     !';'!!!  !!         !.   !!  !.....      !    !     !!';'!!  !!          ..    !! !!!  .  !    !   !  !  !!';'!!!  !   !  !   .!      !!!!! .  !!   !   ! !! !!!';'!!!  !   !  !   .!       !!!!!.   !   !!    !  !!!';'!!   !   !  !   .!        !  !.   !!   !    !   !!';'!! ! ! !  ! !   . ! !!!!!!  ! ..   !   !    !   !!';'!! ! ! !  ! !!  . ! !      !   .....!  !    !   !!';'!! ! ! !  !  !  . ! !!   !!    !!!!.!  !    !   !!';'!! ! ! !! !  !  ..!  !!!   !!!!    .!  !    !!  !!';'!! ! !  ! !  !   .!    !!        ! .!   !    !  !!';'!  !!!  ! !  !!  . !    !        ! .!   !    !   !';'!  !!!  ! !   !  ..!     !       ! . !  !    !   !';'!  ! !  ! !   !   .!     !       ! . !   !   !   !';'!  ! !  ! !!  !   .!!!   !      !! . !     ! !   !';'!  !    !  !  !   ...!!!!       !  . !     ! ! ! !';'!  !    !  !  !!!!!!.... !!!!!!!   .         ! ! !';'!  ! !! !  !! !  !!!!  .!          .             !';'!  !!!! !   !!!! !!!   .!   !!!!!  .!!!!!!!!!!!  !';'!    !! !    !!!  !!  !.!!..........             !';'!!!   !  !   !!!      !. !.       !!!   !   !    !';'!!!      !  !!!!      !. !.       !             !!';'!!!!!!!!!!!!!!!!!!!!!!!. !.!!!!!!!!!!!!!!!!!!!! !!';'!!!  !                 . !.....................!!!';'!!   !   !!!  !!  O..... !                    ..!!';'!!   !  !     !!  !!!!!! !  !!!!!!             .!!';'!!   !  !    !!   !!!!!! ! !!!!!!!!          ! .!!';'!!  !   !   !!   !!!!!!! ! !!!!!! !!   !  !    .!!';'!!  !   !   !   !!!!!!!! ! !!!  !  !   !       .!!';'!!  !   !   !  !!!!!!!!! ! !!!! !   !  !  !  ! .!!';'!!  !   !   !            !  !!  !   !  !     ! .!!';'!! !!!  !   !   !!!!!!   !      !    !!        .!!';'!! ! !   !  !   !     !!  !    !!!    !    !   .!!';'!! ! !   !  !   ! !        !   ! !!    !     ! .!!';'!! ! !   !  !   ! !!  !!    !!!   !!   !   ! ! .!!';'!! ! !   !  !!  ! !!! !           !!   !      !.!!';'!! ! !   !   !  ! !!!!!!!         !    !       .!!';'!  ! !   !   !  !  !!!  !    !!!!     !  !     . !';'!  ! !   !   !! !       !    !      !!         . !';'! !! !   !    !  !!!!!!!!!   !    !!   !   !!!!. !';'! !  !   !    !!       !!!   !!!!!        !    . !';'! !  !    !!   !!!     !!!   !         !!!!    . !';'! !  !     !!     !!!!      !!!!!!!!!!!!      !..+';'! !         !         !!!!!!                     !';'!           !              !                     !';'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!']

;

    %replace dot with space
    maze(maze=='.')=' ';
    %position of mouse
    [u,v]=find(maze=='O'); 
    maze(maze=='O') = ' ';
    step_counter = 0;
    while true; %game loop
        %test if mouse found cheese
        if maze(u,v) == '+';
            disp(['mouse found cheese after ', num2str(step_counter), ' steps']);
            break;
        end

        %extract NESW tiles
        nesw = [maze(u-1,v),maze(u,v+1),maze(u+1,v),maze(u,v-1)];

        %get result and move accordingly
        answer = find_the_cheese(nesw);
        switch answer;
            case 'n';
                u = u-1;
            case 'e';
                v = v+1;
            case 's';
                u = u+1;
            otherwise;
                v = v-1;
        end

        %make sure, mouse did not run into wall
        assert(maze(u,v) ~= '!','mouse ran into wall!');
        step_counter = step_counter + 1;
    end
end


function step = find_the_cheese(nesw)
    global State;
    NESW = 'nesw';
    NESW_REVERSE = 'swne';

    if all(nesw == '0000');
        return;
    elseif ~isfield(State,'maze');
        State = struct('maze', zeros(140)+' ','u',75,'v',75,'state','E');
        State.maze(State.u,State.v) = 'S';
    end    
    if State.maze(State.u-1,State.v) == ' '
        State.maze(State.u-1,State.v) = nesw(1);
    end
    if State.maze(State.u,State.v+1) == ' '
        State.maze(State.u,State.v+1) = nesw(2);
    end
    if State.maze(State.u+1,State.v) == ' '
        State.maze(State.u+1,State.v) = nesw(3);
    end
    if State.maze(State.u,State.v-1) == ' '
        State.maze(State.u,State.v-1) = nesw(4);
    end

    current_nesw = [State.maze(State.u-1,State.v),State.maze(State.u,State.v+1),State.maze(State.u+1,State.v),State.maze(State.u,State.v-1)];

    if any(current_nesw == '+'); % if there is cheese, go there
        nesw_index = find(current_nesw == '+',1);

    elseif any(current_nesw == ' '); % if there is a path that we did not walk, go there
        nesw_index = find(current_nesw == ' ',1);
        State.state = 'E';

    else % return to previous crossing
        nesw_index = find(NESW == State.maze(State.u,State.v),1);
        assert(numel(nesw_index)>0,'not enough indices')

        State.state = 'R';
    end

    %execute the step
    if State.state == 'E';
        step = NESW(nesw_index);
    else %State.state = 'R'
        step = State.maze(State.u, State.v);
    end

    switch step;
        case 'n';
            State.u = State.u-1;
        case 'e';
            State.v = State.v+1;
        case 's';
            State.u = State.u+1;
        otherwise;
            State.v = State.v-1;
    end

    if State.maze(State.u,State.v) == ' '; %if we do not have a reverse poniter yet
        State.maze(State.u,State.v) = NESW_REVERSE(nesw_index); %reverse pointer
    end

    %disp_important(State.maze); %uncomment for display
end


function disp_important(m) %just show the important stuff of the maze
    if any(m(:) ~= ' ');
        for k=1:4
            m = rot90(m);
            while all(m(:,1) == ' ');
                m = m(:,2:end);
            end
        end
    end
    if numel(m) == 0;
        m = 'X';
    end
    m = padarray(m,[1,1],35,'both');
    disp([m,'']);
end

1

간단한 봇, Java 1.4+, 176 + 25 + 1118 + 486 + 10944 + 1847 = 14596 단계

이 봇은 방문한 각 셀에 도달하기 위해 취한 단계 수를 계산하고 기록한 다음 이동할 방향을 결정할 때 가장 낮은 단계 수를 갖는 방향을 선택합니다. 동점 일 경우 방향을 N, E, S, W 순서로 선택합니다.

나는 녹을 설치하지 않았으므로 자바로 컨트롤러를 구현해야했고 이것이 테스트에 사용 된 봇이었습니다. 좀 더 영리한 솔버를 시도 할 것입니다.

결정 론적. 로 실행java SimpleBot

import java.io.IOException;
import java.io.InputStream;

public class SimpleBot 
{
    private static final char[] DIRECTION = {'n','e','s','w'};
    public static void main(String[] args) throws Exception
    {
        int[][] stepMap = new int[100][100];
        int mx=49, my=49;
        int[][] offsets = new int[][]{{0,-1},{1,0},{0,1},{-1,0}};

        String line=readLine(System.in);
        int step=0;
        while (line!=null && !"0000".equals(line))
        {
            stepMap[mx][my]=step++;

            int minStep = Integer.MAX_VALUE;
            int minIndex = -1;
            for (int i=0;i<4;i++)
            {
                if (line.charAt(i) == '+')
                {
                    minIndex=i;
                    break;
                }
                else if (line.charAt(i) == ' ')
                {
                    if (stepMap[mx+offsets[i][0]][my+offsets[i][1]]<minStep)
                    {
                        minStep = stepMap[mx+offsets[i][0]][my+offsets[i][1]];
                        minIndex=i;
                    }
                }
            }
            mx +=offsets[minIndex][0];
            my +=offsets[minIndex][1];

            System.out.println(DIRECTION[minIndex]);
            line=readLine(System.in);
        }
    }

    /**
     * Reads a line of text from the input stream. Blocks until a new line character is read.
     * NOTE: This method is used in favor of BufferedReader.readLine(...) as BufferedReader buffers data before performing
     * text line tokenization. This means that BufferedReader.readLine() will block until sufficient input have been received. 
     * @param in a InputStream, nominally System.in
     * @return a line of text or null if end of stream.
     * @throws IOException
     */
    private static String readLine(InputStream in) throws IOException
    {
       StringBuilder sb = new StringBuilder();
       int readByte = in.read();
       while (readByte>-1 && readByte!= '\n')
       {
          sb.append((char) readByte);
          readByte = in.read();
       }
       return readByte==-1?null:sb.toString();
    }

}
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.