Title Description: There is a puzzle game, the classic Pacman game has been adapted. The player will play in a grid map similar to the following image, and when the player finishes eating all the beans, the game ends. The specific rules of the game are as follows: 1. After the game starts, the player is born in the upper left corner of the map and faces to the right.
2. The player can only move one box at a time, the way to move can only choose one of the following two ways: A. Press the player's current orientation Level one pane (if the player is currently facing to the right, the player moves to the right one, and if the player is currently facing to the left, the player moves one cell to the left).
B. Move down one cell. However, each move down, the player will change direction, if the player is currently facing the right side, moving down one pane, the face to the left, and vice versa. 3. When the player moves to the "beans" place, it will "eat" the Beans, "eat" the operation for the game automatically completed.
4. After the player has "eaten" all the beans in the map, the game is successfully cleared and the total number of steps that the player takes to move is counted, and the lower the number of steps, the higher the score.
Now I want you to write a "plug-in" for the game, when the game map is loaded, automatically plan to calculate the game can be cleared and the minimum number of steps to take, for convenience, you only need to output the number of steps.
Input
The first line will enter two numbers n and M (1<=n, m<=150) separated by spaces, each representing the number of rows and columns currently loading the map. The next n rows will enter m characters to describe the contents of the map. Where ' E ' stands for no beans in the lattice, ' W ' stands for beans. We make sure that the upper left-hand corner of the lattice is no beans in the spawn point.
Output
An integer that enables the game to pass the minimum number of steps.
Sample input
4 5 Eweeweeweeeweeeweeee
Sample output
11
#include <iostream> #include <vector> using namespace std;
int main () {int n,m;
int flower=0;
vector<vector<int>> VV;
while (cin>>n>>m) {vv.resize (n);
for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {char tmp;
cin>>tmp;
if (tmp== ' E ') vv[i].push_back (0);
if (tmp== ' W ') {vv[i].push_back (1);
flower++;
}}}} if (flower==0) {cout<<0<<endl;
return 0;
} int cursor=0,pre_cur=0;
int count=0,delta=0;
BOOL End=false;
Odd Line:from right-to-left.
Even line:from left to right. for (int i=0;i<vv.size (); i++) {//Pre-Search 1 if (i%2==0) {for (int j=vv[i].size () -1;j>=i ; j--) {if (Vv[i][j] | | (I+1<n && vv[i+1][j]))
{pre_cur=j; Break }}} else{for (int j=0;j<vv[i].size (); j + +) {if (Vv[i][j] | | (I+1<n && vv[i+1][j]))
{pre_cur=j;
Break
}}} count+=abs (Pre_cur-cursor); When no flowers, it'll turn down to the end.
So calculate the steps, if (ABS (pre_cur-cursor) ==0 && vv[i][cursor]==0) {delta++;
}else{delta=0;
} cursor=pre_cur;
count++;//Next line!
} cout<<count-delta-1<<endl;
return 0; }
E Special test Cases:
4 5
Eweew
Eweee
Eeeee
Eeeee
4 5
Eeeee
Eeeee
Eeeee
Eeeee