题意:给定每一步的定方向与初始位置 让求出去房间 或者进入循环的步数
直接模拟
#include<iostream>
#include<cstring>
using namespace std;
int map[11][11];
int v[11][11];
int dir[4][2]={-1,0,1,0,0,-1,0,1};
int main()
{
int m,n,y,x,step,i,j;
int xx,yy;
char c;
while(cin>>m>>n>>y)
{
if(m==n&&n==y&&y==0) break;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
cin>>c;
switch(c){
case ‘N‘:map[i][j]=0;break;
case ‘S‘:map[i][j]=1;break;
case ‘W‘:map[i][j]=2;break;
case ‘E‘:map[i][j]=3;break;
}
}
x=1;
step=0;
memset(v,-1,sizeof(v));
while(true)
{
xx=x;yy=y;
v[x][y]=step++;
x+=dir[map[xx][yy]][0];
y+=dir[map[xx][yy]][1];
if(x<=0||x>m||y<=0||y>n)
{
cout<<step<<" step(s) to exit"<<endl;
break;
}
if(v[x][y]!=-1)
{
cout<<v[x][y]<<" step(s) before a loop of "<<step-v[x][y]<<" step(s)"<<endl;
break;
}
}
}
return 0;
}
时间: 2024-10-17 03:09:28