PKU 1573 Robot Motion
http://poj.org/problem?id=1573
ロボットの動きをシミュレートして、ループと脱出を検知する。
string in[10]; int vis[10][10]; main(){ int r,c,s; while(cin>>r>>c>>s,r){ rep(i,r)cin>>in[i]; memset(vis,0,sizeof(vis)); int x=0,y=s-1; int cost=1; while(true){ if(x<0 || r<=x || y<0 || c<=y){ cout<<cost-1<<" step(s) to exit"<<endl; break; } if(vis[x][y]){ cout<<vis[x][y]-1<<" step(s) before a loop of "<<cost-vis[x][y]<<" step(s)"<<endl; break; } vis[x][y]=cost; ++cost; switch(in[x][y]){ case 'W':--y;break; case 'S':++x;break; case 'E':++y;break; case 'N':--x;break; } } } }