http://www.pipioj.online/problem.php?id=1021
1 #define IO std::ios::sync_with_stdio(0); 2 #define bug(x) cout<<#x<<" is "<<x<<endl 3 #include <bits/stdc++.h> 4 #define iter ::iterator 5 using namespace std; 6 typedef long long ll; 7 typedef pair<int,ll>P; 8 #define pb push_back 9 #define mk make_pair 10 #define se second 11 #define fi first 12 #define rs o*2+1 13 #define ls o*2 14 const ll mod=1e9+7; 15 const int N=1e2+5; 16 int T,n,m; 17 int sx,sy,ex,ey; 18 int a[N]; 19 char s[N][N]; 20 int vis[N][N]; 21 int dx[8]={-1,0,1,0}; 22 int dy[8]={0,1,0,-1}; 23 map<char,int>mp; 24 void gao(){ 25 mp[‘U‘]=0; 26 mp[‘R‘]=1; 27 mp[‘D‘]=2; 28 mp[‘L‘]=3; 29 } 30 int check(int x,int y){ 31 if(x<1||x>n||y<1||y>m||s[x][y]==‘*‘||vis[x][y])return 0; 32 return 1; 33 } 34 int bfs(){ 35 int ans=1; 36 int r=mp[s[sx][sy]]; 37 int x=sx,y=sy,px,py; 38 int f=0; 39 vis[sx][sy]=1; 40 while(1){ 41 px=x,py=y; 42 x=x+dx[r]; 43 y=y+dy[r]; 44 f++; 45 if(check(x,y)){ 46 vis[x][y]=1; 47 f=0; 48 ans++; 49 } 50 else{ 51 if(f>1)return ans; 52 x=px; 53 y=py; 54 r=(r+1)%4; 55 } 56 } 57 } 58 int main(){ 59 gao(); 60 while(~scanf("%d%d",&n,&m)){ 61 for(int i=1;i<=n;i++){ 62 scanf("%s",s[i]+1); 63 for(int j=1;j<=m;j++){ 64 vis[i][j]=0; 65 if(s[i][j]!=‘*‘&&s[i][j]!=‘.‘){ 66 sx=i; 67 sy=j; 68 } 69 } 70 } 71 printf("%d\n",bfs()); 72 } 73 } 74 /* 75 76 2 3 77 ..* 78 .U. 79 80 2 3 81 ..* 82 U.. 83 84 2 3 85 .*. 86 U.. 87 88 4 4 89 .... 90 .**. 91 .**. 92 ...U 93 94 */
原文:https://www.cnblogs.com/ccsu-kid/p/14515284.html