您的答案為: 4 37 正確答案為: 4 37 E
#include<iostream>
#include<vector>
using namespace std;
char cd(char a,char b){
if(a=='N' and b=='R') return 'E';
else if(a=='N' and b=='L') return 'W';
else if(a=='E' and b=='R') return 'S';
else if(a=='E' and b=='L') return 'N';
else if(a=='S' and b=='R') return 'W';
else if(a=='S' and b=='L') return 'E';
else if(a=='W' and b=='R') return 'N';
else if(a=='W' and b=='L') return 'S';
}
void pos(int *a,int *b,char c){
if(c=='N') *b+=1;
else if(c=='E') *a+=1;
else if(c=='S') *b-=1;
else if(c=='W') *a-=1;
}
int main(){
int x,y,mx,my;
char d;
cin >> x >> y;
char lostpos[x][y]={};
string ins="";
while(cin>>mx>>my>>d>>ins){
bool isbotlost=false;
for(int i=0;i<ins.length();i++){
if(ins[i]=='L' or ins[i]=='R') d=cd(d,ins[i]);
else if(ins[i]=='F' and mx==x and d=='E' and lostpos[mx][my]=='*') continue;
else if(ins[i]=='F' and mx==0 and d=='W' and lostpos[mx][my]=='*') continue;
else if(ins[i]=='F' and my==y and d=='N' and lostpos[mx][my]=='*') continue;
else if(ins[i]=='F' and my==0 and d=='S' and lostpos[mx][my]=='*') continue;
else if(ins[i]=='F' and mx==x and d=='E'){
lostpos[mx][my]='*';
isbotlost=true;
break;
}
else if(ins[i]=='F' and mx==0 and d=='W'){
lostpos[mx][my]='*';
isbotlost=true;
break;
}
else if(ins[i]=='F' and my==y and d=='N'){
lostpos[mx][my]='*';
isbotlost=true;
break;
}
else if(ins[i]=='F' and my==0 and d=='S'){
lostpos[mx][my]='*';
isbotlost=true;
break;
}
else pos(&mx,&my,d);
}
if(isbotlost==true) cout << mx << " " << my << " " << d << " LOST" << endl;
else cout << mx << " " << my << " " << d << endl;
}
}
您的答案為: 4 37 正確答案為: 4 37 E
#include
#include
using namespace std;
char cd(char a,char b){
if(a=='N' and b=='R') return 'E';
else if(a=='N' and b=='L') return 'W';
else if(a=='E' and b=='R') return 'S';
else if(a=='E' and b=='L') return 'N';
else if(a=='S' and b=='R') return 'W';
else if(a=='S' and b=='L') return 'E';
else if(a=='W' and b=='R') return 'N';
else if(a=='W' and b=='L') return 'S';
}
void pos(int *a,int *b,char c){
if(c=='N') *b+=1;
else if(c=='E') *a+=1;
else if(c=='S') *b-=1;
else if(c=='W') *a-=1;
}
int main(){
int x,y,mx,my;
char d;
cin >> x >> y;
char lostpos[x][y]={};
string ins="";
while(cin>>mx>>my>>d>>ins){
bool isbotlost=false;
for(int i=0;i<ins.length();i++){
if(ins[i]=='L' or ins[i]=='R') d=cd(d,ins[i]);
else if(ins[i]=='F' and mx==x and d=='E' and lostpos[mx][my]=='*') continue;
else if(ins[i]=='F' and mx==0 and d=='W' and lostpos[mx][my]=='*') continue;
else if(ins[i]=='F' and my==y and d=='N' and lostpos[mx][my]=='*') continue;
else if(ins[i]=='F' and my==0 and d=='S' and lostpos[mx][my]=='*') continue;
else if(ins[i]=='F' and mx==x and d=='E'){
lostpos[mx][my]='*';
isbotlost=true;
break;
}
else if(ins[i]=='F' and mx==0 and d=='W'){
lostpos[mx][my]='*';
isbotlost=true;
break;
}
else if(ins[i]=='F' and my==y and d=='N'){
lostpos[mx][my]='*';
isbotlost=true;
break;
}
else if(ins[i]=='F' and my==0 and d=='S'){
lostpos[mx][my]='*';
isbotlost=true;
break;
}
else pos(&mx,&my,d);
}
if(isbotlost==true) cout << mx << " " << my << " " << d << " LOST" << endl;
else cout << mx << " " << my << " " << d << endl;
}
}
你的lostpost陣列開太小了(注意(x, y)是右上角頂點座標,左下角頂點座標是(0, 0),所以大小是x+1, y+1),所以可能會超出範圍。
至於為什麼這樣會造成少輸出我也不知道,只能說undefined behavior什麼神奇的事都有可能發生...如果有人知道原因的話歡迎補充...
您的答案為: 4 37 正確答案為: 4 37 E
#include
#include
using namespace std;
char cd(char a,char b){
if(a=='N' and b=='R') return 'E';
else if(a=='N' and b=='L') return 'W';
else if(a=='E' and b=='R') return 'S';
else if(a=='E' and b=='L') return 'N';
else if(a=='S' and b=='R') return 'W';
else if(a=='S' and b=='L') return 'E';
else if(a=='W' and b=='R') return 'N';
else if(a=='W' and b=='L') return 'S';
}
void pos(int *a,int *b,char c){
if(c=='N') *b+=1;
else if(c=='E') *a+=1;
else if(c=='S') *b-=1;
else if(c=='W') *a-=1;
}
int main(){
int x,y,mx,my;
char d;
cin >> x >> y;
char lostpos[x][y]={};
string ins="";
while(cin>>mx>>my>>d>>ins){
bool isbotlost=false;
for(int i=0;i<ins.length();i++){
if(ins[i]=='L' or ins[i]=='R') d=cd(d,ins[i]);
else if(ins[i]=='F' and mx==x and d=='E' and lostpos[mx][my]=='*') continue;
else if(ins[i]=='F' and mx==0 and d=='W' and lostpos[mx][my]=='*') continue;
else if(ins[i]=='F' and my==y and d=='N' and lostpos[mx][my]=='*') continue;
else if(ins[i]=='F' and my==0 and d=='S' and lostpos[mx][my]=='*') continue;
else if(ins[i]=='F' and mx==x and d=='E'){
lostpos[mx][my]='*';
isbotlost=true;
break;
}
else if(ins[i]=='F' and mx==0 and d=='W'){
lostpos[mx][my]='*';
isbotlost=true;
break;
}
else if(ins[i]=='F' and my==y and d=='N'){
lostpos[mx][my]='*';
isbotlost=true;
break;
}
else if(ins[i]=='F' and my==0 and d=='S'){
lostpos[mx][my]='*';
isbotlost=true;
break;
}
else pos(&mx,&my,d);
}
if(isbotlost==true) cout << mx << " " << my << " " << d << " LOST" << endl;
else cout << mx << " " << my << " " << d << endl;
}
}
你的lostpost陣列開太小了(注意(x, y)是右上角頂點座標,左下角頂點座標是(0, 0),所以大小是x+1, y+1),所以可能會超出範圍。至於為什麼這樣會造成少輸出我也不知道,只能說undefined behavior什麼神奇的事都有可能發生...如果有人知道原因的話歡迎補充...
謝謝! 修改後通過了,原來是陣列開太小了...