编程介的小学生 2017-11-26 03:56 采纳率: 20.5%
浏览 673
已采纳

Lemmings, Lemmings Everywhere. But Not For Long.

Problem Description
On an n * m board there is a lemming on each square. Every second, the lemmings try to move either north, south, east or west, according to rules which are explained below. To determine which direction to move in, each lemming has an agenda, which is an ordering of the four possible directions (for example, one possible agenda might be NWES). The rules for lemming movement are the following:
1. Initially each lemming sets its direction of movement D to the first direction in its agenda.
2. At each time step, each lemming tries to move in its direction D. Three things can happen to lemming L:
(a) If L's current direction D causes it to move off the board, then the world has one less lemming in it. Otherwise, L's target destination will be to another square.
(b) If L's target square is empty or about to become empty as a result of another lemming leaving it, and no other lemming wants to move to the same square, then L moves into its target square. In this case, the lemming will use the same direction D in the next time step.
(c) Otherwise, if another lemming is trying to move into L's target square, or if the target square contains a lemming which cannot move, then L stays put. In this case, it will update its D by going to the next direction in its agenda (wrapping around to the beginning if necessary).
Two lemmings which want to exchange squares can do so, unless of course some other lemming is trying to move into one of their two squares (in which case all three of the lemmings would stay in their current squares). Lemmings being lemmings, they continue to move until all of them have moved off the board.
Your job is to determine how long that takes.

Input
Input will consist of multiple test cases. Each test case will consist of multiple lines. The first line will contain two positive integers n m, specifying the number of rows and columns in the board. The maximum value for each of these is 100. The board is situated so that square (0, 0) is in the southwest corner, and square (0,m -1) is in the southeast corner. Following this will be several rows containing the agendas for the nm lemmings. Each agenda will be a permutation of the string NESW. There will be 16 agendas on each line (except perhaps the last), with a single space between each. The agendas are assigned row-wise to the lemmings, so that the first agenda is associated with the lemming on square (0, 0), the second with the lemming on square (0, 1), and so on. The last case will be followed by the line 0 0 which will terminate input.

Output
For each test case, output a single line containing the case number (using the format shown below) followed by the number of steps it takes until the last lemming(s) falls off the board. Use only single spaces to separate items on a line.

Sample Input
2 2
ENWS WSNE NESW WENS
2 2
ENWS WSNE NESW SWEN
0 0

Sample Output
Case 1: 2
Case 2: 3

  • 写回答

1条回答

报告相同问题?

悬赏问题

  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 武汉岩海低应变分析软件,导数据库里不显示波形图
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile