C++萌新求助。宽搜代码怎么也调不出来。求助dalao

P2385 [USACO07FEB] Bronze Lilypad Pond B

cuizp @ 2018-10-18 23:48:48

#include <bits/stdc++.h>
using namespace std;

struct node
{
    int x;
    int y;
    int cnt;
}p,q;

int a[40][40],vis[40][40],n,m,m1,m2;

int main()
{
    cin>>n>>m>>m1>>m2;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            {
                cin>>a[i][j];

                if (a[i][j] == 3) p.x = i, p.y = j;
            }

//  cout<<p.x<<' '<<p.y<<endl;

    int dx[9]={0,m1,m1,-m1,-m1,m2,m2,-m2,-m2};
    int dy[9]={0,m2,-m2,m2,-m2,m1,-m1,m1,-m1};
    memset (vis,0,sizeof(vis));     
    queue <node> line;
    p.cnt = 0;
    line.push(p);

    while (!line.empty())
    {
        p = line.front();

        //cout<<p.cnt<<endl;
    //  cout<<p.x<<' '<<p.y<<endl;

        if (a[p.x][p.y] == 4)
            {
                cout<<p.cnt;

                return 0;
        //  break;
            }

        p.cnt++;

        for (int i = 1; i <= 8; i++)
            {
                q = p;
                q.x += dx[i];
                q.y += dy[i];

                if (q.x > n || q.x < 1 || q.y > m || q.y < 1 || a[q.x][q.y] == 0 || a[q.x][q.y] == 2 || vis[q.x][q.y]) continue;

                if (!vis[q.x][q.y] && a[q.x][q.y] == 1)
                    {
                        vis[q.x][q.y] = 1;
                        line.push(q);
                    }
            }

        line.pop();
    }

//  cout<<p.x<<' '<<p.y<<endl;
}

by JustinRochester @ 2018-10-19 08:00:51

if (!vis[q.x][q.y] && a[q.x][q.y] == 1)

???

你这样不是访问不到终点了吗 23333

if (!vis[q.x][q.y] && (a[q.x][q.y] == 1||a[q.x][q.y]==4))

|