Description
As you probably know, Berhattan is a district of Berland's largest city and it consists of equal square blocks. There are n block lines in the east-west direction and m block lines in the south-north direction. The map shows Berhattan as a rectangle with n rows and mcolumns, so there are nx m blocks in total.
There are n+1 streets running parallel in the east-west direction (horizontally), and there are m+1 avenues running parallel in the south-north direction (vertically). Streets and avenues split the district into blocks and separate Berhattan from other districts of Berland. Each block in Berhattan is characterized by its beauty bij.
A pedestrian can walk only along streets and avenues. When the pedestrian walks along any of four sides of a block, we say he passes the block. Every time the pedestrian passes a block his satisfaction is increased by bij. If the pedestrian has already passed the block one or more times his satisfaction is increased only by bij/2 rounded down when he passes the block again.
You are given the map of Berhattan with the information about the blocks' beauty and the pedestrian's path along the streets and avenues. The path is given as a string containing letters '
Your task is to calculate the total satisfaction the pedestrian will get after finishing his route.
Picture of the sample test
There are n+1 streets running parallel in the east-west direction (horizontally), and there are m+1 avenues running parallel in the south-north direction (vertically). Streets and avenues split the district into blocks and separate Berhattan from other districts of Berland. Each block in Berhattan is characterized by its beauty bij.
A pedestrian can walk only along streets and avenues. When the pedestrian walks along any of four sides of a block, we say he passes the block. Every time the pedestrian passes a block his satisfaction is increased by bij. If the pedestrian has already passed the block one or more times his satisfaction is increased only by bij/2 rounded down when he passes the block again.
You are given the map of Berhattan with the information about the blocks' beauty and the pedestrian's path along the streets and avenues. The path is given as a string containing letters '
L', '
R' and '
M', where '
L' means a 90 degree left turn, '
R' means a 90 degree right turn, and '
M' means walking one block forward by a street or avenue. Facing the east, the pedestrian starts his path in the north-west corner of Berhattan having zero satisfaction level. His path can cross itself and go along the same streets or avenues several times. Pedestrian's satisfaction is increased every time he moves according to the rules described above.
Your task is to calculate the total satisfaction the pedestrian will get after finishing his route.
Picture of the sample test
Input
The first line of input contains two integers n and m (1 ≤ n, m ≤ 100), where n is a number of block lines in Berhattan running in the east-west direction, and m is a number of block lines in Berhattan running in the south-north direction. The following n lines contain m digits each. The j-th digit of the i-th line represents bij (0 ≤ bij ≤ 9) — the beauty of the corresponding block. The last line of input contains a path in the format specified above. The path consists of 1 up to 500 characters, inclusively. It is guaranteed that the given path doesn't go outside Berhattan.
Output
Print a single integer to the output — the total pedestrian's satisfaction.
Sample Input
sample input |
sample output |
3 3 123 456 789 MRMMLM |
22 |
简单模拟,n,m貌似给反了(两个地方给的不一致 ) 害我wa了两发
1 /************************************************************************* 2 > File Name: code/2015summer/#5/K.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年07月30日 星期四 14时00分56秒 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<iomanip> 10 #include<cstdio> 11 #include<algorithm> 12 #include<cmath> 13 #include<cstring> 14 #include<string> 15 #include<map> 16 #include<set> 17 #include<queue> 18 #include<vector> 19 #include<stack> 20 #define y0 abc111qqz 21 #define y1 hust111qqz 22 #define yn hez111qqz 23 #define j1 cute111qqz 24 #define tm crazy111qqz 25 #define lr dying111qqz 26 using namespace std; 27 #define REP(i, n) for (int i=0;i<int(n);++i) 28 typedef long long LL; 29 typedef unsigned long long ULL; 30 const int inf = 0x7fffffff; 31 const int N=1e2+5; 32 int b[N][N]; 33 int n,m; 34 char cmd[505]; 35 bool vis[N][N]; 36 int nx,ny; 37 int dx[4]={-1,0,1,0}; 38 int dy[4]={0,1,-0,-1}; 39 char ch[N][N]; 40 int main() 41 { 42 cin>>n>>m; 43 nx = 0; 44 ny = 0; 45 for ( int i = 0 ; i < n ; i++) 46 cin>>ch[i]; 47 for ( int i = 0 ; i <n ; i++ ) 48 { 49 for ( int j = 0 ; j < m ; j++ ) 50 { 51 b[i+1][j+1]=(int)(ch[i][j]-'0'); 52 } 53 } 54 int ans = 0; 55 memset(vis,false,sizeof(vis)); 56 int dir = 1; 57 cin>>cmd; 58 int len = strlen(cmd); 59 for ( int i = 0 ; i < len ; i ++ ) 60 { 61 // cout<<"ans:"<<ans<<endl; 62 // cout<<"nx:"<<nx<<" ny:"<<ny<<endl; 63 if (cmd[i]=='L') 64 { 65 dir = (dir+3)%4; 66 } 67 if (cmd[i]=='R') 68 { 69 dir = (dir+1)%4; 70 } 71 if (cmd[i]=='M') 72 { 73 if (dir==0) 74 { 75 if (vis[nx][ny]) 76 { 77 ans = ans + b[nx][ny]/2; 78 } 79 else 80 { 81 ans = ans + b[nx][ny]; 82 } 83 if (vis[nx][ny+1]) 84 { 85 ans = ans + b[nx][ny+1]/2; 86 } 87 else 88 { 89 ans = ans + b[nx][ny+1]; 90 } 91 vis[nx][ny]=true; 92 vis[nx][ny+1]=true; 93 nx = nx +dx[dir]; 94 ny = ny +dy[dir]; 95 } 96 if (dir==2) 97 { 98 nx = nx + dx[dir]; 99 ny = ny + dy[dir]; 100 if (vis[nx][ny]) 101 { 102 ans = ans + b[nx][ny]/2; 103 } 104 else 105 { 106 ans = ans + b[nx][ny]; 107 } 108 if (vis[nx][ny+1]) 109 { 110 ans = ans + b[nx][ny+1]/2; 111 } 112 else 113 { 114 ans = ans + b[nx][ny+1]; 115 } 116 vis[nx][ny]=true; 117 vis[nx][ny+1]=true; 118 } 119 if (dir==1) 120 { 121 nx = nx + dx[dir]; 122 ny = ny + dy[dir]; 123 if (vis[nx][ny]) 124 { 125 ans = ans + b[nx][ny]/2; 126 } 127 else 128 { 129 ans = ans + b[nx][ny]; 130 } 131 if (vis[nx+1][ny]) 132 { 133 ans = ans + b[nx+1][ny]/2; 134 } 135 else 136 { 137 ans = ans + b[nx+1][ny]; 138 } 139 vis[nx][ny]=true; 140 vis[nx+1][ny]=true; 141 142 } 143 if (dir==3) 144 { 145 146 if (vis[nx][ny]) 147 { 148 ans = ans + b[nx][ny]/2; 149 } 150 else 151 { 152 ans = ans + b[nx][ny]; 153 } 154 if (vis[nx+1][ny]) 155 { 156 ans = ans + b[nx+1][ny]/2; 157 } 158 else 159 { 160 ans = ans + b[nx+1][ny]; 161 } 162 vis[nx][ny]=true; 163 vis[nx+1][ny]=true; 164 nx = nx +dx[dir]; 165 ny = ny +dy[dir]; 166 } 167 } 168 169 } 170 cout<<ans<<endl; 171 172 return 0; 173 }