编程介的小学生 2017-10-12 15:20 采纳率: 20.5%
浏览 1388
已采纳

Obstructed Rook Circuits

Description

A board is a rectangular array of squares such as on a chessboard, with possibly some squares blocked off. A rook tour of a board is a path that visits each empty square of the board exactly once, moving at each step to an empty adjacent square (North, South, East or West but not diagonally). A rook tour is a rook circuit if it starts and ends on the same square. In the figures below, let the + symbol be the rook, and the X symbol be an obstruction. The following are descriptions of each figure: (a) is a board with no rook circuit, (b) and (c) give distinct rook circuits of the same board and (d) gives the unique (up to direction) rook circuit of another board.


Write a program, which takes as input the description of a board and either finds a rook circuit or determines that there is no rook circuit.
Input

Input consists of a sequence of board descriptions and starting points. The first line of the input is
Nrows Ncols Nblocks StartX StartY
where Nrows is the number of rows in the rectangular array, Ncols is the number of columns in the rectangular array, Nblocks is the number of blocked off squares on the board and (StartX, StartY) is the position on the board where the path is to start (and end). StartX and StartY are 0 based (StartX ranges from 0 to Ncols - 1). Following the first line there are Nblocks lines giving the coordinates of the blocked off squares, one per line. The coordinates of these points are 0 based and are of the form X Y. The final line of each board description is a blank line.
The last line of the input is line of 5 zeroes.
Output

If there is no rook circuit for the corresponding board, the output is a line "NO SOLUTION" followed by a blank line. Otherwise, the output is a sequence of the letters N, S, E, W giving the moves from the starting point to traverse a rook circuit and return to the starting point. (N indicates moving to the previous row, S moving to the next row, E moving to the next column and W moving to the previous column.) If more than 40 moves are required, the moves will be output 40 to a line (except possibly for the last line). The move output is to be followed by a blank line.
Note that the same board may
have several rook circuits. Your program need only find any one (correct) rook circuit.
Sample Input

4 4 2 0 0
1 2
3 0

4 4 0 2 2

4 4 0 0 0

4 4 2 1 2
0 3
2 2

8 8 0 0 0

0 0 0 0 0
Sample Output

NO SOLUTION

NENWWWSESWSEEENW

EEESWWSEESWWWNNN

WNNESENESSSWWN

EEEEEEESWWWWWWSEEEEEESWWWWWWSEEEEEESWWWW
WWSEEEEEESWWWWWWWNNNNNNN

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-10-30 13:54
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题