黎子卿 发表于 2024-2-18 21:12:46

为什么说我编译失败啊

#include <iostream>
#include <cstring>
using namespace std;
int n,m,h;
int startx,starty,endx,endy;
int a;
int vis;
int xx = {1,0,-1,0};
int yy = {0,1,0,-1};
bool yes = false;
void dfs(int x,int y,int xue)
{
    if(xue>=h or yes)
    {
      return;
    }
    else if(x==endx and y==endy)
    {
      cout << "Yes" << '\n';
      yes = true;
      return;
    }
    vis = 1;
    for(int i = 0;i<4;i++)
    {
      int newx = x+xx;
      int newy = y+yy;
      if(newx>=1 and newx<=n and newy>=1 and newy<=m andvis==0)
      {
            dfs(newx,newy,xue+a);
      }
    }
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    int q;
    cin >> q;
    while(q--)
    {
      memset(vis,0,sizeof(vis));
      yes = false;
      cin >> n >> m >> h;
      for(int i = 1;i<=n;i++)
      {
            for(int j = 1;j<=m;j++)
            {
                char s;
                cin >> s;
                if(s=='S')
                {
                  startx = i;
                  starty = j;
                }
                else if(s=='T')
                {
                  endx = i;
                  endy = j;
                }
                else
                {
                  a = s-'0';
                }
            }
      }
      dfs(startx,starty,0);
      if(not yes)
      {
            cout << "No" << "\n";
      }
    }
    return 0;
}

FishC 发表于 2024-2-18 21:13:09

你的代码出现了一些问题,导致编译失败。让我来帮你修复一下。

首先,你需要包含`iostream`头文件,这样才能使用输入输出流。然后,在代码中有几个地方需要做一些修改。

1. 数组`xx`和`yy`的声明方式不正确。你可以将它们声明为`int xx[] = {1,0,-1,0};`和`int yy[] = {0,1,0,-1};`。

2. 在`cout << n >> m >> h;`这一行,你需要将左移运算符`<<`改为右移运算符`>>`,这样才能正确读取`n`、`m`和`h`的值。即`cout >> n >> m >> h;`改为`cin >> n >> m >> h;`。

3. 在最后一行的`cout`语句中,`not`是Python中的关键字,而在C++中使用`!`来表示否定。所以将`not yes`改为`!yes`。

修复完这些问题后,你的代码应该可以顺利编译了。如果还有其他问题,请随时告诉我。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

zhangchenyvn 发表于 2024-2-21 16:03:41

检查有没有缺头文件神马的

三体人的智子 发表于 2024-2-22 10:23:46

问题:尺度过大
试一下我修改后的吧#include <iostream>
#include <cstring>
using namespace std;
int n,m,h;
int startx,starty,endx,endy;
int a;
int vis;
int xx = {1,0,-1,0};
int yy = {0,1,0,-1};
bool yes = false;
void dfs(int x,int y,int xue)
{
    if(xue>=h or yes)
    {
      return;
    }
    else if(x==endx and y==endy)
    {
      cout << "Yes" << '\n';
      yes = true;
      return;
    }
    vis = 1;
    for(int i = 0;i<4;i++)
    {
      int newx = x+xx;
      int newy = y+yy;
      if(newx>=1 and newx<=n and newy>=1 and newy<=m andvis==0)
      {
            dfs(newx,newy,xue+a);
      }
    }
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    int q;
    cin >> q;
    while(q--)
    {
      memset(vis,0,sizeof(vis));
      yes = false;
      cin >> n >> m >> h;
      for(int i = 1;i<=n;i++)
      {
            for(int j = 1;j<=m;j++)
            {
                char s;
                cin >> s;
                if(s=='S')
                {
                  startx = i;
                  starty = j;
                }
                else if(s=='T')
                {
                  endx = i;
                  endy = j;
                }
                else
                {
                  a = s-'0';
                }
            }
      }
      dfs(startx,starty,0);
      if(not yes)
      {
            cout << "No" << "\n";
      }
    }
    return 0;
}

给个最佳答案吧,求求了
页: [1]
查看完整版本: 为什么说我编译失败啊