[POJ]poj1573(模擬)
阿新 • • 發佈:2019-03-12
clas push_back ring main stream loop oop ack nbsp
題意:模擬
模擬就行
#include<iostream> #include<cstring> #include<cstdio> using namespace std; #define rep(i,n) for(int i=1;i<=n;i++) #define pb push_back #define mp make_pair #define LL long long #define nd second #define st first #define pii pair<int , int> constint N = 2e1+7; const int dx[]={1,-1,0,0}; const int dy[]={0,0,1,-1}; int n,m,s,cnt; int a[N][N],vis[N][N],tag[N][N]; void dfs(int x,int y,int step){ if(x>n||y>m||x<1||y<1){ printf("%d step(s) to exit\n",step); return; } if(tag[x][y]==-1)tag[x][y]=step; vis[x][y]++; if(vis[x][y]==2){ printf("%d step(s) before a loop of %d step(s)\n",tag[x][y],step-tag[x][y]); return ; } int nx=x+dx[a[x][y]]; int ny=y+dy[a[x][y]]; dfs(nx,ny,step+1); } void init(){ memset(vis,0,sizeof(vis)); memset(tag,-1,sizeof(tag)); }int main(){ while(cin>>n>>m>>s){ if(!n)return 0; init(); rep(i,n){ string s; cin>>s; for(int j=0;j<s.size();j++){ if(s[j]==‘S‘)a[i][j+1]=0; if(s[j]==‘N‘)a[i][j+1]=1; if(s[j]==‘E‘)a[i][j+1]=2; if(s[j]==‘W‘)a[i][j+1]=3; } } dfs(1,s,0); } }
[POJ]poj1573(模擬)