POJ-1573 Robot Motion 模拟水题
题目链接:http://poj.org/problem?id=1573
随便模拟,水水~
1 //STATUS:G++_AC_0MS_696KB 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<string.h> 5 #include<math.h> 6 #include<iostream> 7 #include<string> 8 #include<algorithm> 9 #include<vector> 10 #include<queue> 11 #include<stack> 12 using namespace std; 13 #define LL __int64 14 #define pii pair<int,int> 15 #define Max(a,b) ((a)>(b)?(a):(b)) 16 #define Min(a,b) ((a)<(b)?(a):(b)) 17 #define mem(a,b) memset(a,b,sizeof(a)) 18 #define lson l,mid,rt<<1 19 #define rson mid+1,r,rt<<1|1 20 const int MAX=110,INF=0x3f3f3f3f,MOD=1999997; 21 const LL LLNF=0x3f3f3f3f3f3f3f3fLL; 22 23 char map[MAX][MAX]; 24 int ma[128],dx[4]={-1,0,1,0},dy[4]={0,1,0,-1}; 25 int n,m,s,ans1,ans2; 26 27 int dfs(int x,int y,int d) 28 { 29 int t=map[x][y]; 30 if(x<0||x>=n || y<0||y>=m){ 31 ans1=d; 32 return 1; 33 } 34 if(t<'A'){ 35 ans1=t;ans2=d-t; 36 return 0; 37 } 38 map[x][y]=d; 39 dfs(x+dx[ma[t]],y+dy[ma[t]],d+1); 40 } 41 42 int main() 43 { 44 // freopen("in.txt","r",stdin); 45 int i,j; 46 ma['N']=0,ma['E']=1,ma['S']=2,ma['W']=3; 47 while(~scanf("%d%d%d",&n,&m,&s) && n) 48 { 49 for(i=0;i<n;i++) 50 scanf("%s",map[i]); 51 52 if(dfs(0,s-1,0))printf("%d step(s) to exit\n",ans1); 53 else printf("%d step(s) before a loop of %d step(s)\n",ans1,ans2); 54 } 55 return 0; 56 }