简体   繁体   English

程序的意外输出

[英]Unexpected Output of the program

problem statement : http://www.spoj.com/problems/NAKANJ/ MY SOLUTION: 问题陈述: http : //www.spoj.com/problems/NAKANJ/我的解决方案:

#include<bits/stdc++.h>
using namespace std;
int x[10]={0,2,2,-2,-2,1,1,-1,-1};
int y[10]={0,1,-1,1,-1,2,-2,2,-2};
int bfs(int a1,int b1,int a2,int b2)
{
    pair<int,int> p;
    int i;
    queue<pair<int,int> >q;
    int moves[9][9],visit[9][9],m,n;
    memset(moves,0,sizeof(moves));
    memset(visit,0,sizeof(visit));
    p.first=a1;
    p.second=b1;
    q.push(p);
    moves[a1][b1]=0;
    while(!q.empty())
    {
        p=q.front();
        q.pop();
        if(p.first==a2&&p.second==b2)
            return moves[a2][b2];
        for(i=1;i<=8;i++)
        {
            m=p.first+x[i];
            n=p.second+y[i];
            if(m>8||m<1||n>8||n<1)
                continue;
            else
            {
                visit[m][n]=1;
                moves[m][n]=moves[p.first][p.second]+1;
                q.push(make_pair(m,n));
            }
        }
    }
}
int main()
{
    long long int t;
    cin>>t;
    while(t--)
    {
        string d,f;
        cin>>d>>f;
        int s=d[0]-'a';int r=f[0]-'a';
        cout<<bfs(s+1,(int)d[1],r+1,(int)f[1])<<endl;
    }
    return 0;
}

Input :
3
a1 h8
a1 c2
h8 c3

output :
-1217403904
-1217403904 
-1217403904

what is the reason of this weird output. 这种奇怪输出的原因是什么? logic and algorithm implementation seem fine to me . 逻辑和算法实现对我来说似乎很好。 any help appreciated. 任何帮助表示赞赏。

Your moves array has 9 rows and 9 columns - 您的moves数组有9行9列-

int moves[9][9];  

While you return something from moves like this - 当您从这样的动作中返回某些东西时-

if(p.first==a2&&p.second==b2)
   return moves[a2][b2];

Make a check whether a2 and b2 are less than 9 - 检查a2和b2是否小于9-

if(p.first==a2&&p.second==b2){
   if(a2 < 9 && b2 <9){
    return moves[a2][b2];
  }
}

Your bfs() function reaches the end while not returning anything, and it's expected to return int . 您的bfs()函数在不返回任何内容的情况下到达末尾,并且预计将返回int

That means that you are either ignoring compiler warnings or silencing them, in both cases it's a bad idea, turn warnings on and listen to them, they tell you that your code almost surely has bugs. 这意味着您要么忽略编译器警告,要么静默它们,在两种情况下都不是一个好主意,请打开警告并聆听它们,它们告诉您您的代码几乎肯定存在错误。

Also, don't do this 另外,不要这样做

int s=d[0]-'a';int r=f[0]-'a';

it's too low quality code that makes your program alsmot impossible to understand, do it like this 质量太低的代码使您的程序无法理解,像这样

int s;
int r;

s = d[0] - 'a';
r = f[0] - 'a';

I think this is likely the problem : 我认为这可能是问题所在:

When you are casting a character which is not a digit, you should do : 投射非数字字符时,应执行以下操作:

int char_not_digit = char - 'a' ;

and when a digit to an int, 当数字转换为整数时,

int char_digit = char - '0';

Look at the solution here 这里看解决方案

Thus all 0th position character in the input string should be casted to int with subtracting 'a' and all 1th position character in the input string should be casted to int by subtracting '0' . 因此,输入字符串中所有第0个位置字符都应减去'a'强制转换为int ,而输入字符串中所有第1个位置字符应减去'0'强制转换为int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM