Bing碎冰 2022-01-16 23:53 采纳率: 83.3%
浏览 23
已结题

一道洛谷简单搜索题有一个数据没过


#include <bits/stdc++.h>
using namespace std;
int f[7][7];
int n, m, t;
int re;
int turn[4][2] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
void dfs(int x, int y)
{
    if (f[y][x] == 2)
    {
        re++;
        return;
    }
    f[y][x] = 1;
    for (int i = 0; i < 4; i++)
    {
        //判
        bool b = 0;
        if (y + turn[i][1] >= 1 && y + turn[i][1] <= n)
            if (x + turn[i][0] >= 1 && x + turn[i][0] <= m)
                if (f[y + turn[i][1]][x + turn[i][0]] != 1)
                    b = 1;
        //递归
        if (b)
        {
            dfs(x + turn[i][0], y + turn[i][1]);
        }
    }
    f[y][x] = 0;
}
int main()
{
    cin >> n >> m >> t;
    int stx, sty, enx, eny;
    cin >> stx >> sty >> enx >> eny;
    f[enx][eny] = 2;
    for (int i = 0; i < t; i++)
    {
        int tx, ty;
        cin >> tx >> ty;
        f[tx][ty] = 1;
    }
    f[stx][sty] = 1;
    dfs(stx, sty);
    cout << re;
    return 0;
}

img

附上题目地址


看了半天也不知道怎么回事

  • 写回答

1条回答 默认 最新

  • wzcwzcwzcwzcwzcwcz 2022-01-17 02:42
    关注
    
     
    #include <bits/stdc++.h>
    using namespace std;
    int f[7][7];
    int n, m, t;
    int re;
    int turn[4][2] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
    void dfs(int x, int y)
    {
        if (f[x][y] == 2)//x y反了
        {
            re++;
            return;
        }
        f[x][y] = 1;//x y
        for (int i = 0; i < 4; i++)
        {
            //判
            bool b = 0;
            if (y + turn[i][1] >= 1 && y + turn[i][1] <= n)
                if (x + turn[i][0] >= 1 && x + turn[i][0] <= m)
                    if (f[x + turn[i][0]][y + turn[i][1]] != 1)//x y
                        b = 1;
            //递归
            if (b)
            {
                dfs(x + turn[i][0], y + turn[i][1]);
            }
        }
        f[x][y] = 0;//x y
    }
    int main()
    {
        cin >> n >> m >> t;
        int stx, sty, enx, eny;
        cin >> stx >> sty >> enx >> eny;
        f[enx][eny] = 2;
        for (int i = 0; i < t; i++)
        {
            int tx, ty;
            cin >> tx >> ty;
            f[tx][ty] = 1;
        }
        f[stx][sty] = 1;
        dfs(stx, sty);
        cout << re;
        return 0;
    }
    

    x y顺序反了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月25日
  • 已采纳回答 1月17日
  • 创建了问题 1月16日

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路