No older revisions available
No older revisions available
~cpp
#include <iostream>
#include <string>
#include <list>
using namespace std;
#define Max 25
#define north 0
#define east 1
#define south 2
#define west 3
#define right 1
#define left -1
#define Max_turn 3
#define Direction 4
int now_row, now_col, terminal_row, terminal_col;
bool table[Max][Max];
int direction_row[4] = {-1,0 , 1,0 };
int direction_col[4] = {0,1 ,0 ,-1 };
struct state
{
int row;
int col;
int direction;
int length;
int time;
int turn_num;
int pre_turn;
};
void init(int M, int N)
{
int i, j;
string temp;
for(i = 0; i < M; i++)
{
for(j = 0; j < N; j++)
table[i][j] = false;
}
for(i = 0; i < M; i++)
{
cin >> temp;
for(j = 0; j < N; j++)
{
if(temp[j] == '.')
table[i][j] = true;
if(temp[j] == 'S')
{
table[i][j] = true;
now_row = i;
now_col = j;
}
if(temp[j] == 'T')
{
table[i][j] = true;
terminal_row = i;
terminal_row = j;
}
}
}
}
int length(int M, int N)
{
state now = {now_row,now_col, 0, 0, 0, 0, 0}, next;
list<state> test;
int a =5;
cout << a;
while(now.row != terminal_row || now.col != terminal_col || now.length%5 != 0)
{
next = now;
next.row += direction_row[next.direction];
next.col += direction_col[next.direction];
next.time++;
next.length++;
next.pre_turn = next.turn_num = NULL;
if(next.row >= 0 && next.row < M && next.col >= 0 && next.col < N && table[next.row][next.col] )
test.push_back(next);
next = now;
if(next.turn_num < Max_turn)
{
if(next.pre_turn == right)
{
next.turn_num++;
next.time++;
next.direction = (next.direction + right)%Direction;
test.push_back(next);
}
else if(next.pre_turn == left)
{
next.turn_num++;
next.time++;
next.direction = (next.direction + left)%Direction;
test.push_back(next);
}
else if(next.pre_turn == NULL)
{
next.turn_num++;
next.time++;
next.pre_turn = right;
next.direction = (next.direction + right)% Direction;
test.push_back(next);
next.pre_turn = left;
next.direction = (next.direction + 2*left)% Direction;
test.push_back(next);
}
}
now = test.front();
test.pop_front();
}
return now.length;
}
void process(int M, int N)
{
init(M,N);
int a = 10;
for(int i = 0; i < M; i++)
{
for(int j = 0; j < N; j++)
cout <<table[i][j];
cout << endl;
}
cout << length(M,N) << endl;;
}
int main()
{
int M, N;
cin >> M >> N;
while(M != 0 || N != 0)
{
process(M,N);
cin >> M >> N;
}
return 0;
}