一道acm题目wrong answer 报错 求大神看看代码
题目的链接是http://acm.tju.edu.cn/toj/vcontest/showp9902_B.html我的代码#include<iostream>usingn...
题目的链接是http://acm.tju.edu.cn/toj/vcontest/showp9902_B.html
我的代码
#include <iostream>
using namespace std;
int main()
{
int i,j,lasti,lastj;
int x,y,in;
while(cin>>x>>y>>in&&x!=0)
{
char grid[1000][1000];
int count=0;
//建立网格
for(j=0;j<y+2;j++)
{
grid[0][j]='A';
grid[x+1][j]='A';
}
for(i=0;i<x+2;i++)
{
grid[i][0]='A';
grid[i][y+1]='A';
}
for(i=1;i<x+1;i++)
{
for(j=1;j<y+1;j++)
{
cin>>grid[i][j];
}
}
i=1;
j=in;
char ch='W';
while(grid[i][j]=='N'||grid[i][j]=='S'||grid[i][j]=='W'||grid[i][j]=='E')
{
ch++;
if(grid[i][j]=='S')
{
grid[i][j]=ch;
lasti=i;
lastj=j;
i++;
}
else if(grid[i][j]=='N')
{
grid[i][j]=ch;
lasti=i;
lastj=j;
i--;
}
else if(grid[i][j]=='E')
{
grid[i][j]=ch;
lasti=i;
lastj=j;
j++;
}
else if(grid[i][j]=='W')
{
grid[i][j]=ch;
lasti=i;
lastj=j;
j--;
}
count++;
}
if(grid[i][j]=='A')
{
cout<<count<<" step(s) to exit"<<endl;
}
if(grid[i][j]!='A')
{
int loop=grid[lasti][lastj]-grid[i][j]+1;
cout<<count-loop<<" step(s) before a loop of "<<loop<<" step(s)"<<endl;
}
}
return 0;
}
嗯 谢谢了 展开
我的代码
#include <iostream>
using namespace std;
int main()
{
int i,j,lasti,lastj;
int x,y,in;
while(cin>>x>>y>>in&&x!=0)
{
char grid[1000][1000];
int count=0;
//建立网格
for(j=0;j<y+2;j++)
{
grid[0][j]='A';
grid[x+1][j]='A';
}
for(i=0;i<x+2;i++)
{
grid[i][0]='A';
grid[i][y+1]='A';
}
for(i=1;i<x+1;i++)
{
for(j=1;j<y+1;j++)
{
cin>>grid[i][j];
}
}
i=1;
j=in;
char ch='W';
while(grid[i][j]=='N'||grid[i][j]=='S'||grid[i][j]=='W'||grid[i][j]=='E')
{
ch++;
if(grid[i][j]=='S')
{
grid[i][j]=ch;
lasti=i;
lastj=j;
i++;
}
else if(grid[i][j]=='N')
{
grid[i][j]=ch;
lasti=i;
lastj=j;
i--;
}
else if(grid[i][j]=='E')
{
grid[i][j]=ch;
lasti=i;
lastj=j;
j++;
}
else if(grid[i][j]=='W')
{
grid[i][j]=ch;
lasti=i;
lastj=j;
j--;
}
count++;
}
if(grid[i][j]=='A')
{
cout<<count<<" step(s) to exit"<<endl;
}
if(grid[i][j]!='A')
{
int loop=grid[lasti][lastj]-grid[i][j]+1;
cout<<count-loop<<" step(s) before a loop of "<<loop<<" step(s)"<<endl;
}
}
return 0;
}
嗯 谢谢了 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询