Robot Motion
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 10219 |
|
Accepted: 4977 |
Description
A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The pZ??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vc3NpYmxlIGluc3RydWN0aW9ucyBhcmUgPGJyPgo8YnI+Ck4gbm9ydGggKHVwIHRoZSBwYWdlKSA8YnI+ClMgc291dGggKGRvd24gdGhlIHBhZ2UpIDxicj4KRSBlYXN0ICh0byB0aGUgcmlnaHQgb24gdGhlIHBhZ2UpIDxicj4KVyB3ZXN0ICh0byB0aGUgbGVmdCBvbiB0aGUgcGFnZSkgPGJyPgo8YnI+CkZvciBleGFtcGxlLCBzdXBwb3NlIHRoZSByb2JvdCBzdGFydHMgb24gdGhlIG5vcnRoICh0b3ApIHNpZGUgb2YgR3JpZCAxIGFuZCBzdGFydHMgc291dGggKGRvd24pLiBUaGUgcGF0aCB0aGUgcm9ib3QgZm9sbG93cyBpcyBzaG93bi4gVGhlIHJvYm90IGdvZXMgdGhyb3VnaCAxMCBpbnN0cnVjdGlvbnMgaW4gdGhlIGdyaWQgYmVmb3JlIGxlYXZpbmcgdGhlIGdyaWQuIDxicj4KPGJyPgpDb21wYXJlIHdoYXQgaGFwcGVucyBpbiBHcmlkIDI6IHRoZSByb2JvdCBnb2VzIHRocm91Z2ggMyBpbnN0cnVjdGlvbnMgb25seSBvbmNlLCBhbmQgdGhlbiBzdGFydHMgYSBsb29wIHRocm91Z2ggOCBpbnN0cnVjdGlvbnMsIGFuZCBuZXZlciBleGl0cy4gPGJyPgo8YnI+CllvdSBhcmUgdG8gd3JpdGUgYSBwcm9ncmFtIHRoYXQgZGV0ZXJtaW5lcyBob3cgbG9uZyBpdCB0YWtlcyBhIHJvYm90IHRvIGdldCBvdXQgb2YgdGhlIGdyaWQgb3IgaG93IHRoZSByb2JvdCBsb29wcyBhcm91bmQuIDxicj4KCjxwIGNsYXNzPQ=="pst">Input
There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.
Output
For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.
Sample Input
3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0 0
Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)
Source
Mid-Central USA 1999
http://poj.org/problem?id=1573
//主要是判断是否构成循环,根据题目特点,可以设置一个标记数组,如果走过了就不能在走,直到退出循环。
用另外一个数组表示到这一个坐标的步数。那么退出循环的时候其实已经到以前走过的点,输出步数,然后循环节就是总步数减去当前步数。
#include
#include
int main() { int n,m,k,l,p,step; char s[11][11]; int map[11][11]; while(scanf("%d %d",&n,&m)!=EOF) { if(n==0||m==0)break; scanf("%d",&k); for(int i=0;i
=0&&l
=0&&p