首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求赐教·一道C++题,不知道错哪儿。新手哦,请多多关照

2013-01-01 
求指教···一道C++题,不知道错哪儿。新手哦,请多多关照!原题是这样的~~~The football foundation (FOFO) has

求指教···一道C++题,不知道错哪儿。新手哦,请多多关照!
原题是这样的~~~
The football foundation (FOFO) has been researching on soccer; they created a set of sensors to describe the ball behavior based on a grid uniformly distributed on the field. They found that they could predict the ball movements based on historical analysis. Each square sensor of the grid can detect the following patterns: 


N  north (up the field)
S  south (south the field)
E  east (to the right on the field)
W  west (to the left on the field) 
For example, in grid 1, suppose the ball was thrown into the field from north side into the field. The path the sensors detected for this movement follows as shown. The ball went through 10 sensors before leaving the field. 

Comparing with what happened on grid 2, the ball went through 3 sensors only once, and started a loop through 8 instructions and never exits the field. 

You are selected to write a program in order to evaluate line judges job, with the following out put based on each grid of sensors, the program needs to determine how long it takes to the ball to get out of the grid or how the ball loops around. 


Input
There will be one or more grids of sensors for the same game. The data for each is in the following form. On the first line are three integers separated by blanks: The number of rows in the grid, the number of columns in the grid, and the number of the column in which the ball enters from the north. The grid column's number starts with one at the left. Then come the rows of direction instructions. The lines of instructions contain only the characters N, S, E or W, with no blanks. The end of input is indicated by a grid containing 0 0 0 as limits. 


Output
For each grid in the input there is one line of output. Either the ball follows a certain number of sensors and exits the field on any one of the four sides or else the ball follows the behavior on some number of sensors repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before is 1. 




Sample Input
3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0 0


Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)



然后我写的代码如下:
#include <iostream>
using namespace std;

int main()
{
    int a,b,c;
    
    while((cin>>a>>b>>c)&&(a!=0&&b!=0&&c!=0))
    {
         char s[10000];
         for(int m=0;m<a;m++)
         {
                 for(int n=0;n<b;n++)
                 {
                         cin>>s[m*b+n];
                 }
         }
         int k[100],f[100];
         int d=0,e=c-1;
         
         
         for(int i=1;i<=a*b;i++)
         {
                 if(s[d*b+e]=='N')
                 {
                      d--;
                 }
                 if(s[d*b+e]=='S')
                 {
                      d++;
                 }
                 if(s[d*b+e]=='E')
                 {
                      e++;
                 }
                 if(s[d*b+e]=='W')    
                 {
                      e--;
                 }
                 
           k[i]=d;


           f[i]=e;
           
           for(int j=0;j<i;j++)
           {
                   if((k[i]==k[j])&&(f[i]==f[j]))
                   {
                          cout<<j<<" step(s) before a loop of "<<(i-j)<<" step(s)"<<endl;
                          break;
                   }
           }
         
           if(d<0||d>a-1||e<0||e>b-1)
           {
                   cout<<i<<" step(s) to exit"<<endl;
                   break;
           }
         }
    }
           system("pause");
           return 0;
}   


对于我这个程序的输出,本来第一个应该是10,而我的变成了7.第二个我的就直接输出了很多,完全不符合题意,但是我自己看了下,真不知道错哪····
我是大一的新生,刚学C++,还有很多都不懂,所以希望各位大神们能多多帮忙,让我更上一个层次,十分感谢。
(由于我没学C语言,请大神们回复的时候多多包涵哦~~~)
谢谢各位亲了···
[解决办法]
 
d,e這兩變量設計的很好

k,f是用來做什麽,沒看明白。
k[100], 這個和a*b比起來,看起來很容易越界。

 for(int i=1;i<=a*b;i++) //這裡爲什麽從1開始呢?求赐教·一道C++题,不知道错哪儿。新手哦,请多多关照

热点排行