#27720: WA(line:4) 請問為什麼我的答案會少輸出?


qazasd0131 (isu10803026a)

學校 : 義守大學
編號 : 88593
來源 : [60.249.202.127]
最後登入時間 :
2022-01-14 10:59:41
c082. 00118 - Mutant Flatworld Expolrers -- UVa118 | From: [219.69.120.16] | 發表日期 : 2021-10-26 00:22

您的答案為: 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;
}
}
 
#27729: Re:WA(line:4) 請問為什麼我的答案會少輸出?


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [39.9.74.255]
最後登入時間 :
2024-10-14 22:20:08
c082. 00118 - Mutant Flatworld Expolrers -- UVa118 | From: [39.8.195.167] | 發表日期 : 2021-10-26 19:03

您的答案為: 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什麼神奇的事都有可能發生...如果有人知道原因的話歡迎補充...

 

 
#27734: Re:WA(line:4) 請問為什麼我的答案會少輸出?


qazasd0131 (isu10803026a)

學校 : 義守大學
編號 : 88593
來源 : [60.249.202.127]
最後登入時間 :
2022-01-14 10:59:41
c082. 00118 - Mutant Flatworld Expolrers -- UVa118 | From: [219.69.120.16] | 發表日期 : 2021-10-27 16:41

您的答案為: 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什麼神奇的事都有可能發生...如果有人知道原因的話歡迎補充...

 

謝謝! 修改後通過了,原來是陣列開太小了...

 
ZeroJudge Forum