【题目描述】
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; int map[105][105]; int n,m,i,j; int xx,yy,k; char s; int run; void dfs(int x,int y,char ss) { if(run==k) { printf("%d %d\n",x,y); return; } if(map[x][y]==0) { if(s=='U') { s='L'; map[x][y]=1; y=y-1; run++; dfs(x,y,s); } else if(s=='D') { s='R'; map[x][y]=1; y=y+1; run++; dfs(x,y,s); } else if(s=='L') { s='D'; map[x][y]=1; x=x+1; run++; dfs(x,y,s); } else if (s=='R') { s='U'; map[x][y]=1; x=x-1; run++; dfs(x,y,s); } } else { if(s=='U') { s='R'; map[x][y]=0; y=y+1; run++; dfs(x,y,s); } else if(s=='L') { s='U'; map[x][y]=0; x=x-1; run++; dfs(x,y,s); } else if(s=='D') { s='L'; map[x][y]=0; y=y-1; run++; dfs(x,y,s); } else if(s=='R') { s='D'; map[x][y]=0; x=x+1; run++; dfs(x,y,s); } } } int main() { cin>>n>>m; for(i=0; i<n; ++i) for(j=0; j<m; ++j) { cin>>map[i][j]; } run=0; cin>>xx>>yy>>s>>k; dfs(xx,yy,s); return 0; }
原文地址:http://blog.csdn.net/u013050857/article/details/44981159