您的答案為: 25 0 E 正確答案為: 40 0 E LOST
程式碼:
#include<iostream>
#include<string>
#include<set>
#include<algorithm>
using namespace std;
int main(){
int rightLimit, upLimit;
cin>>rightLimit>>upLimit;
int xIni, yIni, x, y, xTmp, yTmp;
char direct, directTmp;
char scentDirect[upLimit+1][rightLimit+1] = {0};
set<char> upRight,upLeft,downRight,downLeft;
while(cin>>xIni>>yIni>>direct){
x = xIni;
y = yIni;
string instruct;
cin>>instruct;
bool loseORNOT = false;
int length = instruct.size();
for(int i=0; i<length; i++){
xTmp=x;
yTmp=y;
directTmp=direct;
if(instruct[i] == 'F'){
if( x==rightLimit && y==upLimit){ //upRight
set<char>::iterator it = upRight.find(direct);
if(it != upRight.end()) continue;
}else if(x==0 && y==upLimit){ //upLeft
set<char>::iterator it = upLeft.find(direct);
if(it != upLeft.end()) continue;
}else if(x==rightLimit && y==0){ //downRight
set<char>::iterator it = downRight.find(direct);
if(it != downRight.end()) continue;
}else if(x==0 && y==0){ //downLeft
set<char>::iterator it = downLeft.find(direct);
if(it != downLeft.end()) continue;
}else if(scentDirect[y][x] == direct) continue;
if(direct == 'E'){
x++;
}
else if(direct == 'W'){
x--;
}
else if(direct == 'S'){
y--;
}
else{
y++;
} //'N'
}else if(instruct[i] == 'R'){
if(direct == 'E'){
direct='S';
}
else if(direct == 'W'){
direct='N';
}
else if(direct == 'S'){
direct='W';
}
else{
direct='E';
} //'N'
}else if(instruct[i] == 'L'){
if(direct == 'E'){
direct='N';
}
else if(direct == 'W'){
direct='S';
}
else if(direct == 'S'){
direct='E';
}
else{
direct='W';
} //'N'
}
if((x>rightLimit || x<0) || (y>upLimit || y<0)){
if(xTmp==rightLimit && yTmp==upLimit){
upRight.insert(directTmp);
break;
}
else if(xTmp==0 && yTmp==upLimit){
upLeft.insert(directTmp);
break;
}
else if(xTmp==rightLimit && yTmp==0){
downRight.insert(directTmp);
break;
}
else if(xTmp==0 && yTmp==0){
downLeft.insert(directTmp);
break;
}
else{
loseORNOT=true;
scentDirect[yTmp][xTmp] = directTmp;
break;
}
}
}
if(loseORNOT){
cout<<xTmp<<" "<<yTmp<<" "<<directTmp<<" "<<"LOST"<<endl;
}
else{
cout<<x<<" "<<y<<" "<<direct<<endl;
}
}
return 0;
}
1.
char scentDirect[upLimit+1][rightLimit+1] = {0};
2.
if(xTmp==rightLimit && yTmp==upLimit){
upRight.insert(directTmp);
break;
}
else if(xTmp==0 && yTmp==upLimit){
upLeft.insert(directTmp);
break;
}
else if(xTmp==rightLimit && yTmp==0){
downRight.insert(directTmp);
break;
}
else if(xTmp==0 && yTmp==0){
downLeft.insert(directTmp);
break;
}
1. 二維vla不能這樣初始化
2. 在角落掉下去loseORNOT也要設為true吧
3. 其實這題根本不需要判斷角落吧,這樣只會把程式碼變得很複雜而已