AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

845.八数码



0


题目链接 [八数码] https://www.acwing.com/problem/content/847/

求解,为什么这里的end声明在开头会报错ambiguous?
我把end的声明放在bfs中就能跑通。

错误的代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_map>

using namespace std;
string end="12345678x";
const int N = 4;

int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

int bfs(string start)
{

    queue<string> q;
    q.push(start);
    unordered_map<string,int> hash;
    hash[start]=0;

    while(q.size())
    {
        auto tt=q.front();
        q.pop();
        int distance=hash[tt];

        if(tt==end) return distance;

        int k=tt.find('x');
        int x=k/3,y=k%3;

        for(int i=0;i<4;i++)
        {
            int a=x+dx[i],b=y+dy[i];
            if(a>=0&&a<3&&b>=0&&b<3)
            {
                swap(tt[k],tt[a*3+b]);
                if(!hash.count(tt))
                {
                    q.push(tt);
                    hash[tt]=distance+1;
                }
                swap(tt[k],tt[a*3+b]);
            }
        }
    }
    return -1;
}

int main()
{
    string a;
    char c;
    for(int i=0;i<9;i++)cin>>c,a.push_back(c);
    cout<<bfs(a)<<endl;
    return 0;
}

编译器报了什么错误 xxx is ambiguous



提问于27天前
往昔的风往西的风
1221


1 个问答



0

就是重名了呗(include的包里有关于 end 的定义)。要学会看编译器给出的错误信息。

回答于27天前
rushhhhh
28907

明白了感谢 –  往昔的风往西的风   26天前


我来回答
你确定删除吗?

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息